wkt@csadfa.cs.adfa.oz.au (Warren Toomey) (06/05/91)
After recompling a program under SunOS 4.1.1 I found that it doesn't run the same way any more. I understand that 4.1.1 now has POSIX job control, but I can't see why this would affect non-POSIX job control code. A small program that reproduces the `bug' is included below. It runs under Pyramid OSx and Ultrix, but not SunOS 4.1.1. I'd be very grateful for any ideas, especially if it's a problem with my code. For those who have already seen this in another newsgroup, sorry, the reponse I got was NULL. Feel free to flame as much as you like :-) Warren Toomey wkt@csadfa.cs.adfa.oz.au #!/bin/sh echo x - fred.c sed '/^X/s///' > fred.c << '/' X#include <signal.h> X#include <sgtty.h> X#include <termio.h> /* Need this for TCGETA definition */ X X X/* The following program exhibits a possible bug in SunOS 4.1.1. Compile X * this program as fred: cc -o fred fred.c and run it with no arguments. X * X * The program sets the terminal to stop background writes to the terminal, X * and forks a child. The child moves itself to a new process group, and X * tries to write to the terminal in one of two ways: just after the fork(), X * and after the exec(). X * X * The parent waits for the SIGCHLD caused by the write, and attempts to X * bring the child back into the parent's pgrp, thus allowing the write to X * succeed. X * X * However, this fails under SunOS 4.1.1 when the child only tries to write X * AFTER the exec(). See `THE BUG' comment below. X */ X Xvoid catchsig() X { } X Xvoid dowrite() X { write(1,"Writing to stdout\n",18); exit(0); } X X Xmain(argc,argv) X int argc; X char *argv[]; X { X struct termio tbuf; X int i; X X if (argc>1) dowrite(); /* If we are called with >1 args, write */ X X/* Set up the terminal so that SIGTTOUs are sent to X * processes which are not members of the owning process group X */ X if (ioctl(0,TCGETA,&tbuf)) perror("ioctl in settou"); X tbuf.c_lflag |= TOSTOP; X if (ioctl(0,TCSETA,&tbuf)) perror("ioctl s"); X X X/* Catch the SIGCHLD with a null handler. X */ X signal(SIGCHLD,catchsig); X X/* Fork, and make the child write on stdout. X */ X switch(i=fork()) X { X case -1: printf("Could not fork\n"); exit(1); X X case 0 : /* The child */ X X i=setpgrp(0,getpid()); /* Take us out of the pgrp */ X sleep(2); /* Wait for a bit */ X X/* THE BUG: X * X * If you uncomment the printf line, the program works everywhere. X * But if you leave the line commented, the program dies with an X * EPERM error in the parent's setpgrp() under SunOS Release 4.1.1. X * It works under Pyramid OSx, generic 4.3BSD system. X */ X /* printf("Can I write??\n"); */ X execlp("fred","arg1","arg2",0); X exit(0); X X default: /* The parent */ X X printf("Forked, now we pause()\n"); X pause(); /* Wait for SIGCHLD */ X if (setpgrp(i,getpgrp(0))==-1) /* set pgrp to the parents's */ X { perror("Setpgrp failed"); exit(1); } X kill(i,SIGCONT); /* Start the child up */ X sleep(1); /* and finish */ X exit(0); X } X } /