Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why wineserver setup master socket in a child process,when run in background?

When the Wine server launches, it creates a Unix socket through open_master_socket(),all Wine processes launched later connects to the Wine server using this socket,here is the code from server/request.c,open_master_socket():

 771     if (!foreground)
 772     {
 773         if (pipe( sync_pipe ) == -1) fatal_perror( "pipe" );
 774         pid = fork();
 775         switch( pid )
 776         {
 777         case 0:  /* child */
 778             setsid();
 779             close( sync_pipe[0] );
 780 
 781             acquire_lock();
 782 
 783             /* close stdin and stdout */
 784             dup2( fd, 0 );
 785             dup2( fd, 1 );
 786 
 787             /* signal parent */
 788             dummy = 0;
 789             write( sync_pipe[1], &dummy, 1 );
 790             close( sync_pipe[1] );
 791             break;
 792 
 793         case -1:
 794             fatal_perror( "fork" );
 795             break;
 796 
 797         default:  /* parent */
 798             close( sync_pipe[1] );
 799 
 800             /* wait for child to signal us and then exit */
 801             if (read( sync_pipe[0], &dummy, 1 ) == 1) _exit(0);
 802 
 803             /* child terminated, propagate exit status */
 804             wait4( pid, &status, 0, NULL );
 805             if (WIFEXITED(status)) _exit( WEXITSTATUS(status) );
 806             _exit(1);
 807         }
 808     }
 809     else  /* remain in the foreground */
 810     {
 811         acquire_lock();
 812     }

acquire_lock() is used to setup the master socket, Question is when run in background,why fork() and let the child process do the work,while the parent just wait and exit()? And why not when run in foreground ?

like image 780
hello.wjx Avatar asked Dec 05 '25 02:12

hello.wjx


1 Answers

"fork() and let the child process do the work" is "run in background".

The pipe is an idiom to let the parent stay around while the child does setup that could fail (in this case, acquiring the lock), and propagate the failure properly if it happens; otherwise, you could not easily find out that the child had failed, but would have to check for its existence or scan log files afterward to see if something had gone wrong.

like image 143
geekosaur Avatar answered Dec 06 '25 17:12

geekosaur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!