1 /*-
2  * Copyright (c) 2003-2005 MAEKAWA Masahide <maekawa@cvsync.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <limits.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 #include "compat_stdbool.h"
43 #include "compat_stdio.h"
44 #include "compat_stdlib.h"
45 #include "compat_limits.h"
46 
47 #include "logmsg.h"
48 #include "pid.h"
49 
50 static char cvsync_pidfile[PATH_MAX + CVSYNC_NAME_MAX + 1];
51 
52 char *
pid_create(const char * pidfile)53 pid_create(const char *pidfile)
54 {
55 	char pid[64];
56 	ssize_t len;
57 	int fd, wn;
58 
59 	cvsync_pidfile[0] = '\0';
60 
61 	if (pidfile == NULL)
62 		return (cvsync_pidfile);
63 
64 	if (pidfile[0] != '/') {
65 		if (realpath(pidfile, cvsync_pidfile) == NULL) {
66 			logmsg_err("%s: %s", pidfile, strerror(errno));
67 			return (NULL);
68 		}
69 	} else {
70 		wn = snprintf(cvsync_pidfile, sizeof(cvsync_pidfile), "%s",
71 			      pidfile);
72 		if ((wn <= 0) || ((size_t)wn >= sizeof(cvsync_pidfile)))
73 			return (NULL);
74 	}
75 
76 	if ((fd = open(cvsync_pidfile, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC,
77 		       S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) {
78 		if (errno == EEXIST)
79 			logmsg_err("Another cvsync process is running?");
80 		else
81 			logmsg_err("%s: %s", cvsync_pidfile, strerror(errno));
82 		cvsync_pidfile[0] = '\0';
83 		return (NULL);
84 	}
85 	wn = snprintf(pid, sizeof(pid), "%d\n", (signed)getpid());
86 	if ((wn <= 0) || ((size_t)wn >= sizeof(pid))) {
87 		(void)unlink(cvsync_pidfile);
88 		cvsync_pidfile[0] = '\0';
89 		return (NULL);
90 	}
91 	if ((len = write(fd, pid, (size_t)wn)) == -1) {
92 		(void)unlink(cvsync_pidfile);
93 		cvsync_pidfile[0] = '\0';
94 		return (NULL);
95 	}
96 	if (len != (ssize_t)wn) {
97 		(void)unlink(cvsync_pidfile);
98 		cvsync_pidfile[0] = '\0';
99 		return (NULL);
100 	}
101 	if (close(fd) == -1) {
102 		logmsg_err("%s: %s", cvsync_pidfile, strerror(errno));
103 		(void)unlink(cvsync_pidfile);
104 		cvsync_pidfile[0] = '\0';
105 		return (NULL);
106 	}
107 
108 	return (cvsync_pidfile);
109 }
110 
111 bool
pid_remove(void)112 pid_remove(void)
113 {
114 	char pid[64], *ep;
115 	ssize_t rn;
116 	long npid;
117 	int fd;
118 
119 	if (strlen(cvsync_pidfile) == 0)
120 		return (true);
121 
122 	if ((fd = open(cvsync_pidfile, O_RDONLY, 0)) == -1) {
123 		if (errno == ENOENT)
124 			return (true);
125 		logmsg_err("%s: %s", cvsync_pidfile, strerror(errno));
126 		return (false);
127 	}
128 	if ((rn = read(fd, pid, sizeof(pid) - 1)) == -1) {
129 		logmsg_err("%s: %s", cvsync_pidfile, strerror(errno));
130 		(void)close(fd);
131 		return (false);
132 	}
133 	pid[rn] = '\0';
134 	errno = 0;
135 	npid = strtol(pid, &ep, 10);
136 	if ((ep == NULL) || (*ep != '\n') ||
137 	    ((npid == 0) && (errno == EINVAL)) ||
138 	    (((npid == LONG_MIN) || (npid == LONG_MAX)) &&
139 	     (errno == ERANGE))) {
140 		logmsg_err("%s: %s: %s", cvsync_pidfile, pid,
141 			   strerror(EINVAL));
142 		(void)close(fd);
143 		return (false);
144 	}
145 	if (close(fd) == -1) {
146 		logmsg_err("%s: %s", cvsync_pidfile, strerror(errno));
147 		return (false);
148 	}
149 	if ((pid_t)npid != getpid())
150 		return (false);
151 
152 	if (unlink(cvsync_pidfile) == -1) {
153 		logmsg_err("%s: %s", cvsync_pidfile, strerror(errno));
154 		return (false);
155 	}
156 
157 	return (true);
158 }
159