brcm63xx: fixup broken patch after kernel bump
[openwrt/staging/lynxis.git] / package / network / utils / iperf / patches / 0002-cleanup-main-startup-fix-daemon-mode-per-redirecting.patch
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
6
7 ---
8
9 --- a/src/main.cpp
10 +++ b/src/main.cpp
11 @@ -167,67 +167,7 @@ int main( int argc, char **argv ) {
12 Settings_ParseCommandLine( argc, argv, ext_gSettings );
13
14 // Check for either having specified client or server
15 - if ( ext_gSettings->mThreadMode == kMode_Client
16 - || ext_gSettings->mThreadMode == kMode_Listener ) {
17 -#ifdef WIN32
18 - // Start the server as a daemon
19 - if ( isDaemon( ext_gSettings )) {
20 - if (ext_gSettings->mThreadMode == kMode_Listener) {
21 - CmdInstallService(argc, argv);
22 - } else {
23 - fprintf(stderr, "Client cannot be run as a daemon\n");
24 - }
25 - return 0;
26 - }
27 -
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");
33 - return 0;
34 - }
35 - }
36 -#else
37 - if ( isDaemon( ext_gSettings ) ) {
38 - if (ext_gSettings->mThreadMode != kMode_Listener) {
39 - fprintf(stderr, "Iperf client cannot be run as a daemon\n");
40 - return 0;
41 - }
42 - if (daemon(1, 1) < 0) {
43 - perror("daemon");
44 - }
45 - fprintf( stderr, "Running Iperf Server as a daemon\n");
46 - fprintf( stderr, "The Iperf daemon process ID : %d\n",((int)getpid()));
47 - fclose(stdout);
48 - fclose(stderr);
49 - fclose(stdin);
50 - }
51 -#endif
52 - // initialize client(s)
53 - if ( ext_gSettings->mThreadMode == kMode_Client ) {
54 - client_init( ext_gSettings );
55 - }
56 -
57 -#ifdef HAVE_THREAD
58 - // start up the reporter and client(s) or listener
59 - {
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;
64 -
65 - // Have the reporter launch the client or listener
66 - into->runNow = ext_gSettings;
67 -
68 - // Start all the threads that are ready to go
69 - thread_start( into );
70 - }
71 -#else
72 - // No need to make a reporter thread because we don't have threads
73 - thread_start( ext_gSettings );
74 -#endif
75 - } else {
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
79
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[] =
84 - {
85 - { (LPSTR)TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)service_main},
86 - { NULL, NULL}
87 - };
88 + {
89 + { (LPSTR)TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)service_main},
90 + { NULL, NULL}
91 + };
92
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
97 #endif
98 - fprintf( stderr, usage_short, argv[0], argv[0] );
99 + fprintf( stderr, usage_short, argv[0], argv[0] );
100 + return 0;
101 + }
102 +
103 +
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");
108 + return 0;
109 + }
110 + // initialize client(s)
111 + client_init( ext_gSettings );
112 + break;
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
117 +#ifdef WIN32
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");
124 + return 0;
125 + }
126 + }
127 +#else
128 + fflush(stderr);
129 + // redirect stdin, stdout and sterr to /dev/null (see dameon and no close flag)
130 + if (daemon(1, 0) < 0) {
131 + perror("daemon");
132 + }
133 + }
134 +#endif
135 + break;
136 + default :
137 + fprintf( stderr, "unknown mode");
138 + break;
139 + }
140 +#ifdef HAVE_THREAD
141 + // start up the reporter and client(s) or listener
142 + {
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;
147 +
148 + // Have the reporter launch the client or listener
149 + into->runNow = ext_gSettings;
150
151 - return 0;
152 + // Start all the threads that are ready to go
153 + thread_start( into );
154 }
155 +#else
156 + // No need to make a reporter thread because we don't have threads
157 + thread_start( ext_gSettings );
158 +#endif
159
160 // wait for other (client, server) threads to complete
161 thread_joinall();