3 * Functions to assist in the writing and removing of pidfiles.
5 * Russ Dill <Russ.Dill@asu.edu> September 2001
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <sys/types.h>
32 static char *saved_pidfile;
34 static void pidfile_delete(void)
36 if (saved_pidfile) unlink(saved_pidfile);
40 int pidfile_acquire(const char *pidfile)
43 if (!pidfile) return -1;
45 pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644);
47 LOG(LOG_ERR, "Unable to open pidfile %s: %m\n", pidfile);
49 lockf(pid_fd, F_LOCK, 0);
51 atexit(pidfile_delete);
52 saved_pidfile = (char *) pidfile;
59 void pidfile_write_release(int pid_fd)
63 if (pid_fd < 0) return;
65 if ((out = fdopen(pid_fd, "w")) != NULL) {
66 fprintf(out, "%d\n", getpid());
69 lockf(pid_fd, F_UNLCK, 0);