pidfile.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. #include "pidfile.h"
  24. #ifndef HAVE_MINGW
  25. /* read_pid
  26. *
  27. * Reads the specified pidfile and returns the read pid.
  28. * 0 is returned if either there's no pidfile, it's empty
  29. * or no pid can be read.
  30. */
  31. pid_t read_pid(const char *pidfile) {
  32. FILE *f;
  33. long pid;
  34. if(!(f = fopen(pidfile, "r"))) {
  35. return 0;
  36. }
  37. if(fscanf(f, "%20ld", &pid) != 1) {
  38. pid = 0;
  39. }
  40. fclose(f);
  41. return (pid_t)pid;
  42. }
  43. /* check_pid
  44. *
  45. * Reads the pid using read_pid and looks up the pid in the process
  46. * table (using /proc) to determine if the process already exists. If
  47. * so the pid is returned, otherwise 0.
  48. */
  49. pid_t check_pid(const char *pidfile) {
  50. pid_t pid = read_pid(pidfile);
  51. /* Amazing ! _I_ am already holding the pid file... */
  52. if((!pid) || (pid == getpid())) {
  53. return 0;
  54. }
  55. /*
  56. * The 'standard' method of doing this is to try and do a 'fake' kill
  57. * of the process. If an ESRCH error is returned the process cannot
  58. * be found -- GW
  59. */
  60. /* But... errno is usually changed only on error.. */
  61. errno = 0;
  62. if(kill(pid, 0) && errno == ESRCH) {
  63. return 0;
  64. }
  65. return pid;
  66. }
  67. /* write_pid
  68. *
  69. * Writes the pid to the specified file. If that fails 0 is
  70. * returned, otherwise the pid.
  71. */
  72. pid_t write_pid(const char *pidfile) {
  73. FILE *f;
  74. int fd;
  75. pid_t pid;
  76. if((fd = open(pidfile, O_RDWR | O_CREAT, 0644)) == -1) {
  77. return 0;
  78. }
  79. if((f = fdopen(fd, "r+")) == NULL) {
  80. close(fd);
  81. return 0;
  82. }
  83. #ifdef HAVE_FLOCK
  84. if(flock(fd, LOCK_EX | LOCK_NB) == -1) {
  85. fclose(f);
  86. return 0;
  87. }
  88. #endif
  89. pid = getpid();
  90. if(!fprintf(f, "%ld\n", (long)pid)) {
  91. fclose(f);
  92. return 0;
  93. }
  94. fflush(f);
  95. #ifdef HAVE_FLOCK
  96. if(flock(fd, LOCK_UN) == -1) {
  97. fclose(f);
  98. return 0;
  99. }
  100. #endif
  101. fclose(f);
  102. return pid;
  103. }
  104. /* remove_pid
  105. *
  106. * Remove the the specified file. The result from unlink(2)
  107. * is returned
  108. */
  109. int remove_pid(const char *pidfile) {
  110. return unlink(pidfile);
  111. }
  112. #endif