Print this page
2831 svc.startd and svc.configd waste memory.
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/svc/configd/configd.c
+++ new/usr/src/cmd/svc/configd/configd.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 -#pragma ident "%Z%%M% %I% %E% SMI"
26 +/*
27 + * Copyright (c) 2012, Joyent, Inc. All rights reserved.
28 + */
27 29
28 30 #include <assert.h>
29 31 #include <door.h>
30 32 #include <errno.h>
31 33 #include <fcntl.h>
32 34 #include <limits.h>
33 35 #include <priv.h>
34 36 #include <procfs.h>
35 37 #include <pthread.h>
36 38 #include <signal.h>
37 39 #include <stdarg.h>
38 40 #include <stdio.h>
39 41 #include <stdio_ext.h>
40 42 #include <stdlib.h>
41 43 #include <string.h>
42 44 #include <syslog.h>
43 45 #include <sys/corectl.h>
44 46 #include <sys/resource.h>
45 47 #include <sys/stat.h>
46 48 #include <sys/wait.h>
47 49 #include <ucontext.h>
48 50 #include <unistd.h>
49 51
50 52 #include "configd.h"
51 53
52 54 /*
53 55 * This file manages the overall startup and shutdown of configd, as well
54 56 * as managing its door thread pool and per-thread datastructures.
55 57 *
56 58 * 1. Per-thread Datastructures
57 59 * -----------------------------
58 60 * Each configd thread has an associated thread_info_t which contains its
59 61 * current state. A pointer is kept to this in TSD, keyed by thread_info_key.
60 62 * The thread_info_ts for all threads in configd are kept on a single global
61 63 * list, thread_list. After creation, the state in the thread_info structure
62 64 * is only modified by the associated thread, so no locking is needed. A TSD
63 65 * destructor removes the thread_info from the global list and frees it at
64 66 * pthread_exit() time.
65 67 *
66 68 * Threads access their per-thread data using thread_self()
67 69 *
68 70 * The thread_list is protected by thread_lock, a leaf lock.
69 71 *
70 72 * 2. Door Thread Pool Management
71 73 * ------------------------------
72 74 * Whenever door_return(3door) returns from the kernel and there are no
73 75 * other configd threads waiting for requests, libdoor automatically
74 76 * invokes a function registered with door_server_create(), to request a new
75 77 * door server thread. The default function just creates a thread that calls
76 78 * door_return(3door). Unfortunately, since it can take a while for the new
77 79 * thread to *get* to door_return(3door), a stream of requests can cause a
78 80 * large number of threads to be created, even though they aren't all needed.
79 81 *
80 82 * In our callback, new_server_needed(), we limit ourself to two new threads
81 83 * at a time -- this logic is handled in reserve_new_thread(). This keeps
82 84 * us from creating an absurd number of threads in response to peaking load.
83 85 */
84 86 static pthread_key_t thread_info_key;
85 87 static pthread_attr_t thread_attr;
86 88
87 89 static pthread_mutex_t thread_lock = PTHREAD_MUTEX_INITIALIZER;
88 90 int num_started; /* number actually running */
89 91 int num_servers; /* number in-progress or running */
90 92 static uu_list_pool_t *thread_pool;
91 93 uu_list_t *thread_list;
92 94
93 95 static thread_info_t main_thread_info;
94 96
95 97 static int finished;
96 98
97 99 static pid_t privileged_pid = 0;
98 100 static int privileged_psinfo_fd = -1;
99 101
100 102 static int privileged_user = 0;
101 103
↓ open down ↓ |
65 lines elided |
↑ open up ↑ |
102 104 static priv_set_t *privileged_privs;
103 105
104 106 static int log_to_syslog = 0;
105 107
106 108 int is_main_repository = 1;
107 109
108 110 int max_repository_backups = 4;
109 111
110 112 #define CONFIGD_MAX_FDS 262144
111 113
114 +const char *
115 +_umem_options_init(void)
116 +{
117 + /*
118 + * Like svc.startd, we set our UMEM_OPTIONS to indicate that we do not
119 + * wish to have per-CPU magazines to reduce our memory footprint. And
120 + * as with svc.startd, if svc.configd is so MT-hot that this becomes a
121 + * scalability problem, there are deeper issues...
122 + */
123 + return ("nomagazines"); /* UMEM_OPTIONS setting */
124 +}
125 +
112 126 /*
113 127 * Thanks, Mike
114 128 */
115 129 void
116 130 abort_handler(int sig, siginfo_t *sip, ucontext_t *ucp)
117 131 {
118 132 struct sigaction act;
119 133
120 134 (void) sigemptyset(&act.sa_mask);
121 135 act.sa_handler = SIG_DFL;
122 136 act.sa_flags = 0;
123 137 (void) sigaction(sig, &act, NULL);
124 138
125 139 (void) printstack(2);
126 140
127 141 if (sip != NULL && SI_FROMUSER(sip))
128 142 (void) pthread_kill(pthread_self(), sig);
129 143 (void) sigfillset(&ucp->uc_sigmask);
130 144 (void) sigdelset(&ucp->uc_sigmask, sig);
131 145 ucp->uc_flags |= UC_SIGMASK;
132 146 (void) setcontext(ucp);
133 147 }
134 148
135 149 /*
136 150 * Don't want to have more than a couple thread creates outstanding
137 151 */
138 152 static int
139 153 reserve_new_thread(void)
140 154 {
141 155 (void) pthread_mutex_lock(&thread_lock);
142 156 assert(num_started >= 0);
143 157 if (num_servers > num_started + 1) {
144 158 (void) pthread_mutex_unlock(&thread_lock);
145 159 return (0);
146 160 }
147 161 ++num_servers;
148 162 (void) pthread_mutex_unlock(&thread_lock);
149 163 return (1);
150 164 }
151 165
152 166 static void
153 167 thread_info_free(thread_info_t *ti)
154 168 {
155 169 uu_list_node_fini(ti, &ti->ti_node, thread_pool);
156 170 if (ti->ti_ucred != NULL)
157 171 uu_free(ti->ti_ucred);
158 172 uu_free(ti);
159 173 }
160 174
161 175 static void
162 176 thread_exiting(void *arg)
163 177 {
164 178 thread_info_t *ti = arg;
165 179
166 180 if (ti != NULL)
167 181 log_enter(&ti->ti_log);
168 182
169 183 (void) pthread_mutex_lock(&thread_lock);
170 184 if (ti != NULL) {
171 185 num_started--;
172 186 uu_list_remove(thread_list, ti);
173 187 }
174 188 assert(num_servers > 0);
175 189 --num_servers;
176 190
177 191 if (num_servers == 0) {
178 192 configd_critical("no door server threads\n");
179 193 abort();
180 194 }
181 195 (void) pthread_mutex_unlock(&thread_lock);
182 196
183 197 if (ti != NULL && ti != &main_thread_info)
184 198 thread_info_free(ti);
185 199 }
186 200
187 201 void
188 202 thread_newstate(thread_info_t *ti, thread_state_t newstate)
189 203 {
190 204 ti->ti_ucred_read = 0; /* invalidate cached ucred */
191 205 if (newstate != ti->ti_state) {
192 206 ti->ti_prev_state = ti->ti_state;
193 207 ti->ti_state = newstate;
194 208 ti->ti_lastchange = gethrtime();
195 209 }
196 210 }
197 211
198 212 thread_info_t *
199 213 thread_self(void)
200 214 {
201 215 return (pthread_getspecific(thread_info_key));
202 216 }
203 217
204 218 /*
205 219 * get_ucred() returns NULL if it was unable to get the credential
206 220 * information.
207 221 */
208 222 ucred_t *
209 223 get_ucred(void)
210 224 {
211 225 thread_info_t *ti = thread_self();
212 226 ucred_t **ret = &ti->ti_ucred;
213 227
214 228 if (ti->ti_ucred_read)
215 229 return (*ret); /* cached value */
216 230
217 231 if (door_ucred(ret) != 0)
218 232 return (NULL);
219 233 ti->ti_ucred_read = 1;
220 234
221 235 return (*ret);
222 236 }
223 237
224 238 int
225 239 ucred_is_privileged(ucred_t *uc)
226 240 {
227 241 const priv_set_t *ps;
228 242
229 243 if ((ps = ucred_getprivset(uc, PRIV_EFFECTIVE)) != NULL) {
230 244 if (priv_isfullset(ps))
231 245 return (1); /* process has all privs */
232 246
233 247 if (privileged_privs != NULL &&
234 248 priv_issubset(privileged_privs, ps))
235 249 return (1); /* process has zone privs */
236 250 }
237 251
238 252 return (0);
239 253 }
240 254
241 255 /*
242 256 * The purpose of this function is to get the audit session data for use in
243 257 * generating SMF audit events. We use a single audit session per client.
244 258 *
245 259 * get_audit_session() may return NULL. It is legal to use a NULL pointer
246 260 * in subsequent calls to adt_* functions.
247 261 */
248 262 adt_session_data_t *
249 263 get_audit_session(void)
250 264 {
251 265 thread_info_t *ti = thread_self();
252 266
253 267 return (ti->ti_active_client->rc_adt_session);
254 268 }
255 269
256 270 static void *
257 271 thread_start(void *arg)
258 272 {
259 273 thread_info_t *ti = arg;
260 274
261 275 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
262 276
263 277 (void) pthread_mutex_lock(&thread_lock);
264 278 num_started++;
265 279 (void) uu_list_insert_after(thread_list, uu_list_last(thread_list),
266 280 ti);
267 281 (void) pthread_mutex_unlock(&thread_lock);
268 282 (void) pthread_setspecific(thread_info_key, ti);
269 283
270 284 thread_newstate(ti, TI_DOOR_RETURN);
271 285
272 286 /*
273 287 * Start handling door calls
274 288 */
275 289 (void) door_return(NULL, 0, NULL, 0);
276 290 return (arg);
277 291 }
278 292
279 293 static void
280 294 new_thread_needed(door_info_t *dip)
281 295 {
282 296 thread_info_t *ti;
283 297
284 298 sigset_t new, old;
285 299
286 300 assert(dip == NULL);
287 301
288 302 if (!reserve_new_thread())
289 303 return;
290 304
291 305 if ((ti = uu_zalloc(sizeof (*ti))) == NULL)
292 306 goto fail;
293 307
294 308 uu_list_node_init(ti, &ti->ti_node, thread_pool);
295 309 ti->ti_state = TI_CREATED;
296 310 ti->ti_prev_state = TI_CREATED;
297 311
298 312 if ((ti->ti_ucred = uu_zalloc(ucred_size())) == NULL)
299 313 goto fail;
300 314
301 315 (void) sigfillset(&new);
302 316 (void) pthread_sigmask(SIG_SETMASK, &new, &old);
303 317 if ((errno = pthread_create(&ti->ti_thread, &thread_attr, thread_start,
304 318 ti)) != 0) {
305 319 (void) pthread_sigmask(SIG_SETMASK, &old, NULL);
306 320 goto fail;
307 321 }
308 322
309 323 (void) pthread_sigmask(SIG_SETMASK, &old, NULL);
310 324 return;
311 325
312 326 fail:
313 327 /*
314 328 * Since the thread_info structure was never linked onto the
315 329 * thread list, thread_exiting() can't handle the cleanup.
316 330 */
317 331 thread_exiting(NULL);
318 332 if (ti != NULL)
319 333 thread_info_free(ti);
320 334 }
321 335
322 336 int
323 337 create_connection(ucred_t *uc, repository_door_request_t *rp,
324 338 size_t rp_size, int *out_fd)
325 339 {
326 340 int flags;
327 341 int privileged = 0;
328 342 uint32_t debugflags = 0;
329 343 psinfo_t info;
330 344
331 345 if (privileged_pid != 0) {
332 346 /*
333 347 * in privileged pid mode, we only allow connections from
334 348 * our original parent -- the psinfo read verifies that
335 349 * it is the same process which we started with.
336 350 */
337 351 if (ucred_getpid(uc) != privileged_pid ||
338 352 read(privileged_psinfo_fd, &info, sizeof (info)) !=
339 353 sizeof (info))
340 354 return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED);
341 355
342 356 privileged = 1; /* he gets full privileges */
343 357 } else if (privileged_user != 0) {
344 358 /*
345 359 * in privileged user mode, only one particular user is
346 360 * allowed to connect to us, and he can do anything.
347 361 */
348 362 if (ucred_geteuid(uc) != privileged_user)
349 363 return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED);
350 364
351 365 privileged = 1;
352 366 }
353 367
354 368 /*
355 369 * Check that rp, of size rp_size, is large enough to
356 370 * contain field 'f'. If so, write the value into *out, and return 1.
357 371 * Otherwise, return 0.
358 372 */
359 373 #define GET_ARG(rp, rp_size, f, out) \
360 374 (((rp_size) >= offsetofend(repository_door_request_t, f)) ? \
361 375 ((*(out) = (rp)->f), 1) : 0)
362 376
363 377 if (!GET_ARG(rp, rp_size, rdr_flags, &flags))
364 378 return (REPOSITORY_DOOR_FAIL_BAD_REQUEST);
365 379
366 380 #if (REPOSITORY_DOOR_FLAG_ALL != REPOSITORY_DOOR_FLAG_DEBUG)
367 381 #error Need to update flag checks
368 382 #endif
369 383
370 384 if (flags & ~REPOSITORY_DOOR_FLAG_ALL)
371 385 return (REPOSITORY_DOOR_FAIL_BAD_FLAG);
372 386
373 387 if (flags & REPOSITORY_DOOR_FLAG_DEBUG)
374 388 if (!GET_ARG(rp, rp_size, rdr_debug, &debugflags))
375 389 return (REPOSITORY_DOOR_FAIL_BAD_REQUEST);
376 390 #undef GET_ARG
377 391
378 392 return (create_client(ucred_getpid(uc), debugflags, privileged,
379 393 out_fd));
380 394 }
381 395
382 396 void
383 397 configd_vlog(int severity, const char *prefix, const char *message,
384 398 va_list args)
385 399 {
386 400 if (log_to_syslog)
387 401 vsyslog(severity, message, args);
388 402 else {
389 403 flockfile(stderr);
390 404 if (prefix != NULL)
391 405 (void) fprintf(stderr, "%s", prefix);
392 406 (void) vfprintf(stderr, message, args);
393 407 if (message[0] == 0 || message[strlen(message) - 1] != '\n')
394 408 (void) fprintf(stderr, "\n");
395 409 funlockfile(stderr);
396 410 }
397 411 }
398 412
399 413 void
400 414 configd_vcritical(const char *message, va_list args)
401 415 {
402 416 configd_vlog(LOG_CRIT, "svc.configd: Fatal error: ", message, args);
403 417 }
404 418
405 419 void
406 420 configd_critical(const char *message, ...)
407 421 {
408 422 va_list args;
409 423 va_start(args, message);
410 424 configd_vcritical(message, args);
411 425 va_end(args);
412 426 }
413 427
414 428 void
415 429 configd_info(const char *message, ...)
416 430 {
417 431 va_list args;
418 432 va_start(args, message);
419 433 configd_vlog(LOG_INFO, "svc.configd: ", message, args);
420 434 va_end(args);
421 435 }
422 436
423 437 static void
424 438 usage(const char *prog, int ret)
425 439 {
426 440 (void) fprintf(stderr,
427 441 "usage: %s [-np] [-d door_path] [-r repository_path]\n"
428 442 " [-t nonpersist_repository]\n", prog);
429 443 exit(ret);
430 444 }
431 445
432 446 /*ARGSUSED*/
433 447 static void
434 448 handler(int sig, siginfo_t *info, void *data)
435 449 {
436 450 finished = 1;
437 451 }
438 452
439 453 static int pipe_fd = -1;
440 454
441 455 static int
442 456 daemonize_start(void)
443 457 {
444 458 char data;
445 459 int status;
446 460
447 461 int filedes[2];
448 462 pid_t pid;
449 463
450 464 (void) close(0);
451 465 (void) dup2(2, 1); /* stderr only */
452 466
453 467 if (pipe(filedes) < 0)
454 468 return (-1);
455 469
456 470 if ((pid = fork1()) < 0)
457 471 return (-1);
458 472
459 473 if (pid != 0) {
460 474 /*
461 475 * parent
462 476 */
463 477 struct sigaction act;
464 478
465 479 act.sa_sigaction = SIG_DFL;
466 480 (void) sigemptyset(&act.sa_mask);
467 481 act.sa_flags = 0;
468 482
469 483 (void) sigaction(SIGPIPE, &act, NULL); /* ignore SIGPIPE */
470 484
471 485 (void) close(filedes[1]);
472 486 if (read(filedes[0], &data, 1) == 1) {
473 487 /* presume success */
474 488 _exit(CONFIGD_EXIT_OKAY);
475 489 }
476 490
477 491 status = -1;
478 492 (void) wait4(pid, &status, 0, NULL);
479 493 if (WIFEXITED(status))
480 494 _exit(WEXITSTATUS(status));
481 495 else
482 496 _exit(-1);
483 497 }
484 498
485 499 /*
486 500 * child
487 501 */
488 502 pipe_fd = filedes[1];
489 503 (void) close(filedes[0]);
490 504
491 505 /*
492 506 * generic Unix setup
493 507 */
494 508 (void) setsid();
495 509 (void) umask(0077);
496 510
497 511 return (0);
498 512 }
499 513
500 514 static void
501 515 daemonize_ready(void)
502 516 {
503 517 char data = '\0';
504 518
505 519 /*
506 520 * wake the parent
507 521 */
508 522 (void) write(pipe_fd, &data, 1);
509 523 (void) close(pipe_fd);
510 524 }
511 525
512 526 const char *
513 527 regularize_path(const char *dir, const char *base, char *tmpbuf)
514 528 {
515 529 if (base == NULL)
516 530 return (NULL);
517 531 if (base[0] == '/')
518 532 return (base);
519 533
520 534 if (snprintf(tmpbuf, PATH_MAX, "%s/%s", dir, base) >= PATH_MAX) {
521 535 (void) fprintf(stderr, "svc.configd: %s/%s: path too long\n",
522 536 dir, base);
523 537 exit(CONFIGD_EXIT_BAD_ARGS);
524 538 }
525 539
526 540 return (tmpbuf);
527 541 }
528 542
529 543 int
530 544 main(int argc, char *argv[])
531 545 {
532 546 thread_info_t *ti = &main_thread_info;
533 547
534 548 char pidpath[sizeof ("/proc/" "/psinfo") + 10];
535 549
536 550 struct rlimit fd_new;
537 551
538 552 const char *endptr;
539 553 sigset_t myset;
540 554 int c;
541 555 int ret;
542 556 int fd;
543 557
544 558 char curdir[PATH_MAX];
545 559 char dbtmp[PATH_MAX];
546 560 char npdbtmp[PATH_MAX];
547 561 char doortmp[PATH_MAX];
548 562
549 563 const char *dbpath = NULL;
550 564 const char *npdbpath = NULL;
551 565 const char *doorpath = REPOSITORY_DOOR_NAME;
552 566 struct sigaction act;
553 567
554 568 int daemonize = 1; /* default to daemonizing */
555 569 int have_npdb = 1;
556 570
557 571 closefrom(3); /* get rid of extraneous fds */
558 572
559 573 if (getcwd(curdir, sizeof (curdir)) == NULL) {
560 574 (void) fprintf(stderr,
561 575 "%s: unable to get current directory: %s\n",
562 576 argv[0], strerror(errno));
563 577 exit(CONFIGD_EXIT_INIT_FAILED);
564 578 }
565 579
566 580 while ((c = getopt(argc, argv, "Dnpd:r:t:")) != -1) {
567 581 switch (c) {
568 582 case 'n':
569 583 daemonize = 0;
570 584 break;
571 585 case 'd':
572 586 doorpath = regularize_path(curdir, optarg, doortmp);
573 587 have_npdb = 0; /* default to no non-persist */
574 588 break;
575 589 case 'p':
576 590 log_to_syslog = 0; /* don't use syslog */
577 591
578 592 /*
579 593 * If our parent exits while we're opening its /proc
580 594 * psinfo, we're vulnerable to a pid wrapping. To
581 595 * protect against that, re-check our ppid after
582 596 * opening it.
583 597 */
584 598 privileged_pid = getppid();
585 599 (void) snprintf(pidpath, sizeof (pidpath),
586 600 "/proc/%d/psinfo", privileged_pid);
587 601 if ((fd = open(pidpath, O_RDONLY)) < 0 ||
588 602 getppid() != privileged_pid) {
589 603 (void) fprintf(stderr,
590 604 "%s: unable to get parent info\n", argv[0]);
591 605 exit(CONFIGD_EXIT_BAD_ARGS);
592 606 }
593 607 privileged_psinfo_fd = fd;
594 608 break;
595 609 case 'r':
596 610 dbpath = regularize_path(curdir, optarg, dbtmp);
597 611 is_main_repository = 0;
598 612 break;
599 613 case 't':
600 614 npdbpath = regularize_path(curdir, optarg, npdbtmp);
601 615 is_main_repository = 0;
602 616 break;
603 617 default:
604 618 usage(argv[0], CONFIGD_EXIT_BAD_ARGS);
605 619 break;
606 620 }
607 621 }
608 622
609 623 /*
610 624 * If we're not running as root, allow our euid full access, and
611 625 * everyone else no access.
612 626 */
613 627 if (privileged_pid == 0 && geteuid() != 0) {
614 628 privileged_user = geteuid();
615 629 }
616 630
617 631 privileged_privs = priv_str_to_set("zone", "", &endptr);
618 632 if (endptr != NULL && privileged_privs != NULL) {
619 633 priv_freeset(privileged_privs);
620 634 privileged_privs = NULL;
621 635 }
622 636
623 637 openlog("svc.configd", LOG_PID | LOG_CONS, LOG_DAEMON);
624 638 (void) setlogmask(LOG_UPTO(LOG_NOTICE));
625 639
626 640 /*
627 641 * if a non-persist db is specified, always enable it
628 642 */
629 643 if (npdbpath)
630 644 have_npdb = 1;
631 645
632 646 if (optind != argc)
633 647 usage(argv[0], CONFIGD_EXIT_BAD_ARGS);
634 648
635 649 if (daemonize) {
636 650 if (getuid() == 0)
637 651 (void) chdir("/");
638 652 if (daemonize_start() < 0) {
639 653 (void) perror("unable to daemonize");
640 654 exit(CONFIGD_EXIT_INIT_FAILED);
641 655 }
642 656 }
643 657 if (getuid() == 0)
644 658 (void) core_set_process_path(CONFIGD_CORE,
645 659 strlen(CONFIGD_CORE) + 1, getpid());
646 660
647 661 /*
648 662 * this should be enabled once we can drop privileges and still get
649 663 * a core dump.
650 664 */
651 665 #if 0
652 666 /* turn off basic privileges we do not need */
653 667 (void) priv_set(PRIV_OFF, PRIV_PERMITTED, PRIV_FILE_LINK_ANY,
654 668 PRIV_PROC_EXEC, PRIV_PROC_FORK, PRIV_PROC_SESSION, NULL);
655 669 #endif
656 670
657 671 /* not that we can exec, but to be safe, shut them all off... */
658 672 (void) priv_set(PRIV_SET, PRIV_INHERITABLE, NULL);
659 673
660 674 (void) sigfillset(&act.sa_mask);
661 675
662 676 /* signals to ignore */
663 677 act.sa_sigaction = SIG_IGN;
664 678 act.sa_flags = 0;
665 679 (void) sigaction(SIGPIPE, &act, NULL);
666 680 (void) sigaction(SIGALRM, &act, NULL);
667 681 (void) sigaction(SIGUSR1, &act, NULL);
668 682 (void) sigaction(SIGUSR2, &act, NULL);
669 683 (void) sigaction(SIGPOLL, &act, NULL);
670 684
671 685 /* signals to abort on */
672 686 act.sa_sigaction = (void (*)(int, siginfo_t *, void *))&abort_handler;
673 687 act.sa_flags = SA_SIGINFO;
674 688
675 689 (void) sigaction(SIGABRT, &act, NULL);
676 690
677 691 /* signals to handle */
678 692 act.sa_sigaction = &handler;
679 693 act.sa_flags = SA_SIGINFO;
680 694
681 695 (void) sigaction(SIGHUP, &act, NULL);
682 696 (void) sigaction(SIGINT, &act, NULL);
683 697 (void) sigaction(SIGTERM, &act, NULL);
684 698
685 699 (void) sigemptyset(&myset);
686 700 (void) sigaddset(&myset, SIGHUP);
687 701 (void) sigaddset(&myset, SIGINT);
688 702 (void) sigaddset(&myset, SIGTERM);
689 703
690 704 if ((errno = pthread_attr_init(&thread_attr)) != 0) {
691 705 (void) perror("initializing");
692 706 exit(CONFIGD_EXIT_INIT_FAILED);
693 707 }
694 708
695 709 /*
696 710 * Set the hard and soft limits to CONFIGD_MAX_FDS.
697 711 */
698 712 fd_new.rlim_max = fd_new.rlim_cur = CONFIGD_MAX_FDS;
699 713 (void) setrlimit(RLIMIT_NOFILE, &fd_new);
700 714
701 715 #ifndef NATIVE_BUILD /* Allow building on snv_38 and earlier; remove later. */
702 716 (void) enable_extended_FILE_stdio(-1, -1);
703 717 #endif
704 718
705 719 if ((ret = backend_init(dbpath, npdbpath, have_npdb)) !=
706 720 CONFIGD_EXIT_OKAY)
707 721 exit(ret);
708 722
709 723 if (!client_init())
710 724 exit(CONFIGD_EXIT_INIT_FAILED);
711 725
712 726 if (!rc_node_init())
713 727 exit(CONFIGD_EXIT_INIT_FAILED);
714 728
715 729 (void) pthread_attr_setdetachstate(&thread_attr,
716 730 PTHREAD_CREATE_DETACHED);
717 731 (void) pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM);
718 732
719 733 if ((errno = pthread_key_create(&thread_info_key,
720 734 thread_exiting)) != 0) {
721 735 perror("pthread_key_create");
722 736 exit(CONFIGD_EXIT_INIT_FAILED);
723 737 }
724 738
725 739 if ((thread_pool = uu_list_pool_create("thread_pool",
726 740 sizeof (thread_info_t), offsetof(thread_info_t, ti_node),
727 741 NULL, UU_LIST_POOL_DEBUG)) == NULL) {
728 742 configd_critical("uu_list_pool_create: %s\n",
729 743 uu_strerror(uu_error()));
730 744 exit(CONFIGD_EXIT_INIT_FAILED);
731 745 }
732 746
733 747 if ((thread_list = uu_list_create(thread_pool, NULL, 0)) == NULL) {
734 748 configd_critical("uu_list_create: %s\n",
735 749 uu_strerror(uu_error()));
736 750 exit(CONFIGD_EXIT_INIT_FAILED);
737 751 }
738 752
739 753 (void) memset(ti, '\0', sizeof (*ti));
740 754 uu_list_node_init(ti, &ti->ti_node, thread_pool);
741 755 (void) uu_list_insert_before(thread_list, uu_list_first(thread_list),
742 756 ti);
743 757
744 758 ti->ti_thread = pthread_self();
745 759 ti->ti_state = TI_SIGNAL_WAIT;
746 760 ti->ti_prev_state = TI_SIGNAL_WAIT;
747 761
748 762 (void) pthread_setspecific(thread_info_key, ti);
749 763
750 764 (void) door_server_create(new_thread_needed);
751 765
752 766 if (!setup_main_door(doorpath)) {
753 767 configd_critical("Setting up main door failed.\n");
754 768 exit(CONFIGD_EXIT_DOOR_INIT_FAILED);
755 769 }
756 770
757 771 if (daemonize)
758 772 daemonize_ready();
759 773
760 774 (void) pthread_sigmask(SIG_BLOCK, &myset, NULL);
761 775 while (!finished) {
762 776 int sig = sigwait(&myset);
763 777 if (sig > 0) {
764 778 break;
765 779 }
766 780 }
767 781
768 782 backend_fini();
769 783
770 784 return (CONFIGD_EXIT_OKAY);
771 785 }
↓ open down ↓ |
650 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX