2
0

pidfile.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. pidfile.c - interact with pidfiles
  3. Copyright (c) 1995 Martin Schulze <Martin.Schulze@Linux.DE>
  4. This file is part of the sysklogd package, a kernel and system log daemon.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA
  16. */
  17. /* left unaltered for tinc -- Ivo Timmermans */
  18. /*
  19. * Sat Aug 19 13:24:33 MET DST 1995: Martin Schulze
  20. * First version (v0.2) released
  21. */
  22. #include "system.h"
  23. #ifndef HAVE_MINGW
  24. /* read_pid
  25. *
  26. * Reads the specified pidfile and returns the read pid.
  27. * 0 is returned if either there's no pidfile, it's empty
  28. * or no pid can be read.
  29. */
  30. pid_t read_pid (char *pidfile)
  31. {
  32. FILE *f;
  33. long pid;
  34. if (!(f=fopen(pidfile,"r")))
  35. return 0;
  36. fscanf(f,"%ld", &pid);
  37. fclose(f);
  38. return pid;
  39. }
  40. /* check_pid
  41. *
  42. * Reads the pid using read_pid and looks up the pid in the process
  43. * table (using /proc) to determine if the process already exists. If
  44. * so the pid is returned, otherwise 0.
  45. */
  46. pid_t check_pid (char *pidfile)
  47. {
  48. pid_t pid = read_pid(pidfile);
  49. /* Amazing ! _I_ am already holding the pid file... */
  50. if ((!pid) || (pid == getpid ()))
  51. return 0;
  52. /*
  53. * The 'standard' method of doing this is to try and do a 'fake' kill
  54. * of the process. If an ESRCH error is returned the process cannot
  55. * be found -- GW
  56. */
  57. /* But... errno is usually changed only on error.. */
  58. errno = 0;
  59. if (kill(pid, 0) && errno == ESRCH)
  60. return 0;
  61. return pid;
  62. }
  63. /* write_pid
  64. *
  65. * Writes the pid to the specified file. If that fails 0 is
  66. * returned, otherwise the pid.
  67. */
  68. pid_t write_pid (char *pidfile)
  69. {
  70. FILE *f;
  71. int fd;
  72. pid_t pid;
  73. if ( ((fd = open(pidfile, O_RDWR|O_CREAT, 0644)) == -1)
  74. || ((f = fdopen(fd, "r+")) == NULL) ) {
  75. return 0;
  76. }
  77. #ifdef HAVE_FLOCK
  78. if (flock(fd, LOCK_EX|LOCK_NB) == -1) {
  79. fclose(f);
  80. return 0;
  81. }
  82. #endif
  83. pid = getpid();
  84. if (!fprintf(f,"%ld\n", (long)pid)) {
  85. close(fd);
  86. return 0;
  87. }
  88. fflush(f);
  89. #ifdef HAVE_FLOCK
  90. if (flock(fd, LOCK_UN) == -1) {
  91. close(fd);
  92. return 0;
  93. }
  94. #endif
  95. close(fd);
  96. return pid;
  97. }
  98. /* remove_pid
  99. *
  100. * Remove the the specified file. The result from unlink(2)
  101. * is returned
  102. */
  103. int remove_pid (char *pidfile)
  104. {
  105. return unlink (pidfile);
  106. }
  107. #endif