@@ -364,6 +364,40 @@ static int socket_bindlisten(const char *socket_path,
364
364
return -1 ;
365
365
}
366
366
367
+ static void remove_pidfile (const char * pidfile )
368
+ {
369
+ if (unlink (pidfile ) != 0 ) {
370
+ ERRORF ("Failed to remove pidfile: \"%s\": %s" , pidfile , strerror (errno ));
371
+ }
372
+ }
373
+
374
+ static int create_pidfile (const char * pidfile )
375
+ {
376
+ int flags = O_WRONLY | O_CREAT | O_EXLOCK | O_TRUNC | O_NONBLOCK ;
377
+ int fd = open (pidfile , flags , 0644 );
378
+ if (fd == -1 ) {
379
+ ERRORF ("Failed to open pidfile: \"%s\": %s" , pidfile , strerror (errno ));
380
+ return -1 ;
381
+ }
382
+
383
+ char pid [20 ];
384
+ snprintf (pid , sizeof (pid ), "%u" , getpid ());
385
+ ssize_t n = write (fd , pid , strlen (pid ));
386
+ if (n != (ssize_t )strlen (pid )) {
387
+ if (n < 0 ) {
388
+ ERRORF ("Failed to write pidfile: \"%s\": %s" , pidfile , strerror (errno ));
389
+ } else {
390
+ // Should never happen, but if it does errno is not set.
391
+ ERRORF ("Short write to pidfile: \"%s\"" , pidfile );
392
+ }
393
+ remove_pidfile (pidfile );
394
+ close (fd );
395
+ return -1 ;
396
+ }
397
+
398
+ return fd ;
399
+ }
400
+
367
401
static void on_accept (struct state * state , int accept_fd , interface_ref iface );
368
402
369
403
int main (int argc , char * argv []) {
@@ -394,15 +428,14 @@ int main(int argc, char *argv[]) {
394
428
// We will receive EPIPE on the socket.
395
429
signal (SIGPIPE , SIG_IGN );
396
430
397
- int pid_fd = -1 ;
431
+ int pidfile_fd = -1 ;
398
432
if (cliopt -> pidfile != NULL ) {
399
- pid_fd = open (cliopt -> pidfile ,
400
- O_WRONLY | O_CREAT | O_EXLOCK | O_TRUNC | O_NONBLOCK , 0644 );
401
- if (pid_fd == -1 ) {
402
- ERRORN ("pidfile_open" );
403
- goto done ;
433
+ pidfile_fd = create_pidfile (cliopt -> pidfile );
434
+ if (pidfile_fd == -1 ) {
435
+ goto done ; // error already logged.
404
436
}
405
437
}
438
+
406
439
DEBUGF ("Opening socket \"%s\" (for UNIX group \"%s\")" , cliopt -> socket_path ,
407
440
cliopt -> socket_group );
408
441
listen_fd = socket_bindlisten (cliopt -> socket_path , cliopt -> socket_group );
@@ -427,15 +460,6 @@ int main(int argc, char *argv[]) {
427
460
goto done ;
428
461
}
429
462
430
- if (pid_fd != -1 ) {
431
- char pid [20 ];
432
- snprintf (pid , sizeof (pid ), "%u" , getpid ());
433
- if (write (pid_fd , pid , strlen (pid )) != (ssize_t )strlen (pid )) {
434
- ERRORN ("write" );
435
- goto done ;
436
- }
437
- }
438
-
439
463
while (1 ) {
440
464
int accept_fd = accept (listen_fd , NULL , NULL );
441
465
if (accept_fd < 0 ) {
@@ -456,9 +480,9 @@ int main(int argc, char *argv[]) {
456
480
if (listen_fd != -1 ) {
457
481
close (listen_fd );
458
482
}
459
- if (pid_fd != -1 ) {
460
- unlink (cliopt -> pidfile );
461
- close (pid_fd );
483
+ if (pidfile_fd != -1 ) {
484
+ remove_pidfile (cliopt -> pidfile );
485
+ close (pidfile_fd );
462
486
}
463
487
if (state .vms_queue != NULL )
464
488
dispatch_release (state .vms_queue );
0 commit comments