r9794: r11627@blu: tridge | 2005-08-30 22:55:27 +1000
[jra/samba/.git] / source4 / lib / pidfile.c
1 /* this code is broken - there is a race condition with the unlink (tridge) */
2
3 /* 
4    Unix SMB/CIFS implementation.
5    pidfile handling
6    Copyright (C) Andrew Tridgell 1998
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "pstring.h"
25 #include "system/filesys.h"
26
27 #ifndef O_NONBLOCK
28 #define O_NONBLOCK
29 #endif
30
31 /* return the pid in a pidfile. return 0 if the process (or pidfile)
32    does not exist */
33 pid_t pidfile_pid(const char *name)
34 {
35         int fd;
36         char pidstr[20];
37         uint_t ret;
38         pstring pidFile;
39
40         slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_piddir(), name);
41
42         fd = open(pidFile, O_NONBLOCK | O_RDONLY, 0644);
43         if (fd == -1) {
44                 return 0;
45         }
46
47         ZERO_STRUCT(pidstr);
48
49         if (read(fd, pidstr, sizeof(pidstr)-1) <= 0) {
50                 goto noproc;
51         }
52
53         ret = atoi(pidstr);
54         
55         if (!process_exists((pid_t)ret)) {
56                 goto noproc;
57         }
58
59         if (fcntl_lock(fd,F_SETLK,0,1,F_RDLCK)) {
60                 /* we could get the lock - it can't be a Samba process */
61                 goto noproc;
62         }
63
64         close(fd);
65         return (pid_t)ret;
66
67  noproc:
68         close(fd);
69         unlink(pidFile);
70         return 0;
71 }
72
73 /* create a pid file in the pid directory. open it and leave it locked */
74 void pidfile_create(const char *name)
75 {
76         int     fd;
77         char    buf[20];
78         pstring pidFile;
79         pid_t pid;
80
81         slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_piddir(), name);
82
83         pid = pidfile_pid(name);
84         if (pid != 0) {
85                 DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n", 
86                          name, pidFile, (int)pid));
87                 exit(1);
88         }
89
90         fd = open(pidFile, O_NONBLOCK | O_CREAT | O_WRONLY | O_EXCL, 0644);
91         if (fd == -1) {
92                 DEBUG(0,("ERROR: can't open %s: Error was %s\n", pidFile, 
93                          strerror(errno)));
94                 exit(1);
95         }
96
97         if (fcntl_lock(fd,F_SETLK,0,1,F_WRLCK)==False) {
98                 DEBUG(0,("ERROR: %s : fcntl lock of file %s failed. Error was %s\n",  
99               name, pidFile, strerror(errno)));
100                 exit(1);
101         }
102
103         memset(buf, 0, sizeof(buf));
104         slprintf(buf, sizeof(buf) - 1, "%u\n", (uint_t) getpid());
105         if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) {
106                 DEBUG(0,("ERROR: can't write to file %s: %s\n", 
107                          pidFile, strerror(errno)));
108                 exit(1);
109         }
110         /* Leave pid file open & locked for the duration... */
111 }