1 From 755be8bdb48d2536e39d2d7cf84e8a8f86b8776f Mon Sep 17 00:00:00 2001
2 From: Robert McMahon <rjmcmahon@rjmcmahon.com>
3 Date: Sat, 6 Oct 2018 13:36:52 -0700
4 Subject: [PATCH] cleanup main startup, fix daemon mode per redirecting stdin,
5 stderr and stdout to /dev/null
11 @@ -167,67 +167,7 @@ int main( int argc, char **argv ) {
12 Settings_ParseCommandLine( argc, argv, ext_gSettings );
14 // Check for either having specified client or server
15 - if ( ext_gSettings->mThreadMode == kMode_Client
16 - || ext_gSettings->mThreadMode == kMode_Listener ) {
18 - // Start the server as a daemon
19 - if ( isDaemon( ext_gSettings )) {
20 - if (ext_gSettings->mThreadMode == kMode_Listener) {
21 - CmdInstallService(argc, argv);
23 - fprintf(stderr, "Client cannot be run as a daemon\n");
28 - // Remove the Windows service if requested
29 - if ( isRemoveService( ext_gSettings ) ) {
30 - // remove the service
31 - if ( CmdRemoveService() ) {
32 - fprintf(stderr, "IPerf Service is removed.\n");
37 - if ( isDaemon( ext_gSettings ) ) {
38 - if (ext_gSettings->mThreadMode != kMode_Listener) {
39 - fprintf(stderr, "Iperf client cannot be run as a daemon\n");
42 - if (daemon(1, 1) < 0) {
45 - fprintf( stderr, "Running Iperf Server as a daemon\n");
46 - fprintf( stderr, "The Iperf daemon process ID : %d\n",((int)getpid()));
52 - // initialize client(s)
53 - if ( ext_gSettings->mThreadMode == kMode_Client ) {
54 - client_init( ext_gSettings );
58 - // start up the reporter and client(s) or listener
60 - thread_Settings *into = NULL;
61 - // Create the settings structure for the reporter thread
62 - Settings_Copy( ext_gSettings, &into );
63 - into->mThreadMode = kMode_Reporter;
65 - // Have the reporter launch the client or listener
66 - into->runNow = ext_gSettings;
68 - // Start all the threads that are ready to go
69 - thread_start( into );
72 - // No need to make a reporter thread because we don't have threads
73 - thread_start( ext_gSettings );
76 + if ((ext_gSettings->mThreadMode != kMode_Client) && (ext_gSettings->mThreadMode != kMode_Listener)) {
77 // neither server nor client mode was specified
78 // print usage and exit
80 @@ -236,20 +176,75 @@ int main( int argc, char **argv ) {
81 // Starting in 2.0 to restart a previously defined service
82 // you must call iperf with "iperf -D" or using the environment variable
83 SERVICE_TABLE_ENTRY dispatchTable[] =
85 - { (LPSTR)TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)service_main},
89 + { (LPSTR)TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)service_main},
93 // starting the service by SCM, there is no arguments will be passed in.
94 // the arguments will pass into Service_Main entry.
95 if (!StartServiceCtrlDispatcher(dispatchTable) )
96 // If the service failed to start then print usage
98 - fprintf( stderr, usage_short, argv[0], argv[0] );
99 + fprintf( stderr, usage_short, argv[0], argv[0] );
104 + switch (ext_gSettings->mThreadMode) {
105 + case kMode_Client :
106 + if ( isDaemon( ext_gSettings ) ) {
107 + fprintf(stderr, "Iperf client cannot be run as a daemon\n");
110 + // initialize client(s)
111 + client_init( ext_gSettings );
113 + case kMode_Listener :
114 + if ( isDaemon( ext_gSettings ) ) {
115 + fprintf( stderr, "Running Iperf Server as a daemon\n");
116 + // Start the server as a daemon
118 + CmdInstallService(argc, argv);
119 + // Remove the Windows service if requested
120 + if ( isRemoveService( ext_gSettings ) ) {
121 + // remove the service
122 + if ( CmdRemoveService() ) {
123 + fprintf(stderr, "IPerf Service is removed.\n");
129 + // redirect stdin, stdout and sterr to /dev/null (see dameon and no close flag)
130 + if (daemon(1, 0) < 0) {
137 + fprintf( stderr, "unknown mode");
141 + // start up the reporter and client(s) or listener
143 + thread_Settings *into = NULL;
144 + // Create the settings structure for the reporter thread
145 + Settings_Copy( ext_gSettings, &into );
146 + into->mThreadMode = kMode_Reporter;
148 + // Have the reporter launch the client or listener
149 + into->runNow = ext_gSettings;
152 + // Start all the threads that are ready to go
153 + thread_start( into );
156 + // No need to make a reporter thread because we don't have threads
157 + thread_start( ext_gSettings );
160 // wait for other (client, server) threads to complete