static bool killPid() { int pid; if ((pid = readPid()) > 1) { return kill(pid, SIGTERM) == 0; } return 0; }
int Mpr::killMpr() { pid_t pid; pid = readPid(); if (pid < 0) { return MPR_ERR_NOT_FOUND; } mprLog(MPR_INFO, "Sending signal %d to process %d\n", SIGTERM, pid); if (kill(pid, SIGTERM) < 0) { if (errno == ESRCH) { mprLog(MPR_INFO, "Pid %d is not valid\n", pid); } else { mprLog(MPR_INFO, "Call to kill(%d) failed, %d\n", pid, errno); } return MPR_ERR_CANT_COMPLETE; } return 0; }
int CPCD::Process::start() { /* We need to fork() twice, so that the second child (grandchild?) can * become a daemon. The original child then writes the pid file, * so that the monitor knows the pid of the new process, and then * exit()s. That way, the monitor process can pickup the pid, and * the running process is a daemon. * * This is a bit tricky but has the following advantages: * - the cpcd can die, and "reconnect" to the monitored clients * without restarting them. * - the cpcd does not have to wait() for the childs. init(1) will * take care of that. */ logger.info("Starting %d: %s", m_id, m_name.c_str()); m_status = STARTING; int pid = -1; switch(m_processType){ case TEMPORARY:{ #ifndef _WIN32 /** * Simple fork * don't ignore child */ switch(pid = fork()) { case 0: /* Child */ setsid(); writePid(getpgrp()); if(runas(m_runas.c_str()) == 0){ signal(SIGCHLD, SIG_DFL); do_exec(); } _exit(1); break; case -1: /* Error */ logger.error("Cannot fork: %s\n", strerror(errno)); m_status = STOPPED; return -1; break; default: /* Parent */ logger.debug("Started temporary %d : pid=%d", m_id, pid); break; } #else //_WIN32 do_exec(); #endif break; } #ifndef _WIN32 case PERMANENT:{ /** * PERMANENT */ switch(fork()) { case 0: /* Child */ signal(SIGCHLD, SIG_IGN); switch(pid = fork()) { case 0: /* Child */ setsid(); writePid(getpgrp()); if(runas(m_runas.c_str()) != 0){ _exit(1); } signal(SIGCHLD, SIG_DFL); do_exec(); _exit(1); /* NOTREACHED */ break; case -1: /* Error */ logger.error("Cannot fork: %s\n", strerror(errno)); writePid(-1); _exit(1); break; default: /* Parent */ logger.debug("Started permanent %d : pid=%d", m_id, pid); _exit(0); break; } break; case -1: /* Error */ logger.error("Cannot fork: %s\n", strerror(errno)); m_status = STOPPED; return -1; break; default: /* Parent */ break; } break; } #endif default: logger.critical("Unknown process type"); return -1; } while(readPid() < 0){ sched_yield(); } errno = 0; pid_t pgid = IF_WIN(-1, getpgid(pid)); if(pgid != -1 && pgid != m_pid){ logger.error("pgid and m_pid don't match: %d %d (%d)", pgid, m_pid, pid); } if(isRunning()){ m_status = RUNNING; return 0; } m_status = STOPPED; return -1; }
/** * Program pooling a directory, and doing a scheduling. **/ int main (int argc, char** argv) { initPins(); adc_init(); lcd_init(); /** The status does not exist at the launch of the schduler prgram*/ clearFile(); int pid=readPid(); stopIfPidExists(pid); writePid(); SetChrMode(); int nbSecond; int remainingSeconds; uchar intensity; uchar tmpintensity; int i; int valueInFile; time_t whenItsComplete ; char timestr[7]; pinMode (RELAY_IN, OUTPUT); // Permanent loop checking file. int cycle=0; while(1){ valueInFile=getCoundownValue(); #ifndef PROD printf("pause:%d,valueInFile:%d\n",pauseSt,valueInFile); #endif if(valueInFile>=0){ nbSecond=valueInFile; // The countdown whenItsComplete = time(NULL)+nbSecond; remainingSeconds=whenItsComplete-time(NULL); } else{remainingSeconds=-1;} #ifndef PROD printf("nbsecond:%d\n",nbSecond); #endif do { /** * Do a regular reset of the LCD **/ if(cycle%500==0){ lcd_init(); } else if(cycle%60==0){ resetLcd(); } /** * Write the remaining seconds **/ if(cycle%60==0){ writeRemaining(remainingSeconds); } /** * Increment only if not in pause (Cycle increments will be at the end of the cycle loop. **/ updateStandbyStatus(); cycle++; if(remainingSeconds>-1){ if(pauseSt==IS_RUNNING){ remainingSeconds=whenItsComplete-time(NULL); }else{ whenItsComplete=remainingSeconds+time(NULL); } openRelay(); if(lastImmobileState==0||(time(NULL)-lastImmobileState)<NBSECONDBEFORECREENSHUTDOWN){ digitalWrite (TRANSISTOR, LOW); } else{ digitalWrite (TRANSISTOR, HIGH); } int seconds=remainingSeconds%60; int hours=remainingSeconds/3600; int minutes=remainingSeconds/60%60; sprintf(timestr,"%02d:%02d:%02d",hours,minutes,seconds); goHome(); lcd_text(timestr); #ifndef PROD printf("%s\n",timestr); #endif sleep(1); } /** * Block measuring intensity */ if(cycle%20==0){ intensity=get_ADC_Result(); #ifndef PROD printf("Intensite: %d\n",intensity); #endif } #ifndef PROD printf("Remaining seconds %d,cycle=%d\n",remainingSeconds,cycle); #endif } while ( remainingSeconds>0&& !isFilePresent()); #ifndef PROD // printf("Sortie boucle décompte\n"); #endif /** * Every 10 ccyle there is a full reset of the screen. Otherwise it is light reset. **/ if(cycle%100==0){ lcd_init(); } nbSecond=0; /** * **/ if(valueInFile==NO_FILE){ goHome(); closeRelay(); digitalWrite (TRANSISTOR, HIGH); lcd_text("Expire " ); #ifndef PROD printf("Expire\n"); #endif } else if(valueInFile==TV_ON){ goHome(); openRelay(); digitalWrite (TRANSISTOR, LOW); lcd_text("Tele on " ); } else if(valueInFile==TV_OFF){ goHome(); digitalWrite (TRANSISTOR, HIGH); closeRelay(); lcd_text("Tele off " ); } sleep(3); } }