xref: /titanic_51/usr/src/cmd/fm/fmd/common/fmd_main.c (revision 45916cd2fec6e79bca5dee0421bd39e3c2910d1e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/wait.h>
32 #include <sys/corectl.h>
33 #include <sys/resource.h>
34 
35 #include <priv_utils.h>
36 #include <signal.h>
37 #include <unistd.h>
38 #include <limits.h>
39 #include <fcntl.h>
40 #include <strings.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 
44 #include <fmd_error.h>
45 #include <fmd_string.h>
46 #include <fmd_conf.h>
47 #include <fmd_dispq.h>
48 #include <fmd_subr.h>
49 #include <fmd.h>
50 
51 fmd_t fmd;
52 
53 /*
54  * For DEBUG builds, we define a set of hooks for libumem that provide useful
55  * default settings for the allocator's debugging facilities.
56  */
57 #ifdef	DEBUG
58 const char *
59 _umem_debug_init()
60 {
61 	return ("default,verbose"); /* $UMEM_DEBUG setting */
62 }
63 
64 const char *
65 _umem_logging_init(void)
66 {
67 	return ("fail,contents"); /* $UMEM_LOGGING setting */
68 }
69 #endif	/* DEBUG */
70 
71 /*
72  * We use a two-phase algorithm for becoming a daemon because we want the
73  * daemon process (the child) to do the work of becoming MT-hot and opening our
74  * event transport.  Since these operations can fail and need to result in the
75  * daemon failing to start, the parent must wait until fmd_run() completes to
76  * know whether it can return zero or non-zero status to the invoking command.
77  * The parent waits on a pipe inside this function to read the exit status.
78  * The child gets the write-end of the pipe returned by daemonize_init() and
79  * then fmd_run() uses the pipe to set the exit status and detach the parent.
80  */
81 static int
82 daemonize_init(void)
83 {
84 	int status, pfds[2];
85 	sigset_t set, oset;
86 	struct rlimit rlim;
87 	char path[PATH_MAX];
88 	pid_t pid;
89 
90 	/*
91 	 * Set our per-process core file path to leave core files in our
92 	 * var/fm/fmd directory, named after the PID to aid in debugging,
93 	 * and make sure that there is no restriction on core file size.
94 	 */
95 	(void) snprintf(path, sizeof (path),
96 	    "%s/var/fm/fmd/core.%s.%%p", fmd.d_rootdir, fmd.d_pname);
97 
98 	(void) core_set_process_path(path, strlen(path) + 1, fmd.d_pid);
99 
100 	rlim.rlim_cur = RLIM_INFINITY;
101 	rlim.rlim_max = RLIM_INFINITY;
102 
103 	(void) setrlimit(RLIMIT_CORE, &rlim);
104 
105 	/*
106 	 * Reset all of our privilege sets to the minimum set of required
107 	 * privileges.  We continue to run as root so that files we create
108 	 * such as logs and checkpoints are secured in the /var filesystem.
109 	 */
110 	if (__init_daemon_priv(PU_RESETGROUPS | PU_LIMITPRIVS | PU_INHERITPRIVS,
111 	    0, 0, /* run as uid 0 and gid 0 */
112 	    PRIV_FILE_DAC_EXECUTE, PRIV_FILE_DAC_READ, PRIV_FILE_DAC_SEARCH,
113 	    PRIV_FILE_DAC_WRITE, PRIV_FILE_OWNER, PRIV_PROC_OWNER,
114 	    PRIV_PROC_PRIOCNTL, PRIV_SYS_ADMIN, PRIV_SYS_CONFIG,
115 	    PRIV_SYS_DEVICES, PRIV_SYS_RES_CONFIG, NULL) != 0)
116 		fmd_error(EFMD_EXIT, "additional privileges required to run\n");
117 
118 	/*
119 	 * Block all signals prior to the fork and leave them blocked in the
120 	 * parent so we don't get in a situation where the parent gets SIGINT
121 	 * and returns non-zero exit status and the child is actually running.
122 	 * In the child, restore the signal mask once we've done our setsid().
123 	 */
124 	(void) sigfillset(&set);
125 	(void) sigdelset(&set, SIGABRT);
126 	(void) sigprocmask(SIG_BLOCK, &set, &oset);
127 
128 	if (pipe(pfds) == -1)
129 		fmd_error(EFMD_EXIT, "failed to create pipe for daemonize");
130 
131 	if ((pid = fork()) == -1)
132 		fmd_error(EFMD_EXIT, "failed to fork into background");
133 
134 	/*
135 	 * If we're the parent process, wait for either the child to send us
136 	 * the appropriate exit status over the pipe or for the read to fail
137 	 * (presumably with 0 for EOF if our child terminated abnormally).
138 	 * If the read fails, exit with either the child's exit status if it
139 	 * exited or with FMD_EXIT_ERROR if it died from a fatal signal.
140 	 */
141 	if (pid != 0) {
142 		(void) close(pfds[1]);
143 
144 		if (read(pfds[0], &status, sizeof (status)) == sizeof (status))
145 			_exit(status);
146 
147 		if (waitpid(pid, &status, 0) == pid && WIFEXITED(status))
148 			_exit(WEXITSTATUS(status));
149 
150 		_exit(FMD_EXIT_ERROR);
151 	}
152 
153 	fmd.d_pid = getpid();
154 	(void) setsid();
155 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
156 	(void) chdir("/");
157 	(void) umask(022);
158 	(void) close(pfds[0]);
159 
160 	return (pfds[1]);
161 }
162 
163 static void
164 daemonize_fini(int fd)
165 {
166 	(void) close(fd);
167 
168 	if ((fd = open("/dev/null", O_RDWR)) >= 0) {
169 		(void) fcntl(fd, F_DUP2FD, STDIN_FILENO);
170 		(void) fcntl(fd, F_DUP2FD, STDOUT_FILENO);
171 		(void) fcntl(fd, F_DUP2FD, STDERR_FILENO);
172 		(void) close(fd);
173 	}
174 }
175 
176 static void
177 handler(int sig)
178 {
179 	if (fmd.d_signal == 0)
180 		fmd.d_signal = sig;
181 }
182 
183 static int
184 usage(const char *arg0, FILE *fp)
185 {
186 	(void) fprintf(fp,
187 	    "Usage: %s [-V] [-f file] [-o opt=val] [-R dir]\n", arg0);
188 
189 	return (FMD_EXIT_USAGE);
190 }
191 
192 int
193 main(int argc, char *argv[])
194 {
195 	const char *opt_f = NULL, *opt_R = NULL;
196 	const char optstr[] = "f:o:R:V";
197 	int c, pfd = -1, opt_V = 0;
198 	char *p;
199 
200 	struct sigaction act;
201 	sigset_t set;
202 
203 	/*
204 	 * Parse the command-line once to validate all options and retrieve
205 	 * any overrides for our configuration file and root directory.
206 	 */
207 	while ((c = getopt(argc, argv, optstr)) != EOF) {
208 		switch (c) {
209 		case 'f':
210 			opt_f = optarg;
211 			break;
212 		case 'o':
213 			break; /* handle -o below */
214 		case 'R':
215 			opt_R = optarg;
216 			break;
217 		case 'V':
218 			opt_V++;
219 			break;
220 		default:
221 			return (usage(argv[0], stderr));
222 		}
223 	}
224 
225 	if (optind < argc)
226 		return (usage(argv[0], stderr));
227 
228 	if (opt_V) {
229 #ifdef DEBUG
230 		(void) printf("%s: version %s (DEBUG)\n",
231 #else
232 		(void) printf("%s: version %s\n",
233 #endif
234 		    argv[0], _fmd_version);
235 		return (FMD_EXIT_SUCCESS);
236 	}
237 
238 	closefrom(STDERR_FILENO + 1);
239 	fmd_create(&fmd, argv[0], opt_R, opt_f);
240 
241 	/*
242 	 * Now that we've initialized our global state, parse the command-line
243 	 * again for any configuration options specified using -o and set them.
244 	 */
245 	for (optind = 1; (c = getopt(argc, argv, optstr)) != EOF; ) {
246 		if (c == 'o') {
247 			if ((p = strchr(optarg, '=')) == NULL) {
248 				(void) fprintf(stderr, "%s: failed to set "
249 				    "option -o %s: option requires value\n",
250 				    fmd.d_pname, optarg);
251 				return (FMD_EXIT_USAGE);
252 			}
253 
254 			*p++ = '\0'; /* strike out the delimiter */
255 
256 			if (p[0] == '"' && p[strlen(p) - 1] == '"') {
257 				p[strlen(p) - 1] = '\0';
258 				(void) fmd_stresc2chr(++p);
259 			}
260 
261 			if (fmd_conf_setprop(fmd.d_conf, optarg, p) != 0) {
262 				(void) fprintf(stderr,
263 				    "%s: failed to set option -o %s: %s\n",
264 				    fmd.d_pname, optarg, fmd_strerror(errno));
265 				return (FMD_EXIT_USAGE);
266 			}
267 		}
268 	}
269 
270 	if (fmd.d_fmd_debug & FMD_DBG_HELP) {
271 		fmd_help(&fmd);
272 		fmd_destroy(&fmd);
273 		return (FMD_EXIT_SUCCESS);
274 	}
275 
276 	/*
277 	 * Update the value of fmd.d_fg based on "fg" in case it changed.  We
278 	 * use this property to decide whether to daemonize below.
279 	 */
280 	(void) fmd_conf_getprop(fmd.d_conf, "fg", &fmd.d_fg);
281 
282 	/*
283 	 * Once we're done setting our global state up, set up signal handlers
284 	 * for ensuring orderly termination on SIGTERM.  If we are starting in
285 	 * the foreground, we also use the same handler for SIGINT and SIGHUP.
286 	 */
287 	(void) sigfillset(&set);
288 	(void) sigdelset(&set, SIGABRT); /* always unblocked for ASSERT() */
289 
290 	(void) sigfillset(&act.sa_mask);
291 	act.sa_handler = handler;
292 	act.sa_flags = 0;
293 
294 	(void) sigaction(SIGTERM, &act, NULL);
295 	(void) sigdelset(&set, SIGTERM);
296 
297 	if (fmd.d_fg) {
298 		(void) sigaction(SIGHUP, &act, NULL);
299 		(void) sigdelset(&set, SIGHUP);
300 		(void) sigaction(SIGINT, &act, NULL);
301 		(void) sigdelset(&set, SIGINT);
302 
303 		(void) sigdelset(&set, SIGTSTP);
304 		(void) sigdelset(&set, SIGTTIN);
305 		(void) sigdelset(&set, SIGTTOU);
306 
307 		(void) printf("%s: [ loading modules ... ", fmd.d_pname);
308 		(void) fflush(stdout);
309 	} else
310 		pfd = daemonize_init();
311 
312 	/*
313 	 * Prior to this point, we are single-threaded.  Once fmd_run() is
314 	 * called, we will be multi-threaded from this point on.  The daemon's
315 	 * main thread will wait at the end of this function for signals.
316 	 */
317 	fmd_run(&fmd, pfd);
318 
319 	if (fmd.d_fg) {
320 		(void) printf("done ]\n");
321 		(void) printf("%s: [ awaiting events ]\n", fmd.d_pname);
322 	} else
323 		daemonize_fini(pfd);
324 
325 	while (!fmd.d_signal)
326 		(void) sigsuspend(&set);
327 
328 	fmd_destroy(&fmd);
329 	return (FMD_EXIT_SUCCESS);
330 }
331