int kill_proc (pid_t pid, int sig, int rty) { int orig_errno; int dead, rtyno; time_t proc_starttime, starttime; dead = 0; proc_starttime = 0; orig_errno = errno; errno = 0; for (rtyno = 0; ! dead; ) { if ((starttime = get_proc_starttime (pid)) == (time_t)-1) { if (errno != ESRCH) goto error; /* no such process, process must be dead */ dead = 1; } else { if (! proc_starttime) proc_starttime = starttime; if (proc_starttime == starttime) { if (rtyno > rty) break; if (kill (pid, sig) < 0) { if (errno != ESRCH) goto error; /* no such process, process must be dead */ dead = 1; } else { if (rtyno) sleep (KILL_PROC_INTVL); rtyno++; } } else { /* starttime not the same, original process must be dead */ dead = 1; } } } errno = orig_errno; return rtyno; error: return -1; }
int ifb_getProcStatus(int type) { char *dname, *ver, fname[100], pname[PROC_NAME_LEN]; int fd, status, pid, procIndex; DIR *dirp; time_t sysuptime; struct dirent *direntp; struct stat st; #if 0 if(get_system_uptime(&sysuptime)) return -1; #endif if( (dirp = opendir(PROC_DIR)) == NULL) { fprintf(stderr, "\n opendir fail[%s]; err=%d(%s)\n\n", PROC_DIR, errno, strerror(errno)); return -1; } while( (direntp = readdir(dirp)) != NULL) { dname = direntp->d_name; if(!isdigit(*dname)) continue; pid = atoi(dname); sprintf(fname, "/proc/%d/cmdline", pid); /* get the process owner */ if( (status = stat(fname, &st)) != 0) continue; if( (fd = open(fname, O_RDONLY)) < 0) continue; else { memset(pname, 0x00, PROC_NAME_LEN); if(read(fd, pname, PROC_NAME_LEN-1) < 0) { close(fd); continue; } else { close(fd); if( (procIndex = ifb_getProcIndex (pname)) < 0) continue; if(confProcTbl[procIndex].runCnt > 0) { if(getPPID(pid) != 1) continue; } confProcTbl[procIndex].runCnt++; confProcTbl[procIndex].pid = pid; strcpy(confProcTbl[procIndex].startTime, "-"); #if 0 get_proc_starttime(pid, sysuptime, NULL, confProcTbl[procIndex].startTime); strftime (confProcTbl[procIndex].startTime, 32, "%m-%d %H:%M",localtime((time_t*)&(st.st_atime))); #endif if( (ver = get_ver_str(pname)) != NULL) { fprintf(stderr, "It fails to get version information -[%s].\n", pname); strncpy(confProcTbl[procIndex].procVersion, "UNKNOWN", 10); } else { if(strlen(ver) == 0) strncpy(confProcTbl[procIndex].procVersion, "UNKNOWN", 10); else strncpy(confProcTbl[procIndex].procVersion, ver, 10); } } } } closedir(dirp); return 1; } //----- End of ifb_getProcStatus -----//