home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2635 < prev    next >
Encoding:
Internet Message Format  |  1991-01-31  |  1.7 KB

  1. From: jay@gdx.UUCP (Jay A. Snyder)
  2. Newsgroups: alt.sources
  3. Subject: logout idle users on selected ports
  4. Message-ID: <114@gdx.UUCP>
  5. Date: 27 Jan 91 01:07:05 GMT
  6.  
  7.  
  8. I wanted to be able to do idle logout for dialins only, but not
  9. console jobs (to keep the dialin lines open).  So I wrote this little
  10. program.  It must be run as root.
  11.  
  12. This is a simpile little utility that will logout selected terminals
  13. after a specified idle time is reached.
  14.  
  15. usage:
  16. idleout [time in minutes] list of ttys
  17.  
  18. --- idleout.c ----
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <sys/signal.h>
  23. #include <utmp.h>
  24.  
  25. kill_process(pid)
  26.      short pid;
  27. {
  28.   if (kill(pid,SIGHUP)) /* try hangup */
  29.     if (kill(pid,SIGTERM)) /* try term */
  30.       kill(pid,SIGKILL); /* just brute force kill it! */
  31. }
  32.  
  33. time_t idletime(linename)
  34.      char *linename;
  35. {
  36.   char filename[40];
  37.   struct stat s;
  38.   sprintf(filename,"/dev/%s",linename);
  39.   stat(filename,&s);
  40.   return((time((time_t *) 0)-s.st_mtime)/60);
  41. }
  42.  
  43. main(argc,argv)
  44.      int argc;
  45.      char *argv[];
  46. {
  47.   struct utmp *u,uu;
  48.   int i;
  49.   time_t t,maxidle;
  50.  
  51.   if (argc<3)
  52.     {
  53.       fprintf(stderr,"usage: idletime [time] [tty] ...\n");
  54.       exit(-1);
  55.     }
  56.   if (fork()) exit(0); /* run as daemon */
  57.   close(0);
  58.   close(1);
  59.   close(2);
  60.   setpgrp(); /* detach from process group */
  61.   maxidle=atoi(argv[1]);
  62.   uu.ut_user[0]=0;
  63.   uu.ut_id[0]=0;
  64.   uu.ut_line[0]=0;
  65.   uu.ut_pid=0;
  66.   uu.ut_type=0;
  67.   for (;;)
  68.     {
  69.       for (i=2;i<argc;++i)
  70.     {
  71.       setutent();
  72.       strcpy(uu.ut_line,argv[i]);
  73.       u=getutline(&uu);
  74.       if (u->ut_type==USER_PROCESS)
  75.         {
  76.           if (idletime(u->ut_line)>maxidle)
  77.         {
  78.           kill_process(u->ut_pid);
  79.         }
  80.         }
  81.     }
  82.       sleep(60);
  83.     }
  84. }
  85.