1 /*****************************************************************************\
2  *  daemonize.c - daemonization routine
3  *****************************************************************************
4  *  Copyright (C) 2002 The Regents of the University of California.
5  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
6  *  Written by Mark A. Grondona <mgrondona@llnl.gov>.
7  *  CODE-OCEC-09-009. All rights reserved.
8  *
9  *  This file is part of Slurm, a resource management program.
10  *  For details, see <https://slurm.schedmd.com/>.
11  *  Please also read the included file: DISCLAIMER.
12  *
13  *  Slurm is free software; you can redistribute it and/or modify it under
14  *  the terms of the GNU General Public License as published by the Free
15  *  Software Foundation; either version 2 of the License, or (at your option)
16  *  any later version.
17  *
18  *  In addition, as a special exception, the copyright holders give permission
19  *  to link the code of portions of this program with the OpenSSL library under
20  *  certain conditions as described in each individual source file, and
21  *  distribute linked combinations including the two. You must obey the GNU
22  *  General Public License in all respects for all of the code used other than
23  *  OpenSSL. If you modify file(s) with this exception, you may extend this
24  *  exception to your version of the file(s), but you are not obligated to do
25  *  so. If you do not wish to do so, delete this exception statement from your
26  *  version.  If you delete this exception statement from all source files in
27  *  the program, then also delete it here.
28  *
29  *  Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
30  *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
31  *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
32  *  details.
33  *
34  *  You should have received a copy of the GNU General Public License along
35  *  with Slurm; if not, write to the Free Software Foundation, Inc.,
36  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
37 \*****************************************************************************/
38 
39 #include <config.h>
40 
41 #define _GNU_SOURCE
42 
43 #include <fcntl.h>
44 #include <sys/resource.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47 #include <unistd.h>
48 
49 #include "src/common/daemonize.h"
50 #include "src/common/fd.h"
51 #include "src/common/log.h"
52 #include "src/common/macros.h"
53 #include "src/common/xassert.h"
54 
55 /*
56  * Double-fork and go into background.
57  * Caller is responsible for umasks
58  */
xdaemon(void)59 int xdaemon(void)
60 {
61 	int devnull;
62 
63 	switch (fork()) {
64 		case  0 : break;        /* child */
65 		case -1 : return -1;
66 		default : _exit(0);     /* exit parent */
67 	}
68 
69 	if (setsid() < 0)
70 		return -1;
71 
72 	switch (fork()) {
73 		case 0 : break;         /* child */
74 		case -1: return -1;
75 		default: _exit(0);      /* exit parent */
76 	}
77 
78 	/*
79 	 * dup stdin, stdout, and stderr onto /dev/null
80 	 */
81 	devnull = open("/dev/null", O_RDWR);
82 	if (devnull < 0)
83 		error("Unable to open /dev/null: %m");
84 	if (dup2(devnull, STDIN_FILENO) < 0)
85 		error("Unable to dup /dev/null onto stdin: %m");
86 	if (dup2(devnull, STDOUT_FILENO) < 0)
87 		error("Unable to dup /dev/null onto stdout: %m");
88 	if (dup2(devnull, STDERR_FILENO) < 0)
89 		error("Unable to dup /dev/null onto stderr: %m");
90 	if (close(devnull) < 0)
91 		error("Unable to close /dev/null: %m");
92 
93 	return 0;
94 }
95 
96 /*
97  * Read and return pid stored in pidfile.
98  * Returns 0 if file doesn't exist or pid cannot be read.
99  * If pidfd != NULL, the file will be kept open and the fd
100  * returned.
101  */
102 pid_t
read_pidfile(const char * pidfile,int * pidfd)103 read_pidfile(const char *pidfile, int *pidfd)
104 {
105 	int fd;
106 	FILE *fp = NULL;
107 	unsigned long pid;
108 	pid_t         lpid;
109 
110 	if ((fd = open(pidfile, O_RDONLY)) < 0)
111 		return ((pid_t) 0);
112 
113 	if (!(fp = fdopen(fd, "r"))) {
114 		error ("Unable to access old pidfile at `%s': %m", pidfile);
115 		(void) close(fd);
116 		return ((pid_t) 0);
117 	}
118 
119 	if (fscanf(fp, "%lu", &pid) < 1) {
120 		error ("Possible corrupt pidfile `%s'", pidfile);
121 		(void) close(fd);
122 		return ((pid_t) 0);
123 	}
124 
125 	if ((lpid = fd_is_read_lock_blocked(fd)) == (pid_t) 0) {
126 		verbose ("pidfile not locked, assuming no running daemon");
127 		(void) close(fd);
128 		return ((pid_t) 0);
129 	}
130 
131 	if (lpid != (pid_t) pid)
132 		fatal ("pidfile locked by %lu but contains pid=%lu",
133 		       (unsigned long) lpid, (unsigned long) pid);
134 
135 	if (pidfd != NULL)
136 		*pidfd = fd;
137 	else
138 		(void) close(fd);
139 
140 /*	fclose(fp);	NOTE: DO NOT CLOSE, "fd" CONTAINS FILE DESCRIPTOR */
141 	return (lpid);
142 }
143 
144 
145 
146 int
create_pidfile(const char * pidfile,uid_t uid)147 create_pidfile(const char *pidfile, uid_t uid)
148 {
149 	FILE *fp;
150 	int fd;
151 
152 	xassert(pidfile != NULL);
153 	xassert(pidfile[0] == '/');
154 
155 	fd = open(pidfile, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC,
156 		  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
157 	if (fd < 0) {
158 		error("Unable to open pidfile `%s': %m", pidfile);
159 		return -1;
160 	}
161 
162 	if (!(fp = fdopen(fd, "w"))) {
163 		error("Unable to access pidfile at `%s': %m", pidfile);
164 		(void) close(fd);
165 		return -1;
166 	}
167 
168 	if (fd_get_write_lock(fd) < 0) {
169 		error ("Unable to lock pidfile `%s': %m", pidfile);
170 		goto error;
171 	}
172 
173 	if (fprintf(fp, "%lu\n", (unsigned long) getpid()) == EOF) {
174 		error("Unable to write to pidfile `%s': %m", pidfile);
175 		goto error;
176 	}
177 
178 	fflush(fp);
179 
180 	if (uid && (fchown(fd, uid, -1) < 0))
181 		error ("Unable to reset owner of pidfile: %m");
182 
183 /*	fclose(fp);	NOTE: DO NOT CLOSE, "fd" CONTAINS FILE DESCRIPTOR */
184 	return fd;
185 
186   error:
187 	(void)fclose(fp); /* Ignore errors */
188 
189 	if (unlink(pidfile) < 0)
190 		error("Unable to remove pidfile `%s': %m", pidfile);
191 	return -1;
192 }
193 
194 void
test_core_limit(void)195 test_core_limit(void)
196 {
197 #ifdef RLIMIT_CORE
198 	struct rlimit rlim[1];
199 	if (getrlimit(RLIMIT_CORE, rlim) < 0)
200 		error("Unable to get core limit");
201 	else if (rlim->rlim_cur != RLIM_INFINITY) {
202 		rlim->rlim_cur /= 1024;	/* bytes to KB */
203 		if (rlim->rlim_cur < 2048) {
204 			verbose("Warning: Core limit is only %ld KB",
205 				(long int) rlim->rlim_cur);
206 		}
207 	}
208 #endif
209 	return;
210 }
211