xref: /netbsd-src/crypto/external/bsd/heimdal/dist/kcm/main.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: main.c,v 1.1.1.2 2014/04/24 12:45:27 pettai Exp $	*/
2 
3 /*
4  * Copyright (c) 1997-2002 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "kcm_locl.h"
37 
38 __RCSID("NetBSD");
39 
40 sig_atomic_t exit_flag = 0;
41 
42 krb5_context kcm_context = NULL;
43 
44 const char *service_name = "org.h5l.kcm";
45 
46 static RETSIGTYPE
47 sigterm(int sig)
48 {
49     exit_flag = 1;
50 }
51 
52 static RETSIGTYPE
53 sigusr1(int sig)
54 {
55     kcm_debug_ccache(kcm_context);
56 }
57 
58 static RETSIGTYPE
59 sigusr2(int sig)
60 {
61     kcm_debug_events(kcm_context);
62 }
63 
64 int
65 main(int argc, char **argv)
66 {
67     krb5_error_code ret;
68     setprogname(argv[0]);
69 
70     ret = krb5_init_context(&kcm_context);
71     if (ret) {
72 	errx (1, "krb5_init_context failed: %d", ret);
73 	return ret;
74     }
75 
76     kcm_configure(argc, argv);
77 
78 #ifdef HAVE_SIGACTION
79     {
80 	struct sigaction sa;
81 
82 	sa.sa_flags = 0;
83 	sa.sa_handler = sigterm;
84 	sigemptyset(&sa.sa_mask);
85 
86 	sigaction(SIGINT, &sa, NULL);
87 	sigaction(SIGTERM, &sa, NULL);
88 
89 	sa.sa_handler = sigusr1;
90 	sigaction(SIGUSR1, &sa, NULL);
91 
92 	sa.sa_handler = sigusr2;
93 	sigaction(SIGUSR2, &sa, NULL);
94 
95 	sa.sa_handler = SIG_IGN;
96 	sigaction(SIGPIPE, &sa, NULL);
97     }
98 #else
99     signal(SIGINT, sigterm);
100     signal(SIGTERM, sigterm);
101     signal(SIGUSR1, sigusr1);
102     signal(SIGUSR2, sigusr2);
103     signal(SIGPIPE, SIG_IGN);
104 #endif
105 #ifdef SUPPORT_DETACH
106     if (detach_from_console)
107 	daemon(0, 0);
108 #endif
109     pidfile(NULL);
110 
111     if (launchd_flag) {
112 	heim_sipc mach;
113 	heim_sipc_launchd_mach_init(service_name, kcm_service, NULL, &mach);
114     } else {
115 	heim_sipc un;
116 	heim_sipc_service_unix(service_name, kcm_service, NULL, &un);
117     }
118 
119     heim_ipc_main();
120 
121     krb5_free_context(kcm_context);
122     return 0;
123 }
124