3 * Functions to assist in the writing and removing of pidfiles.
5 * Russ Dill <Russ.Dill@asu.edu> Soptember 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 int pidfile_acquire(char *pidfile)
35 if (pidfile == NULL) return -1;
37 pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644);
39 LOG(LOG_ERR, "Unable to open pidfile %s: %s\n",
40 pidfile, strerror(errno));
42 lockf(pid_fd, F_LOCK, 0);
49 void pidfile_write_release(int pid_fd)
53 if (pid_fd < 0) return;
55 if ((out = fdopen(pid_fd, "w")) != NULL) {
56 fprintf(out, "%d\n", getpid());
59 lockf(pid_fd, F_UNLCK, 0);
64 void pidfile_delete(char *pidfile)
66 if (pidfile) unlink(pidfile);