r13752: Add doxyfile and fix formatting of comments. Current output is available...
[samba.git] / source4 / lib / util / 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 "system/filesys.h"
25
26 #ifndef O_NONBLOCK
27 #define O_NONBLOCK
28 #endif
29
30 /**
31  * @file
32  * @brief Pid file handling
33  */
34
35 /**
36  * return the pid in a pidfile. return 0 if the process (or pidfile)
37  * does not exist 
38  */
39 pid_t pidfile_pid(const char *name)
40 {
41         int fd;
42         char pidstr[20];
43         uint_t ret;
44         char *pidFile;
45
46         asprintf(&pidFile, "%s/%s.pid", lp_piddir(), name);
47
48         fd = open(pidFile, O_NONBLOCK | O_RDONLY, 0644);
49
50         if (fd == -1) {
51                 SAFE_FREE(pidFile);
52                 return 0;
53         }
54
55         ZERO_STRUCT(pidstr);
56
57         if (read(fd, pidstr, sizeof(pidstr)-1) <= 0) {
58                 goto noproc;
59         }
60
61         ret = atoi(pidstr);
62         
63         if (!process_exists((pid_t)ret)) {
64                 goto noproc;
65         }
66
67         if (fcntl_lock(fd,F_SETLK,0,1,F_RDLCK)) {
68                 /* we could get the lock - it can't be a Samba process */
69                 goto noproc;
70         }
71
72         close(fd);
73         SAFE_FREE(pidFile);
74         return (pid_t)ret;
75
76  noproc:
77         close(fd);
78         unlink(pidFile);
79         SAFE_FREE(pidFile);
80         return 0;
81 }
82
83 /**
84  * create a pid file in the pid directory. open it and leave it locked 
85  */
86 void pidfile_create(const char *name)
87 {
88         int     fd;
89         char    buf[20];
90         char *pidFile;
91         pid_t pid;
92
93         asprintf(&pidFile, "%s/%s.pid", lp_piddir(), name);
94
95         pid = pidfile_pid(name);
96         if (pid != 0) {
97                 DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n", 
98                          name, pidFile, (int)pid));
99                 exit(1);
100         }
101
102         fd = open(pidFile, O_NONBLOCK | O_CREAT | O_WRONLY | O_EXCL, 0644);
103         if (fd == -1) {
104                 DEBUG(0,("ERROR: can't open %s: Error was %s\n", pidFile, 
105                          strerror(errno)));
106                 exit(1);
107         }
108
109         if (fcntl_lock(fd,F_SETLK,0,1,F_WRLCK)==False) {
110                 DEBUG(0,("ERROR: %s : fcntl lock of file %s failed. Error was %s\n",  
111               name, pidFile, strerror(errno)));
112                 exit(1);
113         }
114
115         memset(buf, 0, sizeof(buf));
116         slprintf(buf, sizeof(buf) - 1, "%u\n", (uint_t) getpid());
117         if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) {
118                 DEBUG(0,("ERROR: can't write to file %s: %s\n", 
119                          pidFile, strerror(errno)));
120                 exit(1);
121         }
122
123         /* Leave pid file open & locked for the duration... */
124         SAFE_FREE(pidFile);
125 }