Path: utzoo!attcan!uunet!mcvax!hp4nl!botter!vulet!hans From: hans@let.vu.nl (Hans Varkevisser) Newsgroups: comp.sources.d Subject: autologout Keywords: autologout BSD 4.3 Message-ID: <557@vulet.let.vu.nl.UUCP> Date: 18 Jan 89 12:21:55 GMT Organization: V.U. Letteren, Amsterdam, the Netherlands Lines: 198 This only is for users of BSD 4.3 with w.c version 5.3 I have made changes to the program w.c and changed the name to autologout.c. I use the program autologout to logout users who are idle for a given time You set the time in the source file. There are two var for time, max_idle and max_limit. max_idle is used to logout a user who is idle for > max_idle and who only has the shellproces running (number of processes =1). max_limit is used to logout a user who is idle for > max_limit and can have more processes running, only the shellproces is killed. I use the kill -1, pid because -1 is the hangup signal . I find this the best way to do, of course you can use other signals. I use the var min_number to test for uid > given number, this is used by us to leave staff users logged in. I put an entry in crontab that start every 5 minutes autologout -k. Because of the copyright I only have send the diff between w.c and autologout.c. The next vars I used for the program, see the diff file at the end. #define KILL_SIGNAL "-1" /* use this for kill, -1 = hangup */ int tmp_pid = 0; /* temporary proces id */ int tmp_uid = 0; /* temporary user id */ int max_idle = 30; /* max idle time in minutes */ int max_limit = 60; /* max limit time in minutes */ int kill_on = 0; /* true if -k flag, kill idle process */ int tot_proc = 0; /* total of processes */ char tmp_itoa[10]; /* tmp var to convert int to string */ char command[512]; /* command string for systemcall */ int min_number = 100; /* user number less than min_number The text is the result of diff -e w.c autologout.c -------- cut here ------------- cut here ---------------- 673a } itoa(n,s) char s[]; int n; { int i, sign; if ((sign = n) < 0) n = -n; i = 0; do { s[i++] = n % 10 + '0'; } while (( n /= 10 ) > 0 ); if (sign < 0) s[i++] = '-'; s[i] = '\0'; reverse(s); } reverse (s) char s[]; { int c, i, j; for (i = 0, j = strlen(s)-1; i < j; i++, j--) { c = s[i]; s[i] = s[j]; s[j] = c; } } kill_shell() { /* convert int pid to string */ itoa(tmp_pid,tmp_itoa); /* put command in command string */ strcpy(command,"kill "); /* KILL_SIGNAL can be user defined */ strcat(command, KILL_SIGNAL); strcat(command, " "); /* append pid to commandstring */ strcat(command,tmp_itoa); /* kill user for idle > 60 minutes don't look at number of processes*/ if ((tmp_uid > min_number) && ( idle >= max_limit)){ /* execute a kill -1 on pid */ system(command); } /* kill user for idle > 30 minutes and only one process running */ if ((tmp_uid > min_number) && ( idle >= max_idle) && (tot_proc == 1)){ /* execute a kill -1 on pid */ system(command); } . 535a pr[np].w_pgrp = mproc.p_pgrp; . 344,374d 342c printf("pid %5d\t%-*.*s ", tmp_pid, NMAX, NMAX, utmp.ut_name); printf("%3d idle \t%-.32s\n", idle,doing ); . 340c . 318a } . 317a strcpy(tmp_doing, lflag ? pr[i].w_args : pr[i].w_comm); if ((doing[0] == '-' || doing[0] == 0) && (tmp_pid == 0)) tmp_pid=pr[i].w_pid; if ((tmp_doing[0] == '-' || tmp_doing[0] == 0) && (tmp_pid == 0)) tmp_pid=pr[i].w_pid; } /* -k flag log users out who are idle > 30 minutes */ if ((kill_on == 1) && ( tmp_pid >= 10)) kill_shell(); else putline(); . 304,316c if(pr[i].w_pid>curpid && (pr[i].w_igintr!=IGINT || empty)){ curpid = pr[i].w_pid; strcpy(doing, lflag ? pr[i].w_args : pr[i].w_comm); . 283,297c tot_proc++; tmp_uid=pr[i].w_uid; if (debug) { printf("\t%5d\t%s",pr[i].w_pid,pr[i].w_args); . 277a tot_proc=0; tmp_pid=0; . 208,260d 201,206d 197,199d 163c case 'a': . 145a case 'k': kill_on++; break; . 106a /* new variables for autologout */ #define KILL_SIGNAL "-1" /* use this for kill, -1 = hangup */ int tmp_pid = 0; /* temporary proces id */ int tmp_uid = 0; /* temporary user id */ int max_idle = 30; /* max idle time in minutes */ int max_limit = 60; /* max limit time in minutes */ int kill_on = 0; /* true if -k flag, kill idle process */ int tot_proc = 0; /* total of processes */ char tmp_itoa[10]; /* tmp var to convert int to string */ char command[512]; /* command string for systemcall */ int min_number = 100; /* user number less than min_number * won't be affected */ char tmp_doing[520]; /* process attached to terminal */ . 41a short w_pgrp; /* proc.p_pgrp */ . 17,22d 14c static char sccsid[] = "@(#)autologout.c 1.0 (Free University) 1/11/89"; . 7,11c /* This programm is adapted from the programm w.c. * There some changes and some features are deleted * I add the autologout feature, the programm looks at the * idle time and the number of processes. If the idle time is > 30 * and the number of processes = 1 the the user is logout by kill signal * For safety there is a check on the uid, on our computer we only kill * the user if the uid > 100. * Hans Varkevisser, Fac of Literature Free University the Netherlands * let.vu.nl!hans or hans@let.vu.nl (UUCP) */ . ----------- end ----------------- -- Organisation: Free University, Faculty of Literature Amsterdam, the Netherlands Name: Hans Varkevisser (system administrator) hans@let.vu.nl or let.vu.nl!hans UUCP