home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / misc / 4196 < prev    next >
Encoding:
Text File  |  1992-11-17  |  4.1 KB  |  143 lines

  1. Xref: sparky comp.unix.misc:4196 comp.unix.questions:13480 comp.sys.sgi:16494 comp.graphics:11941
  2. Path: sparky!uunet!mcsun!uknet!doc.ic.ac.uk!mrccrc!warwick!warwick!not-for-mail
  3. From: maupb@csv.warwick.ac.uk (Mr J L Saunders)
  4. Newsgroups: comp.unix.misc,comp.unix.questions,comp.sys.sgi,comp.graphics
  5. Subject: Re: Interactive vs Background users
  6. Message-ID: <1e8s6uINNc7g@violet.csv.warwick.ac.uk>
  7. Date: 16 Nov 92 19:18:54 GMT
  8. References: <1992Nov13.042252.11811@kurango.cit.gu.edu.au> <1992Nov15.143104.6301@discus.technion.ac.il> <BxsApH.4FH@news.cso.uiuc.edu>
  9. Organization: Computing Services, University of Warwick, UK
  10. Lines: 130
  11. NNTP-Posting-Host: violet.csv.warwick.ac.uk
  12.  
  13. In article <BxsApH.4FH@news.cso.uiuc.edu> ercolessi@uimrl3.mrl.uiuc.edu (furio ercolessi) writes:
  14.  
  15. >SGI users are particularly lucky to have the 'npri' command which
  16. >allows to define the time slice, which is the critical parameter.   
  17.  
  18. I noticed someone earlier in this thread suggested npri - unfortunately
  19. not everyone has it.
  20.  
  21. Here's a pseudo(ish)code version of a process that will turn it's child on and
  22. off depending on who's logged on (a lot of it's ripped out of one of my own
  23. programs - so flames about readability, incompatible pointer types and the
  24. like to /dev/null) ...
  25.  
  26. <daemonname> whatever command you want to execute
  27.  
  28. [More comments at the end]
  29.  
  30. ---cut here---
  31.  
  32. #include <stdio.h>
  33. #include <utmp.h>
  34. #include <rpcsvc/rusers.h>
  35. #include <signal.h>
  36.  
  37. char user[8];
  38. int child_process_number;
  39.  
  40. main(int argc, char **argv) {
  41.  
  42. getenv("USER",user);
  43.  
  44.  
  45. if (!(child_process_number=fork()) {
  46.   execvp(*argv[1],argv[1]);
  47.   fprintf(stderr,"demon: Cannot exec process\n"); /* Only gets here is exec
  48.                             fails */
  49.   kill(getppid(),1); /* Kill the parent process */
  50. }
  51.  
  52. signal(SIGHUP,SIG_IGN);
  53. signal(SIGCHLD,childgone);
  54.  
  55. while (1) {
  56.   if (!consolefree()) {
  57.     kill(child_process_number,SIGSTOP); /* Stop the child */
  58.     while (!consolefree()) sleep(30); /* Check console every 30 seconds */
  59.   }
  60.   kill(child_process_number,SIGCONT); /* Cont the child */
  61.   sleep(30); /* Wait another 30 seconds */
  62. } /* while */
  63.  
  64. /* Alternatively, if you're concerned about anyone using a tty it's simpler..
  65.  
  66. int numer_of_users;
  67. while (1) {
  68. if (rnusers("localhost")>0) { /* 1 or more users */
  69.   stop the child etc...
  70. }
  71.  
  72. */
  73.  
  74. /* Or we may just want to stop 9 to 5, then we'd need to have
  75. #include <time.h>
  76. time_t TIME;
  77. int hour;
  78.  
  79. while(1) {
  80. time(&TIME);
  81. hour=localtime(&TIME)->tm_hour;
  82. if ((hour>=9)|&&|(hour<17)) {
  83.   kill(child_process_number,SIGSTOP);
  84.   while ((hour>=9)&&(hour<17)) {
  85.     sleep(60);     /* Check every 60 seconds - gosh we're keen */
  86.     time(&TIME);
  87.     hour=localtime(&TIME)->tm_hour;
  88.   }
  89. sleep(60);
  90. }
  91. */
  92.  
  93.  
  94. } /* main */
  95.  
  96.  
  97. int consolefree() {
  98.  
  99.   struct utmpidlearr users;
  100.     bzero((char *)&users,sizeof(users)); /* Zero it out otherwise rusers()
  101.                         might complain! */
  102.       rusers("localhost", &users); /* Shove utmp information into users */
  103.  
  104.         if (strcmp(users.uia_arr[0]->ui_utmp.ut_line,"console")) {
  105.           fprintf(stderr,"demon: Console clear on %s. Continuing...\n",host);
  106.           return 1;
  107.         } /* Noone on console */
  108.  
  109.         if (!strcmp(users.uia_arr[0]->ui_utmp.ut_name,user)) 
  110.       fprintf(stderr,"demon: Console clear on %s. Continuing...\n",host);
  111.           return 1;
  112.     } /* User is on console */
  113.  
  114.     fprintf(stderr,"demon: Someone else is on %s console! waiting...\n",host);
  115.  
  116.     return 0; /* Someone else on console */
  117. }
  118.  
  119. childgone() {
  120. fprintf(stderr,"demon: Your program has exited\n");
  121. exit(0);
  122. }
  123.  
  124. ---cut here---
  125.  
  126. Of course, there's loads of nested comments, which you need to strip and
  127. you'll need to remove whatever code you don't need. I'm assuming that console
  128. will be the first entry in utmp. If this isn't so on your system then you can
  129. easily check each member of users.uia_arr[]
  130.  
  131. I'm not sure about whether my signal(SIGCHLD,childgone) will do as it's
  132. supposed - if it doesn't I'd be glad if someone would email me the correct
  133. thing (does a child signal the parent when it dies, for instance?).
  134.  
  135.  
  136. Hope this does the trick!
  137.  
  138. Jason
  139. -- 
  140.                        Jason L Saunders [ RouE ]      
  141.                      email: maupb@csv.warwick.ac.uk
  142. snail: Warwick Business School, University of Warwick, Coventry CV4 7AL, UK
  143.