common.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* vi: set sw=4 ts=4: */
  2. /* common.c
  3. *
  4. * Functions for debugging and logging as well as some other
  5. * simple helper functions.
  6. *
  7. * Russ Dill <Russ.Dill@asu.edu> 2001-2003
  8. * Rewritten by Vladimir Oleynik <dzo@simtreas.ru> (C) 2003
  9. *
  10. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  11. */
  12. #include <syslog.h>
  13. #include "common.h"
  14. long uptime(void)
  15. {
  16. struct sysinfo info;
  17. sysinfo(&info);
  18. return info.uptime;
  19. }
  20. /*
  21. * This function makes sure our first socket calls
  22. * aren't going to fd 1 (printf badness...) and are
  23. * not later closed by daemon()
  24. */
  25. static inline void sanitize_fds(void)
  26. {
  27. int fd = xopen(bb_dev_null, O_RDWR);
  28. while (fd < 3)
  29. fd = dup(fd);
  30. close(fd);
  31. }
  32. void udhcp_background(const char *pidfile)
  33. {
  34. #ifdef __uClinux__
  35. bb_error_msg("cannot background in uclinux (yet)");
  36. #else /* __uClinux__ */
  37. int pid_fd;
  38. /* hold lock during fork. */
  39. pid_fd = pidfile_acquire(pidfile);
  40. setsid();
  41. xdaemon(0, 0);
  42. logmode &= ~LOGMODE_STDIO;
  43. pidfile_write_release(pid_fd);
  44. #endif /* __uClinux__ */
  45. }
  46. void udhcp_start_log_and_pid(const char *pidfile)
  47. {
  48. int pid_fd;
  49. /* Make sure our syslog fd isn't overwritten */
  50. sanitize_fds();
  51. /* do some other misc startup stuff while we are here to save bytes */
  52. pid_fd = pidfile_acquire(pidfile);
  53. pidfile_write_release(pid_fd);
  54. /* equivelent of doing a fflush after every \n */
  55. setlinebuf(stdout);
  56. if (ENABLE_FEATURE_UDHCP_SYSLOG) {
  57. openlog(applet_name, LOG_PID, LOG_LOCAL0);
  58. logmode |= LOGMODE_SYSLOG;
  59. }
  60. bb_info_msg("%s (v%s) started", applet_name, BB_VER);
  61. }