home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / aix / 8976 < prev    next >
Encoding:
Text File  |  1992-08-21  |  3.6 KB  |  143 lines

  1. Newsgroups: comp.unix.aix
  2. Path: sparky!uunet!ddssuprs!lpc
  3. From: lpc@dickens.com (Luis P Caamano)
  4. Subject: Re: system() and SIGCHLD
  5. Message-ID: <1992Aug21.135407.1571@dickens.com>
  6. Date: Fri, 21 Aug 1992 13:54:07 GMT
  7. References: <94029@bu.edu>
  8. Organization: Dickens Data Systems, Inc.
  9. Lines: 132
  10.  
  11. In article <94029@bu.edu> tasos@cs.bu.edu (Anastasios Kotsikonas) writes:
  12. >A few weeks ago I posted the following question to comp.unix.programmer:
  13. >
  14. >If process A forks B, then A does a system(), is A supposed to receive
  15. >a SIGCLD when system() returns?
  16. >
  17.  
  18. I ran your program.  I got the bug on both AIX 3.1.5 and 3.2. However, 
  19. on an old 9021 AIX (3.1.0) it ran fine by changing the system line
  20. to
  21.     system ("sleep 4"); 
  22.  
  23. Anyway, I added a mysystem() function to your program.  mysystem()
  24. is doing what I think (I might be wrong ...) system() should do.
  25.  
  26. The man page for the system subroutine says that it checks for a
  27. trusted shell.  I don't know how to check from which shell I'm running
  28. other than traverse up the process tree (which is kind of expensive).
  29. I suspect that a simple way to find out if you're running under a trusted 
  30. shell might be part of the misterious priv.h functions (setpriv, getpriv, 
  31. privilege, privcheck, etc.)  Whatever, I added some pseudo code
  32. for that part since I don't think you're running the trusted shell.
  33.  
  34. >
  35. >If any can suggest a workaround I would really appreciate it.
  36. >
  37.  
  38. I don't know what you are doing with SIGCLHD, but you can always check
  39. the pid of the reaped child.
  40.  
  41. Another workaround is mysystem().
  42.  
  43. Here is systes1.c
  44.  
  45. ----CUT HERE-----8<------systest1.c----------8<--------------
  46.  
  47.  
  48. #include <stdio.h>
  49. #include <signal.h>
  50. #include <sys/types.h>
  51.  
  52.  
  53. int pid;
  54.  
  55. main ()
  56. {
  57.   extern int sighandle();
  58.  
  59. #ifdef SIGCLD
  60.   signal (SIGCLD, (void *) sighandle);
  61. #else
  62.   signal (SIGCHLD, (void *) sighandle);
  63. #endif
  64.   if ((pid = fork ()) < 0)
  65.     perror ("Cannot fork"),
  66.     exit (1);
  67.   else if (pid == 0)
  68.     while (1);
  69.   else
  70.     mysystem ("/bin/echo > /dev/null"); /**/
  71.   sleep (2);
  72.   printf ("system() OK.\n");
  73. #ifdef SIGCLD
  74.   signal (SIGCLD, SIG_DFL);
  75. #else
  76.   signal (SIGCHLD, SIG_DFL);
  77. #endif
  78.   kill (pid, SIGKILL);
  79.   exit (0);
  80. }
  81.  
  82. sighandle (sig)
  83. int sig;
  84. {
  85.   fprintf (stderr, "system() bug.\n");
  86. #ifdef SIGCLD
  87.   signal (SIGCLD, SIG_DFL);
  88. #else
  89.   signal (SIGCHLD, SIG_DFL);
  90. #endif
  91.   kill (pid, SIGKILL);
  92.   exit (2);
  93. }
  94.  
  95. mysystem(char *cmd)
  96. {
  97. int    status, rc;
  98. pid_t    pid;
  99. struct sigaction sa, oldi, oldq, oldc;
  100.  
  101.     printf("mysystem() function\n");
  102.  
  103.     sigemptyset(&sa.sa_mask);
  104.     sa.sa_flags = 0 ;
  105.  
  106.     sa.sa_handler = SIG_DFL;
  107.     sigaction(SIGCHLD,&sa, &oldc);
  108.  
  109.     sa.sa_handler = SIG_IGN;
  110.     sigaction(SIGINT,&sa, &oldi);
  111.     sigaction(SIGQUIT,&sa, &oldq);
  112.  
  113.     if((pid = fork()) == 0) {
  114. #ifdef KNOWABOUTTSH
  115.         if (someway_to_find_if_it_is_a_trusted_shell) {
  116.         (void) execl("/bin/tsh", "tsh", "-c", cmd, 0);
  117.         _exit(-1);
  118.         }
  119. #endif
  120.         (void) execl("/bin/bsh", "bsh", "-c", cmd, 0);
  121.         _exit(-1);
  122.     }
  123.  
  124.  
  125.     while((rc = waitpid(pid, &status, 0)) != pid && rc != -1)
  126.         ;
  127.  
  128.     sigaction(SIGCHLD,&oldc,NULL);
  129.     sigaction(SIGINT,&oldi,NULL);
  130.     sigaction(SIGQUIT,&oldq,NULL);
  131.  
  132.     return(rc == -1 ? rc : status);
  133. }
  134. ----CUT HERE-----8<------systest1.c----------8<--------------
  135. -- 
  136. ---------------------------------------------------------------------------
  137. Luis P. Caamano                    |         lpc@dickens.com
  138. Dickens Data Systems, Inc.         |         uunet!dickens.com!lpc
  139. Atlanta, GA                        |         (404) 475-8860
  140. ---------------------------------------------------------------------------
  141. If I think I know it all, I'll stop learning. -myself
  142. The more I learn, the more I know I know nothing. -somebody else
  143.