unbound.c (368129) | unbound.c (368693) |
---|---|
1/* 2 * daemon/unbound.c - main program for unbound DNS resolver daemon. 3 * 4 * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 * 6 * This software is open source. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 323 unchanged lines hidden (view full) --- 332 return -1; 333 } 334 return pid; 335} 336 337/** write pid to file. 338 * @param pidfile: file name of pid file. 339 * @param pid: pid to write to file. | 1/* 2 * daemon/unbound.c - main program for unbound DNS resolver daemon. 3 * 4 * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 * 6 * This software is open source. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 323 unchanged lines hidden (view full) --- 332 return -1; 333 } 334 return pid; 335} 336 337/** write pid to file. 338 * @param pidfile: file name of pid file. 339 * @param pid: pid to write to file. |
340 * @return false on failure |
|
340 */ | 341 */ |
341static void | 342static int |
342writepid (const char* pidfile, pid_t pid) 343{ | 343writepid (const char* pidfile, pid_t pid) 344{ |
344 FILE* f; | 345 int fd; 346 char pidbuf[32]; 347 size_t count = 0; 348 snprintf(pidbuf, sizeof(pidbuf), "%lu\n", (unsigned long)pid); |
345 | 349 |
346 if ((f = fopen(pidfile, "w")) == NULL ) { | 350 if((fd = open(pidfile, O_WRONLY | O_CREAT | O_TRUNC 351#ifdef O_NOFOLLOW 352 | O_NOFOLLOW 353#endif 354 , 0644)) == -1) { |
347 log_err("cannot open pidfile %s: %s", 348 pidfile, strerror(errno)); | 355 log_err("cannot open pidfile %s: %s", 356 pidfile, strerror(errno)); |
349 return; | 357 return 0; |
350 } | 358 } |
351 if(fprintf(f, "%lu\n", (unsigned long)pid) < 0) { 352 log_err("cannot write to pidfile %s: %s", 353 pidfile, strerror(errno)); | 359 while(count < strlen(pidbuf)) { 360 ssize_t r = write(fd, pidbuf+count, strlen(pidbuf)-count); 361 if(r == -1) { 362 if(errno == EAGAIN || errno == EINTR) 363 continue; 364 log_err("cannot write to pidfile %s: %s", 365 pidfile, strerror(errno)); 366 close(fd); 367 return 0; 368 } else if(r == 0) { 369 log_err("cannot write any bytes to pidfile %s: " 370 "write returns 0 bytes written", pidfile); 371 close(fd); 372 return 0; 373 } 374 count += r; |
354 } | 375 } |
355 fclose(f); | 376 close(fd); 377 return 1; |
356} 357 358/** 359 * check old pid file. 360 * @param pidfile: the file name of the pid file. 361 * @param inchroot: if pidfile is inchroot and we can thus expect to 362 * be able to delete it. 363 */ --- 137 unchanged lines hidden (view full) --- 501 /* daemonize because pid is needed by the writepid func */ 502 if(!debug_mode && cfg->do_daemonize) { 503 detach(); 504 } 505 506 /* write new pidfile (while still root, so can be outside chroot) */ 507#ifdef HAVE_KILL 508 if(cfg->pidfile && cfg->pidfile[0] && need_pidfile) { | 378} 379 380/** 381 * check old pid file. 382 * @param pidfile: the file name of the pid file. 383 * @param inchroot: if pidfile is inchroot and we can thus expect to 384 * be able to delete it. 385 */ --- 137 unchanged lines hidden (view full) --- 523 /* daemonize because pid is needed by the writepid func */ 524 if(!debug_mode && cfg->do_daemonize) { 525 detach(); 526 } 527 528 /* write new pidfile (while still root, so can be outside chroot) */ 529#ifdef HAVE_KILL 530 if(cfg->pidfile && cfg->pidfile[0] && need_pidfile) { |
509 writepid(daemon->pidfile, getpid()); 510 if(cfg->username && cfg->username[0] && cfg_uid != (uid_t)-1 && 511 pidinchroot) { | 531 if(writepid(daemon->pidfile, getpid())) { 532 if(cfg->username && cfg->username[0] && cfg_uid != (uid_t)-1 && 533 pidinchroot) { |
512# ifdef HAVE_CHOWN | 534# ifdef HAVE_CHOWN |
513 if(chown(daemon->pidfile, cfg_uid, cfg_gid) == -1) { 514 verbose(VERB_QUERY, "cannot chown %u.%u %s: %s", 515 (unsigned)cfg_uid, (unsigned)cfg_gid, 516 daemon->pidfile, strerror(errno)); 517 } | 535 if(chown(daemon->pidfile, cfg_uid, cfg_gid) == -1) { 536 verbose(VERB_QUERY, "cannot chown %u.%u %s: %s", 537 (unsigned)cfg_uid, (unsigned)cfg_gid, 538 daemon->pidfile, strerror(errno)); 539 } |
518# endif /* HAVE_CHOWN */ | 540# endif /* HAVE_CHOWN */ |
541 } |
|
519 } 520 } 521#else 522 (void)daemon; 523 (void)need_pidfile; 524#endif /* HAVE_KILL */ 525 526 /* Set user context */ --- 267 unchanged lines hidden --- | 542 } 543 } 544#else 545 (void)daemon; 546 (void)need_pidfile; 547#endif /* HAVE_KILL */ 548 549 /* Set user context */ --- 267 unchanged lines hidden --- |