4e4d3bbf1c819847db856064f287689873bf2328
[public/netxms.git] / src / libnetxms / threads.cpp
1 /*
2 ** libnetxms - Common NetXMS utility library
3 ** Copyright (C) 2003, 2004 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** $module: threads.cpp
20 **
21 **/
22
23 #if !defined(_WIN32) && !defined(_NETWARE)
24
25 #include "libnetxms.h"
26 #include <signal.h>
27 #include <sys/wait.h>
28
29 #if HAVE_SYS_UTSNAME_H
30 #include <sys/utsname.h>
31 #endif
32
33
34 //
35 // Start main loop and signal handler for daemon
36 //
37
38 void LIBNETXMS_EXPORTABLE
39 StartMainLoop(THREAD_RESULT (THREAD_CALL * pfSignalHandler)(void *),
40 THREAD_RESULT (THREAD_CALL * pfMain)(void *))
41 {
42 THREAD hThread;
43 struct utsname un;
44 int nModel = 0;
45 sigset_t signals;
46
47 // Block signals
48 sigemptyset(&signals);
49 sigaddset(&signals, SIGTERM);
50 sigaddset(&signals, SIGINT);
51 sigaddset(&signals, SIGPIPE);
52 sigaddset(&signals, SIGSEGV);
53 sigaddset(&signals, SIGCHLD);
54 sigaddset(&signals, SIGHUP);
55 sigaddset(&signals, SIGUSR1);
56 sigaddset(&signals, SIGUSR2);
57 sigprocmask(SIG_BLOCK, &signals, NULL);
58
59 if (uname(&un) != -1)
60 {
61 char *ptr;
62
63 ptr = strchr(un.release, '.');
64 if (ptr != NULL)
65 *ptr = 0;
66 if (!stricmp(un.sysname, "FreeBSD") && (atoi(un.release) >= 5))
67 nModel = 1;
68 }
69
70 if (pfMain != NULL)
71 {
72 if (nModel == 0)
73 {
74 hThread = ThreadCreateEx(pfMain, 0, NULL);
75 pfSignalHandler(NULL);
76 ThreadJoin(hThread);
77 }
78 else
79 {
80 hThread = ThreadCreateEx(pfSignalHandler, 0, NULL);
81 pfMain(NULL);
82 ThreadJoin(hThread);
83 }
84 }
85 else
86 {
87 if (nModel == 0)
88 {
89 pfSignalHandler(NULL);
90 }
91 else
92 {
93 hThread = ThreadCreateEx(pfSignalHandler, 0, NULL);
94 ThreadJoin(hThread);
95 }
96 }
97 }
98
99
100 #endif /* _WIN32 && _NETWARE*/