home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / amiga / 2629 < prev    next >
Encoding:
Text File  |  1993-01-04  |  7.6 KB  |  196 lines

  1. Newsgroups: comp.unix.amiga
  2. Path: sparky!uunet!spool.mu.edu!agate!rsoft!mindlink!a218
  3. From: Charlie_Gibbs@mindlink.bc.ca (Charlie Gibbs)
  4. Subject: Re: Need help on workstation...
  5. Organization: MIND LINK! - British Columbia, Canada
  6. Date: Mon, 4 Jan 1993 22:49:19 GMT
  7. Message-ID: <19225@mindlink.bc.ca>
  8. Sender: news@deep.rsoft.bc.ca (Usenet)
  9. Lines: 185
  10.  
  11. In article <4JAN199322260588@v9000> sd7047787@v9000 (SD7047787) writes:
  12.  
  13. >Hi!
  14. >I have some problems on workstation. I know this is not the
  15. >appropriate place to ask. But pardon me, I really need the answer fast and I
  16. >dunno
  17. >which usenet discussion list should I ask. So would some kind souls
  18. >out there please help me:
  19. >
  20. >Problem Encounter: In the company I attached, ppl from different departments
  21. >log into the few Sun workstations. They left for urgent meetings etc. quite
  22. >oftenly without logging out from the station. This cause inconvience to
  23. >others who came at random to use the stations.
  24. >
  25. >Solution Requirement: Is there any daemons/programs that came with Sun that
  26. I
  27. >can invoke so that
  28. >users idled for a period of time will automatically be logged out? I know
  29. that
  30. >this is quite
  31. >similar to a screen blanker. But I have no idea how to do/program it because
  32. it
  33. >is also required that the processes status and application programs eg. CAD
  34. >program files etc. called/created by the user are saved before the user is
  35. >logged out
  36. >of the workstation.
  37. >
  38. >I am no experience user for workstation. I think it might not be possible to
  39. >achieve the requirement. But I need advices. Please reply to my address
  40. >as far as possible. All suggestions are welcomed! Thanx a million!!!
  41.  
  42.      I don't know where I found this, and I haven't had a chance to
  43. try it out, but I thought it might be handy some day so I kept it:
  44.  
  45. --------------------------- cut here ------------------------------
  46. /* "@(#) autologout.c by David Dickson" */
  47. #include    <stdio.h>
  48. #include    <signal.h>
  49. #include    <string.h>
  50. #include    <sys/types.h>
  51. #include    <sys/stat.h>
  52. #include    <utmp.h>
  53. #define     GRACE       60  /* grace time (sec) for user reply */
  54. #define     KWAIT       20  /* time to wait after kill (in sec.) */
  55. #define     WARNING     1   /* a warning message */
  56. #define     LOGOFF      2   /* a log-off message */
  57. #define     NOLOGOFF    3   /* a log-off failure message */
  58. #define     SEC         60  /* seconds per minute */
  59. #define     TIMEMAX     20  /* maximum idle time (minutes) */
  60.  
  61. char        *ctime();       /* returns pointer to time string */
  62. struct utmp *utmpp;         /* pointer to utmp file entry */
  63. struct utmp *getutent();    /* returns next utmp file entry */
  64.  
  65. main()
  66. {
  67.     if (fork())             /* the parent process */
  68.         exit(0);            /* exits */
  69.     /* the child processes all utmp file entries: */
  70.     while ((utmpp = getutent()) != (struct utmp *) NULL)
  71.         check_idle();
  72.     exit(0);    /* done, so bye */
  73. }
  74.  
  75. check_idle()    /* select utmp entries older than TIMEMAX */
  76. {
  77.     char        dev[24], name[12];
  78.     struct stat status, *pstat;
  79.     time_t      idle, pres_time, start, time();
  80.  
  81.     pstat = &status;    /* point to status structure */
  82.     if (utmpp->ut_type != USER_PROCESS) /* if not user process */
  83.         return(0);                      /* skip the utmp entry */
  84.     strcpy(dev, "/dev/");   /* add "/dev/" directory prefix */
  85.     strcat(dev, utmpp->ut_line);    /* append basename of port */
  86.     if (stat(dev, pstat))   /* if can't get status for port */
  87.         bailout("can't get status of user's terminal", 1);
  88.     pres_time = time((time_t *)0);  /* get current time */
  89.     /* idle time is current time less last access time: */
  90.     idle = (pres_time - pstat->st_atime) / SEC;
  91.     if (idle < TIMEMAX)     /* if user was recently active */
  92.         return(0);          /* ignore this utmp entry */
  93.     strncpy(name, utmpp->ut_user, 8);   /* else get user name */
  94.     name[8] = '\0';          /* null terminate user name string */
  95.     mesg(WARNING, name, dev, (int)idle); /* send warning to user */
  96.     if (stat(dev, pstat))
  97.         bailout("can't get status of user's terminal", 2);
  98.     start = pstat->st_atime;    /* start time for countdown */
  99.     sleep(GRACE);
  100.     if (stat(dev, pstat))
  101.         bailout("can't get status of user's terminal", 3);
  102.     if (start < pstat->st_atime)    /* user did something */
  103.         return(0);
  104.     else {  /* user abandoned terminal */
  105.         if (!killit(utmpp->ut_pid))
  106.             mesg(NOLOGOFF, name, dev, (int)idle); /* couldn't kill */
  107.         else
  108.             mesg(LOGOFF, name, dev, (int)idle); /* okay */
  109.         return(0);
  110.     }
  111. }
  112.  
  113. mesg(flag, name, dev, idle) /* mail to user and log message */
  114. int     flag, idle;     /* flag indicates message type */
  115. char    *name, *dev;
  116. {
  117.     char    mbuf[256];  /* message buffer */
  118.     time_t  tvec;
  119.     FILE    *fopen(), *fp, *log, *mprog;
  120.  
  121.     time(&tvec);    /* store time in tvec */
  122.     if ((log = fopen("/usr/adm/logoutlog", "a")) == (FILE *) NULL)
  123.         bailout("can't open log file", 4);
  124.     if (flag == WARNING) {  /* process warning message */
  125.         if ((fp = fopen(dev, "w")) == (FILE *) NULL)
  126.             bailout("can't open user's terminal", 5);
  127.         fprintf(fp,
  128.             "%s: You've been idle for %d min.\07\n", name, idle);
  129.         fprintf(fp,
  130.             "you'll be logged off in 60 sec. unless you hit return:");
  131.         fclose(fp);
  132.         fprintf(log, "** WARNING ** %s %s (%d min idle time) %s",
  133.             name, dev+5, idle, ctime(&tvec)+3);
  134.     }
  135.     if (flag == LOGOFF) {   /* process log-off message */
  136.         fprintf(log, "** LOGOFF ** %s %s (%d min idle time) %s",
  137.             name, dev + 5, idle, ctime(&tvec) + 3);
  138.         sprintf(mbuf, "/bin/mail %s", name);
  139.         /* open pipe to mail program for writing */
  140.         if ((mprog = popen(mbuf, "w")) == (FILE *) NULL)
  141.             bailout("can't use /bin/mail program", 6);
  142.         fprintf(mprog, "Subject: Excess Idle Time\nLogged \
  143. off - excess idle time - %s %s\ntty = %s\n",
  144.             name, ctime(&tvec), dev + 5);
  145.         fclose(mprog);
  146.     }
  147.     if (flag == NOLOGOFF) {  /* send mail to root if can't kill */
  148.         fprintf(log, "** LOGOFF-FAIL ** %s (pid = %d) \
  149. %s (%d min idle time) %s",
  150.             name, utmpp->ut_pid, dev+5, idle, ctime(&tvec)+3);
  151.         sprintf(mbuf, "/bin/mail root");
  152.         if ((mprog = popen(mbuf, "w")) == (FILE *) NULL)
  153.             bailout("can't use /bin/mail program", 7);
  154.         fprintf(mprog, "Subject: Can't logoff %s\nCan't Log off \
  155. - %s %s\ntty = %s\n", name, name, ctime(&tvec), dev+5);
  156.         fclose(mprog);
  157.     }
  158.     fclose(log);
  159.     return(0);
  160. }
  161.  
  162. killit(pid) /* terminate process using SIGHUP, then SIGKILL */
  163. int pid;
  164. {
  165.     kill(pid, SIGHUP);  /* first send "hangup" signal */
  166.     sleep(KWAIT);
  167.     if (!kill(pid, 0)) {    /* SIGHUP might be ignored */
  168.         kill(pid, SIGKILL); /* then send sure "kill" signal */
  169.         sleep(KWAIT);
  170.         if (!kill(pid, 0))
  171.             return(0);  /* failure--refuses to die! */
  172.         else
  173.             return(1);  /* successful kill with SIGKILL */
  174.     } else
  175.         return(1);  /* successful kill with SIGHUP */
  176. }
  177.  
  178. bailout(message, status) /* display error message and exit */
  179. int     status;     /* exit status */
  180. char    *message;   /* pointer to the error message */
  181. {
  182.     fprintf(stderr, "autologout: %s\n", message);
  183.     exit(status);
  184. }
  185. --------------------------- cut here ------------------------------
  186.  
  187.      It probably doesn't do the sort of file saves etc. that you're
  188. asking about.  That could be extremely difficult.  On the other hand,
  189. I've gotten so tired of people abandoning terminals around here that
  190. I tend to just cut them down without warning.  Maybe having to rebuild
  191. a file or two might teach them to be more careful.
  192.  
  193. Charlie_Gibbs@mindlink.bc.ca
  194. "I'm cursed with hair from HELL!"  -- Night Court
  195.  
  196.