Changeset 24821 in vbox for trunk/src/VBox/Main
- Timestamp:
- Nov 20, 2009 1:33:26 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/xpcom/server.cpp
r24820 r24821 53 53 54 54 #include <stdio.h> 55 56 // for the signal handler 57 #include <signal.h> 55 #include <signal.h> // for the signal handler 58 56 #include <stdlib.h> 59 57 #include <unistd.h> 60 58 #include <errno.h> 61 59 #include <getopt.h> 62 60 #include <sys/fcntl.h> 63 61 #ifndef RT_OS_OS2 64 62 # include <sys/resource.h> … … 748 746 { 749 747 { "automate", no_argument, NULL, 'a' }, 750 #if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)751 748 { "auto-shutdown", no_argument, NULL, 'A' }, 752 #endif753 749 { "daemonize", no_argument, NULL, 'd' }, 754 750 { "pidfile", required_argument, NULL, 'p' }, 755 #if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)756 751 { "pipe", required_argument, NULL, 'P' }, 757 #endif758 752 { NULL, 0, NULL, 0 } 759 753 }; 760 int c; 761 754 int c; 762 755 bool fDaemonize = false; 763 #ifndef RT_OS_OS2 764 static int daemon_pipe_fds[2] = {-1, -1}; 765 #endif 756 int daemon_pipe_wr = -1; 766 757 767 758 for (;;) … … 782 773 } 783 774 784 # if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)785 775 /* Used together with '-P', see below. Internal use only. */ 786 776 case 'A': … … 789 779 break; 790 780 } 791 #endif792 781 793 782 case 'd': … … 803 792 } 804 793 805 # if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)806 794 /* we need to exec on darwin, this is just an internal 807 795 * hack for passing the pipe fd along to the final child. */ 808 796 case 'P': 809 797 { 810 daemon_pipe_ fds[1]= atoi(optarg);798 daemon_pipe_wr = atoi(optarg); 811 799 break; 812 800 } 813 #endif814 801 815 802 default: … … 834 821 { 835 822 /* create a pipe for communication between child and parent */ 823 int daemon_pipe_fds[2] = {-1, -1}; 836 824 if (pipe(daemon_pipe_fds) < 0) 837 825 { … … 839 827 return 1; 840 828 } 829 daemon_pipe_wr = daemon_pipe_fds[1]; 830 int daemon_pipe_rd = daemon_pipe_fds[0]; 841 831 842 832 pid_t childpid = fork(); … … 853 843 854 844 /* close the writing end of the pipe */ 855 close(daemon_pipe_ fds[1]);845 close(daemon_pipe_wr); 856 846 857 847 /* try to read a message from the pipe */ 858 char msg[10] = {0}; /* initialize so it's NULL terminated */ 859 if (read(daemon_pipe_fds[0], msg, sizeof(msg)) > 0) 848 char msg[10 + 1]; 849 RT_ZERO(msg); /* initialize so it's NULL terminated */ 850 if (read(daemon_pipe_rd, msg, sizeof(msg) - 1) > 0) 860 851 { 861 852 if (strcmp(msg, "READY") == 0) 862 853 fSuccess = true; 863 854 else 864 printf ("ERROR: Unknown message from child " 865 "process (%s)\n", msg); 855 printf ("ERROR: Unknown message from child process (%s)\n", msg); 866 856 } 867 857 else … … 869 859 870 860 /* close the reading end of the pipe as well and exit */ 871 close(daemon_pipe_ fds[0]);861 close(daemon_pipe_rd); 872 862 return fSuccess ? 0 : 1; 873 863 } … … 898 888 } 899 889 900 /* Redirect standard i/o streams to /dev/null */ 901 if (daemon_pipe_fds[0] > 2) 902 { 903 freopen ("/dev/null", "r", stdin); 904 freopen ("/dev/null", "w", stdout); 905 freopen ("/dev/null", "w", stderr); 906 } 907 908 /* close the reading end of the pipe */ 909 close(daemon_pipe_fds[0]); 910 911 # if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD) 890 /* Close all file handles except for the write end of the pipe. */ 891 int fdMax; 892 struct rlimit lim; 893 if (getrlimit(RLIMIT_NOFILE, &lim) == 0) 894 fdMax = (int)RT_MIN(lim.rlim_cur, 65535); /* paranoia */ 895 else 896 fdMax = 1024; 897 for (int fd = 0; fd < fdMax; fd++) 898 if (fd != daemon_pipe_wr) 899 close(fd); 900 901 /* Make sure the pipe isn't any of the standard handles. */ 902 if (daemon_pipe_wr <= 2) 903 { 904 if (dup2(daemon_pipe_wr, 3) == 3) 905 { 906 close(daemon_pipe_wr); 907 daemon_pipe_wr = 3; 908 } 909 } 910 911 /* Redirect the standard handles to NULL by opening /dev/null three times. */ 912 open("/dev/null", O_RDWR, 0); 913 open("/dev/null", O_RDWR, 0); 914 open("/dev/null", O_RDWR, 0); 915 912 916 /* 913 917 * On leopard we're no longer allowed to use some of the core API's … … 921 925 * a frontend (debugger and strace don't contain any useful info). 922 926 */ 923 const char *apszArgs[7 ];927 const char *apszArgs[7 + 2]; 924 928 unsigned i = 0; 925 929 apszArgs[i++] = argv[0]; 926 930 apszArgs[i++] = "--pipe"; 927 931 char szPipeArg[32]; 928 RTStrPrintf (szPipeArg, sizeof (szPipeArg), "%d", daemon_pipe_ fds[1]);932 RTStrPrintf (szPipeArg, sizeof (szPipeArg), "%d", daemon_pipe_wr); 929 933 apszArgs[i++] = szPipeArg; 930 934 if (pszPidFile) … … 938 942 execv (apszArgs[0], (char * const *)apszArgs); 939 943 exit (126); 940 # endif941 944 } 942 945 … … 1046 1049 } 1047 1050 1048 #ifndef RT_OS_OS2 1049 if (daemon_pipe_fds[1] >= 0) 1051 if (daemon_pipe_wr >= 0) 1050 1052 { 1051 1053 printf ("\nStarting event loop....\n[send TERM signal to quit]\n"); 1052 1054 /* now we're ready, signal the parent process */ 1053 write(daemon_pipe_ fds[1], "READY", strlen("READY"));1055 write(daemon_pipe_wr, "READY", strlen("READY")); 1054 1056 } 1055 1057 else 1056 #endif1057 1058 { 1058 1059 printf ("\nStarting event loop....\n[press Ctrl-C to quit]\n"); … … 1133 1134 } 1134 1135 1135 #ifndef RT_OS_OS2 1136 if (daemon_pipe_fds[1] >= 0) 1136 if (daemon_pipe_wr >= 0) 1137 1137 { 1138 1138 /* close writing end of the pipe as well */ 1139 close(daemon_pipe_fds[1]); 1140 } 1141 #endif 1139 close(daemon_pipe_wr); 1140 } 1142 1141 1143 1142 return 0;
Note:
See TracChangeset
for help on using the changeset viewer.