r23779: Change from v2 or later to v3 or later.
[samba.git] / source3 / 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 3 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
25 #ifndef O_NONBLOCK
26 #define O_NONBLOCK
27 #endif
28
29 /* return the pid in a pidfile. return 0 if the process (or pidfile)
30    does not exist */
31 pid_t pidfile_pid(const char *name)
32 {
33         int fd;
34         char pidstr[20];
35         pid_t pid;
36         unsigned int ret;
37         char * pidFile;
38
39         if (asprintf(&pidFile, "%s/%s.pid", lp_piddir(), name) == -1) {
40                 return 0;
41         }
42
43         fd = sys_open(pidFile, O_NONBLOCK | O_RDONLY, 0644);
44         if (fd == -1) {
45                 SAFE_FREE(pidFile);
46                 return 0;
47         }
48
49         ZERO_ARRAY(pidstr);
50
51         if (read(fd, pidstr, sizeof(pidstr)-1) <= 0) {
52                 goto noproc;
53         }
54
55         ret = atoi(pidstr);
56
57         if (ret == 0) {
58                 /* Obviously we had some garbage in the pidfile... */
59                 DEBUG(1, ("Could not parse contents of pidfile %s\n",
60                           pidFile));
61                 goto noproc;
62         }
63         
64         pid = (pid_t)ret;
65         if (!process_exists_by_pid(pid)) {
66                 goto noproc;
67         }
68
69         if (fcntl_lock(fd,SMB_F_SETLK,0,1,F_RDLCK)) {
70                 /* we could get the lock - it can't be a Samba process */
71                 goto noproc;
72         }
73
74         SAFE_FREE(pidFile);
75         close(fd);
76         return (pid_t)ret;
77
78  noproc:
79         close(fd);
80         unlink(pidFile);
81         SAFE_FREE(pidFile);
82         return 0;
83 }
84
85 /* create a pid file in the pid directory. open it and leave it locked */
86 void pidfile_create(const char *program_name)
87 {
88         int     fd;
89         char    buf[20];
90         char    *short_configfile;
91         char *name;
92         char *pidFile;
93         pid_t pid;
94
95         /* Add a suffix to the program name if this is a process with a
96          * none default configuration file name. */
97         if (strcmp( CONFIGFILE, dyn_CONFIGFILE) == 0) {
98                 name = SMB_STRDUP(program_name);
99         } else {
100                 short_configfile = strrchr( dyn_CONFIGFILE, '/');
101                 if (short_configfile == NULL) {
102                         /* conf file in current directory */
103                         short_configfile = dyn_CONFIGFILE;
104                 } else {
105                         /* full/relative path provided */
106                         short_configfile++;
107                 }
108                 if (asprintf(&name, "%s-%s", program_name,
109                              short_configfile+1) == -1) {
110                         smb_panic("asprintf failed");
111                 }
112         }
113
114         if (asprintf(&pidFile, "%s/%s.pid", lp_piddir(), name) == -1) {
115                 smb_panic("asprintf failed");
116         }
117
118         pid = pidfile_pid(name);
119         if (pid != 0) {
120                 DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n", 
121                          name, pidFile, (int)pid));
122                 exit(1);
123         }
124
125         fd = sys_open(pidFile, O_NONBLOCK | O_CREAT | O_WRONLY | O_EXCL, 0644);
126         if (fd == -1) {
127                 DEBUG(0,("ERROR: can't open %s: Error was %s\n", pidFile, 
128                          strerror(errno)));
129                 exit(1);
130         }
131
132         if (fcntl_lock(fd,SMB_F_SETLK,0,1,F_WRLCK)==False) {
133                 DEBUG(0,("ERROR: %s : fcntl lock of file %s failed. Error was %s\n",  
134               name, pidFile, strerror(errno)));
135                 exit(1);
136         }
137
138         memset(buf, 0, sizeof(buf));
139         slprintf(buf, sizeof(buf) - 1, "%u\n", (unsigned int) sys_getpid());
140         if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) {
141                 DEBUG(0,("ERROR: can't write to file %s: %s\n", 
142                          pidFile, strerror(errno)));
143                 exit(1);
144         }
145         /* Leave pid file open & locked for the duration... */
146         SAFE_FREE(name);
147         SAFE_FREE(pidFile);
148 }