@@ -57,6 +57,7 @@ struct lws_stub_manager {
5757 struct lws_context * cx ;
5858 struct lws_vhost * vh ;
5959 char uds_path [256 ];
60+ char stub_name [128 ];
6061 char secret [129 ];
6162 struct lws_spawn_piped * lsp ;
6263 struct lws_stub_config config ;
@@ -92,7 +93,12 @@ lws_stub_spawn(const struct lws_stub_config *config)
9293 mgr -> cx = config -> cx ;
9394 mgr -> vh = config -> vh ;
9495 memcpy (& mgr -> config , config , sizeof (mgr -> config ));
95- lws_strncpy (mgr -> uds_path , config -> uds_path , sizeof (mgr -> uds_path ));
96+ if (config -> uds_path )
97+ lws_strncpy (mgr -> uds_path , config -> uds_path , sizeof (mgr -> uds_path ));
98+ mgr -> config .uds_path = mgr -> uds_path ;
99+ if (config -> stub_name )
100+ lws_strncpy (mgr -> stub_name , config -> stub_name , sizeof (mgr -> stub_name ));
101+ mgr -> config .stub_name = mgr -> stub_name ;
96102 mgr -> protocols = config -> protocols ;
97103
98104 /* Generate a secure 128-char secret */
@@ -147,7 +153,7 @@ lws_stub_spawn(const struct lws_stub_config *config)
147153#endif
148154
149155 mgr -> exec_array [n ++ ] = exe_path ;
150- lwsl_notice ( "%s: Spawning stub with exe: %s\n" , __func__ , exe_path );
156+ lwsl_vhost_notice ( mgr -> vh , "%s: Spawning stub '%s' with exe: %s\n" , __func__ , config -> stub_name , exe_path );
151157 /* Construct the stub argument dynamically */
152158 lws_snprintf (mgr -> stub_arg , sizeof (mgr -> stub_arg ), "--lws-stub=%s" , config -> stub_name );
153159 mgr -> exec_array [n ++ ] = mgr -> stub_arg ;
@@ -165,43 +171,43 @@ lws_stub_spawn(const struct lws_stub_config *config)
165171 if (stdin_fd ) {
166172 DWORD bw ;
167173 if (!WriteFile (stdin_fd , mgr -> secret , 128 , & bw , NULL )) {
168- lwsl_err ( "%s: Failed writing secret to pipe\n" , __func__ );
174+ lwsl_vhost_err ( mgr -> vh , "%s: stub '%s' failed writing secret to pipe\n" , __func__ , config -> stub_name );
169175 goto spawn_fail ;
170176 }
171177 if (config -> extra_payload && config -> extra_payload_len ) {
172178 if (!WriteFile (stdin_fd , config -> extra_payload , (DWORD )config -> extra_payload_len , & bw , NULL )) {
173- lwsl_err ( "%s: Failed writing extra payload to pipe\n" , __func__ );
179+ lwsl_vhost_err ( mgr -> vh , "%s: stub '%s' failed writing extra payload to pipe\n" , __func__ , config -> stub_name );
174180 goto spawn_fail ;
175181 }
176182 }
177183 } else {
178- lwsl_err ( "%s: No stdin pipe available\n" , __func__ );
184+ lwsl_vhost_err ( mgr -> vh , "%s: stub '%s' no stdin pipe available\n" , __func__ , config -> stub_name );
179185 goto spawn_fail ;
180186 }
181187#else
182188 if (stdin_fd >= 0 ) {
183189 if (write (stdin_fd , mgr -> secret , 128 ) < 0 ) {
184- lwsl_err ( "%s: Failed writing secret to pipe\n" , __func__ );
190+ lwsl_vhost_err ( mgr -> vh , "%s: stub '%s' failed writing secret to pipe\n" , __func__ , config -> stub_name );
185191 goto spawn_fail ;
186192 }
187193 if (config -> extra_payload && config -> extra_payload_len ) {
188194 if (write (stdin_fd , config -> extra_payload , (unsigned int )config -> extra_payload_len ) < 0 ) {
189- lwsl_err ( "%s: Failed writing extra payload to pipe\n" , __func__ );
195+ lwsl_vhost_err ( mgr -> vh , "%s: stub '%s' failed writing extra payload to pipe\n" , __func__ , config -> stub_name );
190196 goto spawn_fail ;
191197 }
192198 }
193199 } else {
194- lwsl_err ( "%s: No stdin pipe available\n" , __func__ );
200+ lwsl_vhost_err ( mgr -> vh , "%s: stub '%s' no stdin pipe available\n" , __func__ , config -> stub_name );
195201 goto spawn_fail ;
196202 }
197203#endif
198204 } else {
199- lwsl_vhost_err (mgr -> vh , "%s: Failed to spawn stub %s \n" , __func__ , config -> stub_name );
205+ lwsl_vhost_err (mgr -> vh , "%s: Failed to spawn stub '%s' \n" , __func__ , config -> stub_name );
200206 lws_free (mgr );
201207 return NULL ;
202208 }
203209
204- lwsl_vhost_notice (mgr -> vh , "%s: Spawned stub %s \n" , __func__ , config -> stub_name );
210+ lwsl_vhost_notice (mgr -> vh , "%s: Spawned stub '%s' \n" , __func__ , config -> stub_name );
205211
206212 if (!mgr -> sul .list .owner && !mgr -> wsi_client )
207213 lws_stub_client_connect (mgr );
@@ -210,7 +216,7 @@ lws_stub_spawn(const struct lws_stub_config *config)
210216
211217spawn_fail :
212218 lws_spawn_piped_kill_child_process (mgr -> lsp );
213- lwsl_vhost_err (mgr -> vh , "%s: Failed to initialize spawned stub %s \n" , __func__ , config -> stub_name );
219+ lwsl_vhost_err (mgr -> vh , "%s: Failed to initialize spawned stub '%s' \n" , __func__ , config -> stub_name );
214220 lws_free (mgr );
215221 return NULL ;
216222}
@@ -238,7 +244,7 @@ lws_stub_server_init(const struct lws_stub_config *config, char *secret_out, voi
238244 }
239245
240246 if (rx < 64 ) {
241- lwsl_err ("%s: Failed to read secret from stdin\n" , __func__ );
247+ lwsl_err ("%s: stub '%s': Failed to read secret from stdin\n" , __func__ , config -> stub_name ? config -> stub_name : "unknown" );
242248 return -1 ;
243249 }
244250 secret_out [128 ] = '\0' ;
@@ -249,7 +255,7 @@ lws_stub_server_init(const struct lws_stub_config *config, char *secret_out, voi
249255 * and unknown to the child, and the pipe remains open for future IPC. */
250256 ssize_t n = read (0 , (void * )extra_out , (unsigned int )extra_len );
251257 if (n < 0 ) {
252- lwsl_err ("%s: Failed to read extra payload\n" , __func__ );
258+ lwsl_err ("%s: stub '%s': Failed to read extra payload\n" , __func__ , config -> stub_name ? config -> stub_name : "unknown" );
253259 /* Non-fatal */
254260 }
255261 }
@@ -265,7 +271,7 @@ lws_stub_server_init(const struct lws_stub_config *config, char *secret_out, voi
265271 unlink (info .iface );
266272 vh_uds = lws_create_vhost (config -> cx , & info );
267273 if (!vh_uds ) {
268- lwsl_err ("%s: Failed to create UDS vhost\n" , __func__ );
274+ lwsl_err ("%s: stub '%s': Failed to create UDS vhost\n" , __func__ , config -> stub_name ? config -> stub_name : "unknown" );
269275 return -1 ;
270276 }
271277
@@ -295,6 +301,23 @@ static const lws_retry_bo_t stub_retry = {
295301static void
296302stub_retry_cb (lws_sorted_usec_list_t * sul );
297303
304+ static int
305+ lws_stub_child_is_alive (struct lws_stub_manager * mgr )
306+ {
307+ if (!mgr || !mgr -> lsp )
308+ return 0 ;
309+
310+ #if !defined(WIN32 )
311+ if (mgr -> lsp -> child_pid <= 0 )
312+ return 0 ;
313+ if (kill (mgr -> lsp -> child_pid , 0 ) == 0 || errno == EPERM )
314+ return 1 ;
315+ return 0 ;
316+ #else
317+ return 1 ;
318+ #endif
319+ }
320+
298321static int
299322lws_stub_client_connect (struct lws_stub_manager * mgr )
300323{
@@ -317,16 +340,22 @@ lws_stub_client_connect(struct lws_stub_manager *mgr)
317340 i .retry_and_idle_policy = & stub_retry ;
318341 i .method = "RAW" ; /* RAW connection */
319342
320- lwsl_vhost_notice (mgr -> vh , "protocol %s, addr %s\n" , i .protocol , i .address );
343+ lwsl_vhost_notice (mgr -> vh , "%s: stub '%s', protocol %s, addr %s\n" , __func__ , mgr -> config . stub_name , i .protocol , i .address );
321344 mgr -> wsi_client = lws_client_connect_via_info (& i );
322345 if (!mgr -> wsi_client ) {
323346 if (mgr -> ctry < 10 ) {
324347 uint32_t ms = stub_retry .retry_ms_table [
325348 mgr -> ctry < stub_retry .retry_ms_table_count ?
326349 mgr -> ctry : stub_retry .retry_ms_table_count - 1 ];
327350 mgr -> ctry ++ ;
328- if (mgr -> ctry > 1 )
329- lwsl_notice ("%s: Synchronous connect failed (errno %d), retrying in %u ms (attempt %d)\n" , __func__ , LWS_ERRNO , (unsigned int )ms , mgr -> ctry );
351+ if (mgr -> ctry > 1 ) {
352+ int alive = lws_stub_child_is_alive (mgr );
353+ lwsl_vhost_notice (mgr -> vh , "%s: stub '%s': Synchronous connect failed (errno %d), stub process %s (PID %d), retrying in %u ms (attempt %d)\n" ,
354+ __func__ , mgr -> config .stub_name , LWS_ERRNO ,
355+ alive ? "is alive" : "has DIED/DOES NOT EXIST" ,
356+ mgr -> lsp ? (int )mgr -> lsp -> child_pid : -1 ,
357+ (unsigned int )ms , mgr -> ctry );
358+ }
330359 lws_sul_schedule (mgr -> cx , 0 , & mgr -> sul , stub_retry_cb , ms * 1000 );
331360 }
332361
@@ -354,14 +383,19 @@ lws_callback_stub_client(struct lws *wsi, enum lws_callback_reasons reason,
354383 return 0 ;
355384
356385 switch (reason ) {
357- case LWS_CALLBACK_CLIENT_CONNECTION_ERROR :
358- lwsl_err ("%s: Client connection failed\n" , __func__ );
386+ case LWS_CALLBACK_CLIENT_CONNECTION_ERROR : {
387+ int alive = lws_stub_child_is_alive (mgr );
388+ lwsl_vhost_err (mgr -> vh , "%s: stub '%s': Client connection failed (stub process %s, PID %d)\n" ,
389+ __func__ , mgr -> config .stub_name ,
390+ alive ? "is alive" : "has DIED/DOES NOT EXIST" ,
391+ mgr -> lsp ? (int )mgr -> lsp -> child_pid : -1 );
359392 mgr -> wsi_client = NULL ;
360393 lws_retry_sul_schedule (mgr -> cx , 0 , & mgr -> sul , & stub_retry , stub_retry_cb , & mgr -> ctry );
361394 break ;
395+ }
362396
363397 case LWS_CALLBACK_RAW_CONNECTED :
364- lwsl_notice ( "%s: UDS connected to stub \n" , __func__ );
398+ lwsl_vhost_notice ( mgr -> vh , "%s: stub '%s': UDS connected \n" , __func__ , mgr -> config . stub_name );
365399 mgr -> ctry = 0 ; /* Reset retry counter on success */
366400 if (mgr -> config .connected_cb )
367401 mgr -> config .connected_cb (mgr );
@@ -409,7 +443,7 @@ lws_callback_stub_client(struct lws *wsi, enum lws_callback_reasons reason,
409443 if (req -> rx_cb ) {
410444 int m = lejp_parse (& req -> jctx , (uint8_t * )in , (int )len );
411445 if (m < 0 && m != LEJP_CONTINUE ) {
412- lwsl_err ( "%s: lejp parse failed: %d\n" , __func__ , m );
446+ lwsl_vhost_err ( mgr -> vh , "%s: stub '%s' lejp parse failed: %d\n" , __func__ , mgr -> config . stub_name , m );
413447 lws_dll2_remove (& req -> list );
414448 lws_free (req -> tx_buf );
415449 lejp_destruct (& req -> jctx );
@@ -509,4 +543,12 @@ lws_stub_destroy(struct lws_stub_manager **_mgr)
509543 lws_free (mgr );
510544 * _mgr = NULL ;
511545}
546+
547+ const char *
548+ lws_stub_get_secret (struct lws_stub_manager * mgr )
549+ {
550+ if (!mgr )
551+ return NULL ;
552+ return mgr -> secret ;
553+ }
512554#endif
0 commit comments