There are no available options for this view.

Parent Directory Parent Directory | Revision <a href="/cvs/aolserver/aolserver/nsd/log.c#A_Log">Log</a> Revision <a href="/cvs/aolserver/aolserver/nsd/log.c#A_Log">Log</a>

Revision 1.20 - (show annotations) (download) (as text)
Mon Jul 18 23:32:53 2005 UTC (12 years, 5 months ago) by jgdavidson
Branch: MAIN
CVS Tags: aolserver_v45_r0, aolserver_v45_r2_rc0, HEAD
Branch point for: aolserver_v45_r1, aolserver_v45_r2, aolserver_v45_bp
Changes since 1.19: +33 -86 lines
File MIME type: text/x-chdr
Integrated Unix and Win32 code for <a href="/cvs/aolserver/aolserver/nsd/sock.c#A_Ns_SockListenEx">Ns_SockListenEx</a>, <a href="/cvs/aolserver/aolserver/nsd/fd.c#A_ns_sockpair">ns_sockpair</a>, and <a href="/cvs/aolserver/aolserver/nsd/fd.c#A_ns_pipe">ns_pipe</a> and removed pthread_kill_other_threads_np call.
1 /*
2 * The contents of this file are subject to the AOLserver Public License
3 * Version 1.1 (the "License"); you may not use this file except in
4 * compliance with the License. You may obtain a copy of the License at
5 * http://aolserver.com/.
6 *
7 * Software distributed under the License is distributed on an "AS IS"
8 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9 * the License for the specific language governing rights and limitations
10 * under the License.
11 *
12 * The Original Code is AOLserver Code and related documentation
13 * distributed by AOL.
14 *
15 * The Initial Developer of the Original Code is America Online,
16 * Inc. Portions created by AOL are Copyright (C) 1999 America Online,
17 * Inc. All Rights Reserved.
18 *
19 * Alternatively, the contents of this file may be used under the terms
20 * of the GNU General Public License (the "GPL"), in which case the
21 * provisions of GPL are applicable instead of those above. If you wish
22 * to allow use of your version of this file only under the terms of the
23 * GPL and not to allow others to use your version of this file under the
24 * License, indicate your decision by deleting the provisions above and
25 * replace them with the notice and other provisions required by the GPL.
26 * If you do not delete the provisions above, a recipient may use your
27 * version of this file under either the License or the GPL.
28 */
29
30
31 /*
32 * unix.c --
33 *
34 * Unix specific routines.
35 */
36
37 static const char *RCSID = "@(#) $Header: /cvsroot-fuse/aolserver/aolserver/nsd/unix.c,v 1.20 2005/07/18 23:32:53 jgdavidson Exp $, compiled: " __DATE__ " " __TIME__;
38
39 #include "nsd.h"
40 #include <pwd.h>
41 #include <grp.h>
42
43 static Ns_Mutex lock;
44 static void <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_Abort">Abort</a>(int signal);
45
46
47 /*
48 *----------------------------------------------------------------------
49 *
50 * <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_NsBlockSignals">NsBlockSignals</a> --
51 *
52 * Block signals at startup.
53 *
54 * Results:
55 * None.
56 *
57 * Side effects:
58 * Signals will be pending until <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_NsHandleSignals">NsHandleSignals</a>.
59 *
60 *----------------------------------------------------------------------
61 */
62
63 void
64 <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_NsBlockSignals">NsBlockSignals</a>(int debug)
65 {
66 sigset_t set;
67
68 /*
69 * Block SIGHUP, SIGPIPE, SIGTERM, and SIGINT. This mask is
70 * inherited by all subsequent threads so that only this
71 * thread will catch the signals in the sigwait() loop below.
72 * Unfortunately this makes it impossible to <a href="/cvs/aolserver/aolserver/nsd/nswin32.c#A_kill">kill</a> the
73 * server with a signal other than SIGKILL until startup
74 * is complete.
75 */
76
77 sigemptyset(&set);
78 sigaddset(&set, SIGPIPE);
79 sigaddset(&set, SIGTERM);
80 sigaddset(&set, SIGHUP);
81 if (!nsconf.debug) {
82 /* NB: Don't block SIGINT in debug mode for Solaris dbx. */
83 sigaddset(&set, SIGINT);
84 }
85 ns_sigmask(SIG_BLOCK, &set, NULL);
86
87 /*
88 * Make sure "synchronous" signals (those generated by execution
89 * errors like SIGSEGV or SIGILL which get delivered to the thread
90 * that caused them) have an appropriate handler installed.
91 */
92
93 ns_signal(SIGILL, <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_Abort">Abort</a>);
94 ns_signal(SIGTRAP, <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_Abort">Abort</a>);
95 ns_signal(SIGBUS, <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_Abort">Abort</a>);
96 ns_signal(SIGSEGV, <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_Abort">Abort</a>);
97 ns_signal(SIGFPE, <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_Abort">Abort</a>);
98 }
99
100
101 /*
102 *----------------------------------------------------------------------
103 * <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_NsRestoreSignals">NsRestoreSignals</a> --
104 *
105 * Restore all signals to their default value.
106 *
107 * Results:
108 * None.
109 *
110 * Side effects:
111 * A new thread will be created.
112 *
113 *----------------------------------------------------------------------
114 */
115
116 void
117 <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_NsRestoreSignals">NsRestoreSignals</a>(void)
118 {
119 sigset_t set;
120 int sig;
121
122 for (sig = 1; sig < NSIG; ++sig) {
123 ns_signal(sig, (void (*)(int)) SIG_DFL);
124 }
125 sigfillset(&set);
126 ns_sigmask(SIG_UNBLOCK, &set, NULL);
127 }
128
129
130 /*
131 *----------------------------------------------------------------------
132 *
133 * <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_NsHandleSignals">NsHandleSignals</a> --
134 *
135 * Loop forever processing signals until a term signal
136 * is received.
137 *
138 * Results:
139 * None.
140 *
141 * Side effects:
142 * HUP callbacks may be called.
143 *
144 *----------------------------------------------------------------------
145 */
146
147 void
148 <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_NsHandleSignals">NsHandleSignals</a>(void)
149 {
150 sigset_t set;
151 int err, sig;
152
153 /*
154 * Wait endlessly for trigger wakeups.
155 */
156
157 sigemptyset(&set);
158 sigaddset(&set, SIGTERM);
159 sigaddset(&set, SIGHUP);
160 if (!nsconf.debug) {
161 sigaddset(&set, SIGINT);
162 }
163 do {
164 do {
165 err = ns_sigwait(&set, &sig);
166 } while (err == EINTR);
167 if (err != 0) {
168 <a href="/cvs/aolserver/aolserver/nsd/log.c#A_Ns_Fatal">Ns_Fatal</a>("signal: ns_sigwait failed: %s", strerror(errno));
169 }
170 if (sig == SIGHUP) {
171 <a href="/cvs/aolserver/aolserver/nsd/callbacks.c#A_NsRunSignalProcs">NsRunSignalProcs</a>();
172 }
173 } while (sig == SIGHUP);
174
175 /*
176 * Unblock the signals and exit.
177 */
178
179 ns_sigmask(SIG_UNBLOCK, &set, NULL);
180 }
181
182
183 /*
184 *----------------------------------------------------------------------
185 *
186 * <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_NsSendSignal">NsSendSignal</a> --
187 *
188 * Send a signal to the <a href="/cvs/aolserver/aolserver/nsd/main.c#A_main">main</a> thread.
189 *
190 * Results:
191 * None.
192 *
193 * Side effects:
194 * Main thread in <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_NsHandleSignals">NsHandleSignals</a> will wakeup.
195 *
196 *----------------------------------------------------------------------
197 */
198
199 void
200 <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_NsSendSignal">NsSendSignal</a>(int sig)
201 {
202 if (<a href="/cvs/aolserver/aolserver/nsd/nswin32.c#A_kill">kill</a>(<a href="/cvs/aolserver/aolserver/nsd/info.c#A_Ns_InfoPid">Ns_InfoPid</a>(), sig) != 0) {
203 <a href="/cvs/aolserver/aolserver/nsd/log.c#A_Ns_Fatal">Ns_Fatal</a>("unix: <a href="/cvs/aolserver/aolserver/nsd/nswin32.c#A_kill">kill</a>() failed: '%s'", strerror(errno));
204 }
205 }
206
207
208 /*
209 *----------------------------------------------------------------------
210 * <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_Ns_GetUserHome">Ns_GetUserHome</a> --
211 *
212 * Get the home directory name for a user name
213 *
214 * Results:
215 * Return NS_TRUE if user name is found in /etc/passwd file and
216 * NS_FALSE otherwise.
217 *
218 * Side effects:
219 * None.
220 *
221 *----------------------------------------------------------------------
222 */
223
224 int
225 <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_Ns_GetUserHome">Ns_GetUserHome</a>(Ns_DString *pds, char *user)
226 {
227 struct passwd *pw;
228 int retcode;
229
230 Ns_MutexLock(&lock);
231 pw = getpwnam(user);
232 if (pw == NULL) {
233 retcode = NS_FALSE;
234 } else {
235 <a href="/cvs/aolserver/aolserver/nsd/dstring.c#A_Ns_DStringAppend">Ns_DStringAppend</a>(pds, pw->pw_dir);
236 retcode = NS_TRUE;
237 }
238 Ns_MutexUnlock(&lock);
239 return retcode;
240 }
241
242
243 /*
244 *----------------------------------------------------------------------
245 * <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_Ns_GetGid">Ns_GetGid</a> --
246 *
247 * Get the group id from a group name.
248 *
249 * Results:
250 * Group id or -1 if not found.
251 *
252 * Side effects:
253 * None.
254 *
255 *----------------------------------------------------------------------
256 */
257
258 int
259 <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_Ns_GetGid">Ns_GetGid</a>(char *group)
260 {
261 int retcode;
262 struct group *grent;
263
264 Ns_MutexLock(&lock);
265 grent = getgrnam(group);
266 if (grent == NULL) {
267 retcode = -1;
268 } else {
269 retcode = grent->gr_gid;
270 }
271 Ns_MutexUnlock(&lock);
272 return retcode;
273 }
274
275
276 /*
277 *----------------------------------------------------------------------
278 * <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_Ns_GetUserGid">Ns_GetUserGid</a> --
279 *
280 * Get the group id for a user name
281 *
282 * Results:
283 * Returns group id of the user name found in /etc/passwd or -1
284 * otherwise.
285 *
286 * Side effects:
287 * None.
288 *
289 *----------------------------------------------------------------------
290 */
291
292 int
293 <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_Ns_GetUserGid">Ns_GetUserGid</a>(char *user)
294 {
295 struct passwd *pw;
296 int retcode;
297
298 Ns_MutexLock(&lock);
299 pw = getpwnam(user);
300 if (pw == NULL) {
301 retcode = -1;
302 } else {
303 retcode = pw->pw_gid;
304 }
305 Ns_MutexUnlock(&lock);
306 return retcode;
307 }
308
309
310 /*
311 *----------------------------------------------------------------------
312 * <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_Ns_GetUid">Ns_GetUid</a> --
313 *
314 * Get user id for a user name.
315 *
316 * Results:
317 * Return NS_TRUE if user name is found in /etc/passwd file and
318 * NS_FALSE otherwise.
319 *
320 * Side effects:
321 * None.
322 *
323 *----------------------------------------------------------------------
324 */
325
326 int
327 <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_Ns_GetUid">Ns_GetUid</a>(char *user)
328 {
329 struct passwd *pw;
330 int retcode;
331
332 Ns_MutexLock(&lock);
333 pw = getpwnam(user);
334 if (pw == NULL) {
335 retcode = -1;
336 } else {
337 retcode = (unsigned) pw->pw_uid;
338 }
339 Ns_MutexUnlock(&lock);
340 return retcode;
341 }
342
343
344 /*
345 *----------------------------------------------------------------------
346 *
347 * <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_Abort">Abort</a> --
348 *
349 * Ensure that we drop core on fatal signals like SIGBUS and
350 * SIGSEGV.
351 *
352 * Results:
353 * None.
354 *
355 * Side effects:
356 * A core file will be left wherever the server was running.
357 *
358 *----------------------------------------------------------------------
359 */
360
361 static void
362 <a href="/cvs/aolserver/aolserver/nsd/unix.c#A_Abort">Abort</a>(int signal)
363 {
364 <a href="/cvs/aolserver/aolserver/nsd/log.c#A_Ns_Log">Ns_Log</a>(Fatal, "received fatal signal %d", signal);
365 abort();
366 }
367