pid_output.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Process id output.
  3. * Copyright (C) 1998, 1999 Kunihiro Ishiguro
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include <fcntl.h>
  24. #include <log.h>
  25. #include "version.h"
  26. #define PIDFILE_MASK 0644
  27. #ifndef HAVE_FCNTL
  28. pid_t
  29. pid_output (const char *path)
  30. {
  31. FILE *fp;
  32. pid_t pid;
  33. mode_t oldumask;
  34. pid = getpid();
  35. oldumask = umask(0777 & ~PIDFILE_MASK);
  36. fp = fopen (path, "w");
  37. if (fp != NULL)
  38. {
  39. fprintf (fp, "%d\n", (int) pid);
  40. fclose (fp);
  41. umask(oldumask);
  42. return pid;
  43. }
  44. /* XXX Why do we continue instead of exiting? This seems incompatible
  45. with the behavior of the fcntl version below. */
  46. zlog_warn("Can't fopen pid lock file %s (%s), continuing",
  47. path, safe_strerror(errno));
  48. umask(oldumask);
  49. return -1;
  50. }
  51. #else /* HAVE_FCNTL */
  52. pid_t
  53. pid_output (const char *path)
  54. {
  55. int tmp;
  56. int fd;
  57. pid_t pid;
  58. char buf[16];
  59. struct flock lock;
  60. mode_t oldumask;
  61. pid = getpid ();
  62. oldumask = umask(0777 & ~PIDFILE_MASK);
  63. fd = open (path, O_RDWR | O_CREAT, PIDFILE_MASK);
  64. if (fd < 0)
  65. {
  66. zlog_err("Can't create pid lock file %s (%s), exiting",
  67. path, safe_strerror(errno));
  68. umask(oldumask);
  69. exit(1);
  70. }
  71. else
  72. {
  73. size_t pidsize;
  74. umask(oldumask);
  75. memset (&lock, 0, sizeof(lock));
  76. lock.l_type = F_WRLCK;
  77. lock.l_whence = SEEK_SET;
  78. if (fcntl(fd, F_SETLK, &lock) < 0)
  79. {
  80. zlog_err("Could not lock pid_file %s, exiting", path);
  81. exit(1);
  82. }
  83. sprintf (buf, "%d\n", (int) pid);
  84. pidsize = strlen(buf);
  85. if ((tmp = write (fd, buf, pidsize)) != (int)pidsize)
  86. zlog_err("Could not write pid %d to pid_file %s, rc was %d: %s",
  87. (int)pid,path,tmp,safe_strerror(errno));
  88. else if (ftruncate(fd, pidsize) < 0)
  89. zlog_err("Could not truncate pid_file %s to %u bytes: %s",
  90. path,(u_int)pidsize,safe_strerror(errno));
  91. }
  92. return pid;
  93. }
  94. #endif /* HAVE_FCNTL */