pidfile.c

Go to the documentation of this file.
00001 /* pidfile.c
00002  *
00003  * Functions to assist in the writing and removing of pidfiles.
00004  *
00005  * Russ Dill <Russ.Dill@asu.edu> Soptember 2001
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020  */
00021 
00022 #include <sys/types.h>
00023 #include <sys/stat.h>
00024 #include <fcntl.h>
00025 #include <unistd.h>
00026 #include <errno.h>
00027 #include <string.h>
00028 #include <stdio.h>
00029 
00030 #include "debug.h"
00031 
00032 int pidfile_acquire(char *pidfile)
00033 {
00034         int pid_fd;
00035         if (pidfile == NULL) return -1;
00036 
00037         pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644);
00038         if (pid_fd < 0) {
00039                 LOG(LOG_ERR, "Unable to open pidfile %s: %s\n",
00040                     pidfile, strerror(errno));
00041         } else {
00042                 lockf(pid_fd, F_LOCK, 0);
00043         }
00044         
00045         return pid_fd;
00046 }
00047 
00048 
00049 void pidfile_write_release(int pid_fd)
00050 {
00051         FILE *out;
00052 
00053         if (pid_fd < 0) return;
00054 
00055         if ((out = fdopen(pid_fd, "w")) != NULL) {
00056                 fprintf(out, "%d\n", getpid());
00057                 fclose(out);
00058         }
00059         lockf(pid_fd, F_UNLCK, 0);
00060         close(pid_fd);
00061 }
00062 
00063 
00064 void pidfile_delete(char *pidfile)
00065 {
00066         if (pidfile) unlink(pidfile);
00067 }
00068 
00069 

Generated on Tue Jan 15 12:24:45 2008 for Dynamics 0.8.1.Dynamo.1 by  doxygen 1.5.1