/* * lpudated.c - Simple timestamping daemon */ #include #include #include #include #include #include #include #include #include #include // #include #include #include #define WORKERCOUNT 1 #define WORKERSLEEPSECS 5 sigset_t g_stSigSet; pthread_t g_stPid; int bRun = 1; int _osal_task_pause (unsigned short sleep_time) { struct timespec ts; ts.tv_sec = sleep_time / 1000; ts.tv_nsec = (sleep_time % 1000) * 1000000; if (nanosleep(&ts, NULL) != 0) return -1; return 0; } static void* sigthread_routine(void *pArg) { sigset_t *pstSigSet = &g_stSigSet; int iSigNum = 0; pthread_t stPid = pthread_self(); int iRet = -1; /* pthread_detach(stPid); */ while(1){ iRet = sigwait(pstSigSet, &iSigNum); if(-1 == iRet){ fprintf(stderr, "Signal error: %s", strerror(errno)); } bRun = 0; break; } return NULL; } static void* worker_a(void *pArg) { pid_t sm_cid; pthread_t stPid = pthread_self(); pthread_detach(stPid); /* Finally do out work */ // char *sm_args[] = {"sudo", "swManager", "swpactivation", "activate", "forced", "standby", NULL }; char *sm_args[] = {"dd", "if=/dev/zero", "of=/dev/null", "bs=1k", "count=1024", NULL }; while(bRun){ sm_cid = fork(); if( sm_cid == 0 ) { //child if (execvp(sm_args[0], sm_args) == -1){ // LOGGER(LCON|LRAM, SWD, SDL, ERR, "%s() - ERROR activating sw package - errno=%d \n", __FUNCTION__, errno); } } /* wait for the SW Manager activation execution */ waitpid(sm_cid, NULL, 0); _osal_task_pause(WORKERSLEEPSECS); } } static void* worker_b(void *pArg) { int num = (int)pArg; int fd, len; time_t timebuf; char fname[1024] = {0}; sprintf(fname, "/dev/null"); pthread_t stPid = pthread_self(); pthread_detach(stPid); /* Finally do out work */ len = strlen(ctime(&timebuf)); while(bRun){ char *buf = malloc(sizeof(char)*(len+1)); if(buf == NULL){ fprintf(stderr, "malloc"); exit(EXIT_FAILURE); } if((fd == open(fname, O_WRONLY|O_APPEND, 0600)) < 0){ fprintf(stderr, "open"); exit(EXIT_FAILURE); } time(&timebuf); strncpy(buf, ctime(&timebuf), len+1); write(fd, buf, len+1); free(buf); close(fd); _osal_task_pause(WORKERSLEEPSECS); } } int main(void) { pid_t pid, sid; time_t timebuf; int fd, len; pthread_t worker_stPid[WORKERCOUNT]; pthread_t worker_b_stPid[WORKERCOUNT]; void *pRet; int i; fprintf(stderr, "zyan1 lpudated is starting.\n"); pid = fork(); if(pid < 0) { fprintf(stderr, "%s\n", strerror(errno)); exit(EXIT_FAILURE); } if(pid > 0){ /* In the parent, let's bail */ exit(EXIT_SUCCESS); } /* In the child ... */ /* Open the system log */ //openlog("lpudated", LOG_PID, LOG_DAEMON); /* First, start a new session */ if((sid = setsid())<0){ fprintf(stderr, "%s\n", "setsid"); exit(EXIT_FAILURE); } #if 0 if ((pid = fork()) < 0) { fprintf(stderr, "%s %d, %s\n", __func__, __LINE__, strerror(errno)); exit(EXIT_FAILURE); }else if (0!=pid) { //parent exit(EXIT_SUCCESS); } #endif /* Next, make / the current directory */ if(chdir("/")<0){ fprintf(stderr, "%s\n", "chdir"); exit(EXIT_FAILURE); } /* Reset the file mode */ umask(0); /* close the unneeded file descriptors */ close(STDIN_FILENO); close(STDOUT_FILENO); //close(STDERR_FILENO); sigset_t *pstSigSet = &g_stSigSet; sigemptyset(pstSigSet); sigaddset(pstSigSet, SIGHUP); sigaddset(pstSigSet, SIGINT); sigaddset(pstSigSet, SIGTERM); sigaddset(pstSigSet, SIGKILL); if(pthread_sigmask(SIG_BLOCK, pstSigSet, NULL)<0){ fprintf(stderr, "Cannot set signal mask\n"); exit(EXIT_FAILURE); } for(i=0;i < WORKERCOUNT; i++){ pthread_create(&worker_stPid[i], NULL, worker_a, (void*)i); } for(i=0;i < WORKERCOUNT; i++){ pthread_create(&worker_b_stPid[i], NULL, worker_b, (void*)i); } if(pthread_create(&g_stPid, NULL, sigthread_routine, NULL)<0){ fprintf(stderr, "Can not start signal handler thread.\n"); exit(EXIT_FAILURE); } pthread_join(g_stPid, &pRet); /* Close the system log and scram */ //closelog(); exit(EXIT_SUCCESS); }