@@ -153,39 +153,32 @@ lws_spawn_piped_destroy(struct lws_spawn_piped **_lsp)
153153int
154154lws_spawn_reap (struct lws_spawn_piped * lsp )
155155{
156- long hz = sysconf (_SC_CLK_TCK ); /* accounting Hz */
157156 void * opaque = lsp -> info .opaque ;
158157 lsp_cb_t cb = lsp -> info .reap_cb ;
159- struct lws_spawn_piped temp ;
160- struct tms tms ;
161- #if defined(__OpenBSD__ ) || defined(__NetBSD__ )
162- struct rusage rusa ;
163- int status ;
164- #endif
165- int n ;
158+ lws_spawn_resource_us_t res ;
159+ struct rusage ru ;
160+ siginfo_t si ;
161+ int n , status ;
166162
167163 if (lsp -> child_pid < 1 )
168164 return 0 ;
169165
170166 /* check if exited, do not reap yet */
171167
172168 memset (& lsp -> si , 0 , sizeof (lsp -> si ));
173- #if defined(__OpenBSD__ ) || defined(__NetBSD__ )
174- n = wait4 (lsp -> child_pid , & status , WNOHANG , & rusa );
175- if (!n )
176- return 0 ;
177- lsp -> si .si_code = WIFEXITED (status );
178- #else
179- n = waitid (P_PID , (id_t )lsp -> child_pid , & lsp -> si , WEXITED | WNOHANG | WNOWAIT );
180- #endif
169+ n = wait4 (lsp -> child_pid , & status , WNOHANG , & ru );
181170 if (n < 0 ) {
182- lwsl_info ("%s: child %d still running\n" , __func__ , lsp -> child_pid );
171+ lwsl_info ("%s: child %d still running (errno %d)\n" , __func__ ,
172+ lsp -> child_pid , errno );
183173 return 0 ;
184174 }
185175
186- if (!lsp -> si . si_code )
176+ if (!n )
187177 return 0 ;
188178
179+ lsp -> si .si_code = WIFEXITED (status );
180+ lsp -> si .si_status = WEXITSTATUS (status );
181+
189182 /* his process has exited... */
190183
191184 if (!lsp -> reaped ) {
@@ -239,33 +232,37 @@ lws_spawn_reap(struct lws_spawn_piped *lsp)
239232 * Collect the final information and then reap the dead process
240233 */
241234
242- if (times (& tms ) != (clock_t ) - 1 ) {
243- /*
244- * Cpu accounting in us
245- */
246- lsp -> accounting [0 ] = (lws_usec_t )((uint64_t )tms .tms_cstime * 1000000 ) / hz ;
247- lsp -> accounting [1 ] = (lws_usec_t )((uint64_t )tms .tms_cutime * 1000000 ) / hz ;
248- lsp -> accounting [2 ] = (lws_usec_t )((uint64_t )tms .tms_stime * 1000000 ) / hz ;
249- lsp -> accounting [3 ] = (lws_usec_t )((uint64_t )tms .tms_utime * 1000000 ) / hz ;
235+ lsp -> res .us_cpu_user =
236+ ((uint64_t )ru .ru_utime .tv_sec * 1000000 ) + (uint64_t )ru .ru_utime .tv_usec ;
237+ lsp -> res .us_cpu_sys =
238+ ((uint64_t )ru .ru_stime .tv_sec * 1000000 ) + (uint64_t )ru .ru_stime .tv_usec ;
239+
240+ /* ru_maxrss is in KB */
241+ lsp -> res .peak_mem_rss = (uint64_t )ru .ru_maxrss * 1024 ;
242+
243+ if (getrusage (RUSAGE_CHILDREN , & ru ) == 0 ) {
244+ lsp -> res .us_cpu_user +=
245+ ((uint64_t )ru .ru_utime .tv_sec * 1000000 ) + (uint64_t )ru .ru_utime .tv_usec ;
246+ lsp -> res .us_cpu_sys +=
247+ ((uint64_t )ru .ru_stime .tv_sec * 1000000 ) + (uint64_t )ru .ru_stime .tv_usec ;
248+ /* ru_maxrss is in KB */
249+ lsp -> res .peak_mem_rss += (uint64_t )ru .ru_maxrss * 1024 ;
250+ } else
251+ lwsl_err ("%s: getrusage failed\n" , __func__ );
252+
253+ n = waitpid (lsp -> child_pid , & status , WNOHANG );
254+ if (n < 0 ) {
255+ lwsl_info ("%s: child %d vanished\n" , __func__ , lsp -> child_pid );
250256 }
251257
252- temp = * lsp ;
253- #if defined(__OpenBSD__ ) || defined(__NetBSD__ )
254- n = wait4 (lsp -> child_pid , & status , WNOHANG , & rusa );
255- if (!n )
256- return 0 ;
257- lsp -> si .si_code = WIFEXITED (status );
258- if (lsp -> si .si_code == CLD_EXITED )
259- temp .si .si_code = CLD_EXITED ;
260- temp .si .si_status = WEXITSTATUS (status );
261- #else
262- n = waitid (P_PID , (id_t )lsp -> child_pid , & temp .si , WEXITED | WNOHANG );
263- #endif
264- temp .si .si_status &= 0xff ; /* we use b8 + for flags */
265- lwsl_warn ("%s: waitd says %d, process exit %d\n" ,
266- __func__ , n , temp .si .si_status );
258+ lwsl_info ("%s: waitd says %d, process exit %d\n" ,
259+ __func__ , n , lsp -> si .si_status );
267260
268- lsp -> child_pid = -1 ;
261+ lsp -> child_pid = -1 ;
262+ si = lsp -> si ;
263+ res = lsp -> res ;
264+ n = lsp -> we_killed_him_timeout |
265+ (lsp -> we_killed_him_spew << 1 );
269266
270267 /* destroy the lsp itself first (it's freed and plsp set NULL */
271268
@@ -275,9 +272,7 @@ lws_spawn_reap(struct lws_spawn_piped *lsp)
275272 /* then do the parent callback informing it's destroyed */
276273
277274 if (cb )
278- cb (opaque , temp .accounting , & temp .si ,
279- temp .we_killed_him_timeout |
280- (temp .we_killed_him_spew << 1 ));
275+ cb (opaque , & res , & si , n );
281276
282277 return 1 ; /* was reaped */
283278}
@@ -559,7 +554,6 @@ lws_spawn_piped(const struct lws_spawn_piped_info *i)
559554 close (cfd );
560555 }
561556
562-
563557 lwsl_info ("%s: created cgroup %s\n" , __func__ , lsp -> cgroup_path );
564558 lws_snprintf (pth , sizeof (pth ), "%s/pids.max" , lsp -> cgroup_path );
565559 cfd = lws_open (pth , LWS_O_WRONLY );
@@ -821,7 +815,7 @@ lws_spawn_prepare_self_cgroup(const char *user, const char *group)
821815 fd = lws_open (path , LWS_O_WRONLY );
822816 if (fd < 0 ) {
823817 /* May fail if user doesn't own the file, that's okay */
824- lwsl_info ("%s: cannot open subtree_control: %s\n" ,
818+ lwsl_notice ("%s: cannot open subtree_control: %s\n" ,
825819 __func__ , strerror (errno ));
826820 return 0 ; /* Still a success if dir exists */
827821 }
@@ -852,24 +846,49 @@ lws_spawn_prepare_self_cgroup(const char *user, const char *group)
852846 lwsl_warn ("%s: group '%s' not found\n" , __func__ , group );
853847 }
854848
855- lwsl_notice ("%s: switching %s to %d:%d\n" ,
856- __func__ , path , uid , gid );
857-
858- if (chown (path , uid , gid ) < 0 )
859- lwsl_warn ("%s: failed to chown %s: %s\n" ,
860- __func__ , path , strerror (errno ));
861- /* 2. ALSO change ownership of the critical control files inside it */
862- lws_snprintf (path , sizeof (path ), "/sys/fs/cgroup%s/cgroup.procs" , self_cgroup );
863- if (chown (path , uid , gid ) < 0 )
864- lwsl_warn ("%s: failed to chown %s: %s\n" ,
865- __func__ , path , strerror (errno ));
866-
867- lws_snprintf (path , sizeof (path ), "/sys/fs/cgroup%s/cgroup.subtree_control" , self_cgroup );
868- if (chown (path , uid , gid ) < 0 )
869- lwsl_warn ("%s: failed to chown %s: %s\n" ,
870- __func__ , path , strerror (errno ));
871-
872- lwsl_notice ("%s: lws cgroup parent configured\n" , __func__ );
849+ if (uid != (uid_t )- 1 || gid != (gid_t )- 1 ) {
850+
851+ lwsl_notice ("%s: switching %s to %d:%d\n" ,
852+ __func__ , path , uid , gid );
853+
854+ if (chown (path , uid , gid ) < 0 )
855+ lwsl_warn ("%s: failed to chown %s: %s\n" ,
856+ __func__ , path , strerror (errno ));
857+ /* 2. ALSO change ownership of the critical control files inside it */
858+ lws_snprintf (path , sizeof (path ), "/sys/fs/cgroup%s/cgroup.procs" , self_cgroup );
859+ if (chown (path , uid , gid ) < 0 )
860+ lwsl_warn ("%s: failed to chown %s: %s\n" ,
861+ __func__ , path , strerror (errno ));
862+
863+ lws_snprintf (path , sizeof (path ), "/sys/fs/cgroup%s/cgroup.subtree_control" , self_cgroup );
864+ if (chown (path , uid , gid ) < 0 )
865+ lwsl_warn ("%s: failed to chown %s: %s\n" ,
866+ __func__ , path , strerror (errno ));
867+ }
868+ lws_snprintf (path , sizeof (path ), "/sys/fs/cgroup%s/lws" , self_cgroup );
869+ mkdir (path , 0775 );
870+ if (uid != (uid_t )- 1 || gid != (gid_t )- 1 ) {
871+
872+ lwsl_notice ("%s: switching %s to %d:%d\n" ,
873+ __func__ , path , uid , gid );
874+
875+ if (chown (path , uid , gid ) < 0 )
876+ lwsl_warn ("%s: failed to chown %s: %s\n" ,
877+ __func__ , path , strerror (errno ));
878+ /* 2. ALSO change ownership of the critical control files inside it */
879+ lws_snprintf (path , sizeof (path ), "/sys/fs/cgroup%s/cgroup.procs" , self_cgroup );
880+ if (chown (path , uid , gid ) < 0 )
881+ lwsl_warn ("%s: failed to chown %s: %s\n" ,
882+ __func__ , path , strerror (errno ));
883+
884+ lws_snprintf (path , sizeof (path ), "/sys/fs/cgroup%s/cgroup.subtree_control" , self_cgroup );
885+ if (chown (path , uid , gid ) < 0 )
886+ lwsl_warn ("%s: failed to chown %s: %s\n" ,
887+ __func__ , path , strerror (errno ));
888+ }
889+
890+
891+ lwsl_notice ("%s: lws cgroup parent configured: %s\n" , __func__ , path );
873892
874893 return 0 ;
875894#endif
0 commit comments