forked from Exhar/otpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
279 lines (238 loc) · 6.56 KB
/
main.c
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
/*
* $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* For alternative licensing terms, contact licensing@tri-dsystems.com.
*
* Copyright 2005-2008 TRI-D Systems, Inc.
*/
#include "ident.h"
RCSID("$Id$")
#ifndef _POSIX_PTHREAD_SEMANTICS
#define _POSIX_PTHREAD_SEMANTICS /* Solaris sigwait() */
#endif
#include <fcntl.h>
#include <libgen.h> /* basename() */
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "extern.h"
#include "main.h"
extern char *optarg;
extern int optind, opterr, optopt;
pthread_attr_t attr_detached; /* create all threads detached */
char *opt_c; /* config file */
char *opt_u; /* user */
char *opt_p; /* plugin rp */
char *opt_d; /* log_level */
char *progname = "otpd";
/* initialize signal handling */
static void
sig_init(void)
{
sigset_t set;
struct sigaction sa;
/* block all signals */
(void) sigfillset(&set);
/* except the sync signals that result in undefined action if blocked */
(void) sigdelset(&set, SIGFPE);
(void) sigdelset(&set, SIGILL);
(void) sigdelset(&set, SIGSEGV);
(void) sigdelset(&set, SIGBUS);
(void) pthread_sigmask(SIG_BLOCK, &set, NULL);
/* ignore SIGPIPE; we want to receive EPIPE instead */
sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
(void) sigemptyset(&sa.sa_mask); /* not really needed */
(void) sigaction(SIGPIPE, &sa, NULL);
}
/*
* Become a daemon, adapted from APUE 2nd Ed.
* Keep fd open; it is our listening socket, opened before daemonizing
* for better error reporting. Use -1 to close all fd's.
*/
static void
daemonize(int listenfd)
{
pid_t pid;
int fd0, fd1, fd2;
int i;
#ifndef HAVE_CLOSEFROM
struct rlimit rl;
/* get max fd */
xgetrlimit(RLIMIT_NOFILE, &rl);
#endif
/* become a session leader to lose controlling tty */
if ((pid = fork()) == -1) {
mlog(LOG_CRIT, "%s: fork", __func__);
exit(1);
} else if (pid != 0) {
/* parent */
_exit(0);
}
/*
* first child
*/
xsetsid();
/* ensure future open()'s won't allocate controlling tty's */
if ((pid = fork()) == -1) {
mlog(LOG_CRIT, "%s: fork", __func__);
exit(1);
} else if (pid != 0) {
/* parent (first child) */
_exit(0);
}
/*
* second child
*/
/* change cwd to root to allow umount of current filesystem */
xchdir("/");
/* avoid listen socket using std* fd's */
if (listenfd == 0 || listenfd == 1 || listenfd == 2) {
mlog(LOG_CRIT, "%s: listen socket must be fd 3 or greater", __func__);
exit(1);
}
/* close all open fd's except passed in arg */
for (i = 0; i < listenfd; ++i)
(void) close(i);
#ifdef HAVE_CLOSEFROM
closefrom(listenfd + 1);
#else /* the sucky way */
if (rl.rlim_max == RLIM_INFINITY)
rl.rlim_max = 1024;
for (i = listenfd + 1; i < (int) rl.rlim_max; ++i)
(void) close(i);
#endif /* !HAVE_CLOSEFROM */
/* open fd 0,1,2 as /dev/null, to head off problems */
fd0 = open("/dev/null", O_RDWR);
fd1 = dup(0);
fd2 = dup(0);
/* sanity check */
if (fd0 != 0 || fd1 != 1 || fd2 != 2) {
mlog(LOG_CRIT, "%s: unexpected fd's %d %d %d", __func__, fd0, fd1, fd2);
exit(1);
}
/* initialize syslog */
mopenlog(basename(progname), LOG_PID|LOG_NDELAY);
}
/* print usage */
static void
usage(int ec)
{
fprintf(stderr,
"Usage: %s [options]\n"
" -h display this help message\n"
" -v display program version\n"
" -c <file> use config file <file>\n"
" -u <user> run as user <user> \n"
" -p <rp> listen to plugins at rendezvous point <rp> \n"
" -f run in foreground\n"
" -d <level> set debug level <level>\n"
" -D set max debug level\n",
progname);
exit(ec);
}
int
main(int argc, char *argv[])
{
int dodaemon = 1;
config_t *config;
progname = argv[0];
/*
* argument parsing
*/
{
int c;
while ((c = getopt(argc, argv, ":hvc:d:fDu:p:")) != -1) {
switch (c) {
case 'c': /* config file */
opt_c = optarg;
break;
case 'd': /* debug */
opt_d = optarg;
break;
case 'f': /* foreground */
dodaemon = 0;
break;
case 'D': /* max debug + foreground */
dodaemon = 0;
opt_d = "debug8";
break;
case 'u': /* user */
opt_u = optarg;
break;
case 'p': /* plugin rendezvous point */
opt_p = optarg;
break;
case 'h': /* help */
usage(0);
case 'v': /* version */
(void) fprintf(stderr, "%s version %s\n", basename(progname), VERSION);
exit(0);
case ':': /* missing arg */
usage(1);
case '?':
(void) fprintf(stderr, "%s: unknown option -%c\n", progname, optopt);
exit(1);
}
}
if (argc > optind)
usage(1);
} /* argument parsing */
config = config_init();
cardops_init();
sig_init();
if (config->state.mode == SMODE_LOCAL)
lock_init();
else
nonce_init();
if (dodaemon)
daemonize(config->s);
mlog(LOG_NOTICE, "%s %s starting", basename(progname), VERSION);
/* all threads should be detached */
(void) pthread_attr_init(&attr_detached);
(void) pthread_attr_setdetachstate(&attr_detached, PTHREAD_CREATE_DETACHED);
/* start failover threads */
{
gsmd_t *g = NULL;
pthread_t tid;
while ((g = gsmd_next(g)))
xpthread_create(&tid, &attr_detached, failover_thread, g);
}
#ifdef USE_SOCKET
/* start the accept thread */
{
pthread_t tid;
xpthread_create(&tid, &attr_detached, accept_thread, config);
}
#endif /* USE_SOCKET */
/* catch any signals and exit. TODO: terminate all threads cleanly */
{
sigset_t set;
/* unblock all signals */
(void) sigemptyset(&set);
(void) pthread_sigmask(SIG_SETMASK, &set, NULL);
for (;;)
(void) pause();
}
/*NOTREACHED*/
return 0;
}