fixed pidfile handling to check for a lock on the file, so we can be
[samba.git] / source3 / lib / pidfile.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    pidfile handling
5    Copyright (C) Andrew Tridgell 1998
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24
25 extern int DEBUGLEVEL;
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(char *name)
34 {
35         int fd;
36         char pidstr[20];
37         unsigned ret;
38         pstring pidFile;
39
40         slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_lockdir(), name);
41
42         fd = open(pidFile, O_NONBLOCK | O_RDWR);
43         if (fd == -1) {
44                 return 0;
45         }
46
47         ZERO_ARRAY(pidstr);
48
49         if (read(fd, pidstr, sizeof(pidstr)-1) <= 0) {
50                 goto ok;
51         }
52
53         ret = atoi(pidstr);
54         
55         if (!process_exists(ret)) {
56                 goto ok;
57         }
58
59         if (fcntl_lock(fd,SMB_F_SETLK,0,1,F_WRLCK)==False) {
60                 /* we could get the lock - it can't be a Samba process */
61                 goto ok;
62         }
63
64         close(fd);
65         return (pid_t)ret;
66
67  ok:
68         close(fd);
69         unlink(pidFile);
70         return 0;
71 }
72
73 /* create a pid file in the lock directory. open it and leave it locked */
74 void pidfile_create(char *name)
75 {
76         int     fd;
77         char    buf[20];
78         pstring pidFile;
79         int pid;
80
81         slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_lockdir(), 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, pid));
87                 exit(1);
88         }
89
90         fd = sys_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,SMB_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", (unsigned int) getpid());
105         if (write(fd, buf, sizeof(buf)) != sizeof(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 }
112