Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!im4u!swrinde!petro!tness1!mechjgh From: mechjgh@tness1.UUCP (8753) Newsgroups: comp.unix.wizards Subject: Re: watch-dog program Message-ID: <191@tness1.UUCP> Date: Mon, 11-May-87 22:22:32 EDT Article-I.D.: tness1.191 Posted: Mon May 11 22:22:32 1987 Date-Received: Sat, 16-May-87 01:12:53 EDT References: <7305@brl-adm.ARPA> Organization: Southwestern Bell, Texas Lines: 190 Summary: source to hogs.c In article <7305@brl-adm.ARPA>, rachiele@nadc.arpa writes: > > I am in need of an automatic logout program for unix sys V (2.2) running on > a VAX785. By this I mean, some software which will logout inactive > users after some set amount of idle time. Can anyone help me out? > Thanks. > Jim > rachiele@nadc.arpa Here is the source for hogs.c which I wrote specifically for our VAX 785 with S5R2. It ignores the root login, handles users that are on sxt pseudo ports, and has an anti-logoff file for special users. The code isn't state-of-the-art, but neither am I ! It's your's to try. Greg Hackney ---------- /* hogs.c Greg Hackney 8-16-86 ihnp4!tness1!mechjgh * Southwestern Bell Telephone Co. (713+521-8753) * 3100 Main, Room 1009 * Houston, Texas 77001 * * to compile: make hogs * * run as root in cron to kill idle users * as often as desired (tested on AT&T S5R2 only) * * Usage: hogs -t time-in-minutes [ -f Anti_Kill_File ] * i.e.: hogs -t 30 -f /etc/antikill (to kill users idle for 30 minutes or more) * * Typical crontab entry: * 0,15,30,45 * * * * /etc/hogs -t 30 -f /etc/antikill * * optional Anti_Kill_File may contain a vertical list of * lognames to be made exempt from the kill * * * This is considered public domain software and not for resale */ #include #include #include #include #include #include #include #define USAGE "Usage: hogs -t time-in-mins [ -f Anti_Kill_File ]" extern char *optarg; FILE *anti; int DEV, DEBUG; #define YES 1 #define NO 0 char *malloc(); time_t ktime; /*minutes of grace allowed before kill*/ long now; /*number of clock ticks since 1-1-70 AD (after dec)*/ struct utmp *getutent(); struct utmp *READ; struct stat statbuf; struct termio term; char *string; char antifile[40], /*pathname to antikill exemption file*/ Device[20]; /*pathname to users tty*/ int aflag; main(argc,argv) int argc; char *argv[]; { int c; DEBUG=NO; if(argc < 2){ fprintf(stderr,"%s\n",USAGE); exit(1); } while((c=getopt(argc,argv,"dt:f:"))!=EOF){ switch(c){ case 'f': aflag=YES; strcpy(antifile,optarg); break; case 't': /*get cutoff time*/ ktime=atol(optarg); break; case 'd': DEBUG = YES; break; default: fprintf(stderr,"%s\n",USAGE); exit(1); break; } } if((string=malloc(512))==NULL){ fprintf(stderr,"Memory allocation error\n"); exit(1); } if(aflag==YES){ /*if antikill file requested with -f option*/ if((anti=fopen(antifile,"r"))==NULL) aflag=NO; } /*get current time*/ now=time((long *)0); while((READ = getutent() )!=NULL){ if(READ->ut_type != USER_PROCESS) /*look for user process*/ continue; /*get idle time for the device*/ strcpy(Device,"/dev/"); strcat(Device,READ->ut_line); stat(Device,&statbuf); if(((now - statbuf.st_mtime)/60) < ktime) /*skip active users*/ continue; if(aflag == YES) /*if antikill file is open*/ if(nokill() == YES) continue; /*an exempt user*/ /*don't ever kill console*/ if((strcmp("root",READ->ut_user))==0) continue; /* sxt devices */ if((strncmp("sxt\0",READ->ut_line,3))==0){ if((kill(READ->ut_pid,1)) != -1) if((kill(READ->ut_pid,0)) != -1) if((kill(READ->ut_pid,9)) != -1) ; continue; } /*hang up modem*/ if((DEV=open(Device,O_RDWR))==-1) continue; if((ioctl(DEV,TCGETA,&term))==-1){ close(DEV); continue; } if(DEBUG == NO){ /*initiate hangup*/ term.c_cflag &= ~CBAUD; term.c_cflag |= B0 & CBAUD; } if((ioctl(DEV,TCSETAW,&term))==-1){ close(DEV); continue; } close(DEV); KILLALL(); /*obliterate the hog's processes !!!*/ } fclose(anti); /*be neat and tidy*/ exit(0); } nokill() /*if user is exempt, return YES, else NO */ { rewind(anti); while((fgets(string,512,anti))!=NULL){ string=strtok(string,"\n"); if((strcmp(string,READ->ut_user))==0) return(YES); } return(NO); } KILLALL() /* kill all processes associated with a given tty*/ { FILE *ps; char buf[512]; sprintf(buf,"/bin/nohup /bin/ps -t %s\0",READ->ut_line); if((ps=popen(buf,"r"))==NULL) return(1); fgets(buf,512,ps); while((fgets(buf,512,ps))!=NULL){ string=strtok(buf," "); if(DEBUG == YES) printf("kill process %s\n",string); else kill(atoi(string),1); if((kill(READ->ut_pid,0))==0) kill(READ->ut_pid,9); } pclose(ps); } ---------