summaryrefslogtreecommitdiff
path: root/usr/src/cmd/hotplugd/hotplugd.c
blob: decd10e7b5085c0e6157da2d3e79742b7864437b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <strings.h>
#include <syslog.h>
#include <priv.h>
#include <wait.h>
#include <getopt.h>
#include <synch.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <libhotplug.h>
#include <libhotplug_impl.h>
#include "hotplugd_impl.h"

/*
 * Define long options for command line.
 */
static const struct option lopts[] = {
	{ "help",	no_argument,	0, '?' },
	{ "version",	no_argument,	0, 'V' },
	{ "debug",	no_argument,	0, 'd' },
	{ 0, 0, 0, 0 }
};

/*
 * Local functions.
 */
static void		usage(void);
static boolean_t	check_privileges(void);
static int		daemonize(void);
static void		init_signals(void);
static void		signal_handler(int signum);
static void		shutdown_daemon(void);

/*
 * Global variables.
 */
static char		*prog;
static char		version[] = "1.0";
static boolean_t	log_flag = B_FALSE;
static boolean_t	debug_flag = B_FALSE;
static boolean_t	exit_flag = B_FALSE;
static sema_t		signal_sem;

/*
 * main()
 *
 *	The hotplug daemon is designed to be a background daemon
 *	controlled by SMF.  So by default it will daemonize and
 *	do some coordination with its parent process in order to
 *	indicate proper success or failure back to SMF.  And all
 *	output will be sent to syslog.
 *
 *	But if given the '-d' command line option, it will instead
 *	run in the foreground in a standalone, debug mode.  Errors
 *	and additional debug messages will be printed to the controlling
 *	terminal instead of to syslog.
 */
int
main(int argc, char *argv[])
{
	int	opt;
	int	pfd;
	int	status;

	if ((prog = strrchr(argv[0], '/')) == NULL)
		prog = argv[0];
	else
		prog++;

	/* Check privileges */
	if (!check_privileges()) {
		(void) fprintf(stderr, "Insufficient privileges.  "
		    "(All privileges are required.)\n");
		return (-1);
	}

	/* Process options  */
	while ((opt = getopt_clip(argc, argv, "dV?", lopts, NULL)) != -1) {
		switch (opt) {
		case 'd':
			debug_flag = B_TRUE;
			break;
		case 'V':
			(void) printf("%s: Version %s\n", prog, version);
			return (0);
		default:
			if (optopt == '?') {
				usage();
				return (0);
			}
			(void) fprintf(stderr, "Unrecognized option '%c'.\n",
			    optopt);
			usage();
			return (-1);
		}
	}

	/* Initialize semaphore for daemon shutdown */
	if (sema_init(&signal_sem, 1, USYNC_THREAD, NULL) != 0)
		exit(EXIT_FAILURE);

	/* Initialize signal handling */
	init_signals();

	/* Daemonize, if not in DEBUG mode */
	if (!debug_flag)
		pfd = daemonize();

	/* Initialize door service */
	if (!door_server_init()) {
		if (!debug_flag) {
			status = EXIT_FAILURE;
			(void) write(pfd, &status, sizeof (status));
			(void) close(pfd);
		}
		exit(EXIT_FAILURE);
	}

	/* Daemon initialized */
	if (!debug_flag) {
		status = 0;
		(void) write(pfd, &status, sizeof (status));
		(void) close(pfd);
	}

	/* Note that daemon is running */
	log_info("hotplug daemon started.\n");

	/* Wait for shutdown signal */
	while (!exit_flag)
		(void) sema_wait(&signal_sem);

	shutdown_daemon();
	return (0);
}

/*
 * usage()
 *
 *	Print a brief usage synopsis for the command line options.
 */
static void
usage(void)
{
	(void) printf("Usage: %s [-d]\n", prog);
}

/*
 * check_privileges()
 *
 *	Check if the current process has enough privileges
 *	to run the daemon.  Note that all privileges are
 *	required in order for RCM interactions to work.
 */
static boolean_t
check_privileges(void)
{
	priv_set_t	*privset;
	boolean_t	rv = B_FALSE;

	if ((privset = priv_allocset()) != NULL) {
		if (getppriv(PRIV_EFFECTIVE, privset) == 0) {
			rv = priv_isfullset(privset);
		}
		priv_freeset(privset);
	}

	return (rv);
}

/*
 * daemonize()
 *
 *	Fork the daemon process into the background, and detach from
 *	the controlling terminal.  Setup a shared pipe that will later
 *	be used to report startup status to the parent process.
 */
static int
daemonize(void)
{
	int		status;
	int		pfds[2];
	pid_t		pid;
	sigset_t	set;
	sigset_t	oset;

	/*
	 * Temporarily block all signals.  They will remain blocked in
	 * the parent, but will be unblocked in the child once it has
	 * notified the parent of its startup status.
	 */
	(void) sigfillset(&set);
	(void) sigdelset(&set, SIGABRT);
	(void) sigprocmask(SIG_BLOCK, &set, &oset);

	/* Create the shared pipe */
	if (pipe(pfds) == -1) {
		log_err("Cannot create pipe (%s)\n", strerror(errno));
		exit(EXIT_FAILURE);
	}

	/* Fork the daemon process */
	if ((pid = fork()) == -1) {
		log_err("Cannot fork daemon process (%s)\n", strerror(errno));
		exit(EXIT_FAILURE);
	}

	/* Parent:  waits for exit status from child. */
	if (pid > 0) {
		(void) close(pfds[1]);
		if (read(pfds[0], &status, sizeof (status)) == sizeof (status))
			_exit(status);
		if ((waitpid(pid, &status, 0) == pid) && WIFEXITED(status))
			_exit(WEXITSTATUS(status));
		log_err("Failed to spawn daemon process.\n");
		_exit(EXIT_FAILURE);
	}

	/* Child continues... */

	(void) setsid();
	(void) chdir("/");
	(void) umask(CMASK);
	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
	(void) close(pfds[0]);

	/* Detach from controlling terminal */
	(void) close(0);
	(void) close(1);
	(void) close(2);
	(void) open("/dev/null", O_RDONLY);
	(void) open("/dev/null", O_WRONLY);
	(void) open("/dev/null", O_WRONLY);

	/* Use syslog for future messages */
	log_flag = B_TRUE;
	openlog(prog, LOG_PID, LOG_DAEMON);

	return (pfds[1]);
}

/*
 * init_signals()
 *
 *	Initialize signal handling.
 */
static void
init_signals(void)
{
	struct sigaction	act;
	sigset_t		set;

	(void) sigfillset(&set);
	(void) sigdelset(&set, SIGABRT);

	(void) sigfillset(&act.sa_mask);
	act.sa_handler = signal_handler;
	act.sa_flags = 0;

	(void) sigaction(SIGTERM, &act, NULL);
	(void) sigaction(SIGHUP, &act, NULL);
	(void) sigaction(SIGINT, &act, NULL);
	(void) sigaction(SIGPIPE, &act, NULL);

	(void) sigdelset(&set, SIGTERM);
	(void) sigdelset(&set, SIGHUP);
	(void) sigdelset(&set, SIGINT);
	(void) sigdelset(&set, SIGPIPE);
}

/*
 * signal_handler()
 *
 *	Most signals cause the hotplug daemon to shut down.
 *	Shutdown is triggered using a semaphore to wake up
 *	the main thread for a clean exit.
 *
 *	Except SIGPIPE is used to coordinate between the parent
 *	and child processes when the daemon first starts.
 */
static void
signal_handler(int signum)
{
	log_info("Received signal %d.\n", signum);

	switch (signum) {
	case 0:
	case SIGPIPE:
		break;
	default:
		exit_flag = B_TRUE;
		(void) sema_post(&signal_sem);
		break;
	}
}

/*
 * shutdown_daemon()
 *
 *	Perform a clean shutdown of the daemon.
 */
static void
shutdown_daemon(void)
{
	log_info("Hotplug daemon shutting down.\n");

	door_server_fini();

	if (log_flag)
		closelog();

	(void) sema_destroy(&signal_sem);
}

/*
 * log_err()
 *
 *	Display an error message.  Use syslog if in daemon
 *	mode, otherwise print to stderr when in debug mode.
 */
/*PRINTFLIKE1*/
void
log_err(char *fmt, ...)
{
	va_list	ap;

	va_start(ap, fmt);
	if (debug_flag || !log_flag)
		(void) vfprintf(stderr, fmt, ap);
	else
		vsyslog(LOG_ERR, fmt, ap);
	va_end(ap);
}

/*
 * log_info()
 *
 *	Display an information message.  Use syslog if in daemon
 *	mode, otherwise print to stdout when in debug mode.
 */
/*PRINTFLIKE1*/
void
log_info(char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	if (debug_flag || !log_flag)
		(void) vfprintf(stdout, fmt, ap);
	else
		vsyslog(LOG_INFO, fmt, ap);
	va_end(ap);
}

/*
 * dprintf()
 *
 *	Print a debug tracing statement.  Only works in debug
 *	mode, and always prints to stdout.
 */
/*PRINTFLIKE1*/
void
dprintf(char *fmt, ...)
{
	va_list	ap;

	if (debug_flag) {
		va_start(ap, fmt);
		(void) vprintf(fmt, ap);
		va_end(ap);
	}
}