#define _GNU_SOURCE #include #include #include #include #include #include #include int main(int argc,char * argv[]){ DIR *dir; struct dirent *d; struct stat st; int fd; char buf[256]; int n; char *p; int i; int signum; pid_t sid,pid; pid_t mypid = getpid(); pid_t mysid = getsid(mypid); if(argc < 2) exit(EXIT_FAILURE); signum = strtol(&argv[1][argv[1][0] == '-' ? 1 : 0],NULL,10); if(signum == 0) exit(EXIT_FAILURE); if(stat("/proc/version",&st) < 0) exit(EXIT_FAILURE); dir = opendir("/proc"); if(dir == NULL) exit(EXIT_FAILURE); while((d = readdir(dir))!=NULL){ pid = strtoul(d->d_name,&p,10); if(*p != '\0') continue; if(pid == 1) continue; snprintf(buf,sizeof(buf),"/proc/%s/stat",d->d_name); fd = open(buf,O_RDONLY); if(fd == -1) continue; n = read(fd,buf,sizeof(buf)-1); close(fd); buf[n] = '\0'; p = buf; for(i=0;i<5;i++){ p = strchr(p,' '); if(p==NULL) break; p++; } if(p==NULL) continue; sid = strtoul(p,&p,10); if(*p != ' ') continue; if(sid == mysid) continue; printf("Killing %d with %d\n",pid,signum); } return(EXIT_SUCCESS); }