1 mike 1.5 //%/////////////////////////////////////////////////////////////////////////////
2 //
|
3 kumpf 1.8 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
4 // The Open Group, Tivoli Systems
|
5 mike 1.5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 kumpf 1.8 // of this software and associated documentation files (the "Software"), to
8 // deal in the Software without restriction, including without limitation the
9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
10 mike 1.5 // sell copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
|
13 kumpf 1.8 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
14 mike 1.5 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
15 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
16 kumpf 1.8 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
19 mike 1.5 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 //==============================================================================
23 //
24 // Author: Mike Day (mdday@us.ibm.com)
25 //
|
26 kumpf 1.10 // Modified By: Jenny Yu, Hewlett-Packard Company (jenny_yu@hp.com)
27 // Yi Zhou, Hewlett-Packard Company (yi_zhou@hp.com)
|
28 mike 1.5 //
29 //%/////////////////////////////////////////////////////////////////////////////
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
|
33 kumpf 1.7 #if defined(PEGASUS_OS_HPUX)
|
34 kumpf 1.6 #include <sys/pstat.h>
|
35 kumpf 1.7 #endif
|
36 mike 1.5 #include <fcntl.h>
37 #include <unistd.h>
|
38 kumpf 1.10 #include <signal.h>
|
39 mike 1.5
|
40 kumpf 1.11 // Note: on the Unix platform, PEGASUS_RETURN_WHEN_READY flag is turned on
41 // by default. This means that the cimserver is ready to serve CIM
42 // requests when the command cimserver is returned. If you like to
43 // turn off this feature on your platform, just undefine this flag for
44 // your platform.
45 #define PEGASUS_RETURN_WHEN_READY
46
|
47 kumpf 1.12 #define MAX_WAIT_TIME 15
48
|
49 mike 1.5 PEGASUS_USING_PEGASUS;
50 PEGASUS_USING_STD;
|
51 kumpf 1.10
52 #ifdef PEGASUS_RETURN_WHEN_READY
53 static sig_atomic_t sigflag;
54 static sigset_t newmask, oldmask, zeromask;
|
55 kumpf 1.12 Boolean signalFlag = true;
|
56 kumpf 1.10 #endif
|
57 kumpf 1.6
|
58 mike 1.5 void cim_server_service(int argc, char **argv ) { return; }
59 unsigned int cimserver_remove_nt_service(void) { return(0) ; }
60 unsigned int cimserver_install_nt_service(String &pegasusHome ) { return(0) ; }
|
61 tony 1.13 unsigned int cimserver_start_nt_service(void) { return(0) ; }
62 unsigned int cimserver_stop_nt_service(void) { return(0) ; }
|
63 mike 1.5
|
64 kumpf 1.9 const char *fname = "/etc/opt/wbem/cimserver_start.conf";
|
65 kumpf 1.6 pid_t server_pid;
66
|
67 kumpf 1.10 #ifdef PEGASUS_RETURN_WHEN_READY
68 static void sig_usr(int signo)
69 {
70 sigflag = 1;
71 return;
72 }
73 #endif
74
|
75 mike 1.5 // daemon_init , RW Stevens, "Advance UNIX Programming"
76
77 int cimserver_fork(void)
78 {
|
79 kumpf 1.10 #ifdef PEGASUS_RETURN_WHEN_READY
80 // set up things
|
81 kumpf 1.12 if (signal(SIGUSR1, sig_usr) == SIG_ERR)
82 {
83 signalFlag = false;
84 }
85 else
86 {
87 sigemptyset(&zeromask);
88 sigemptyset(&newmask);
89 sigaddset(&newmask, SIGUSR1);
90
91 // block SIGUSR1 and save current signal mask
92 sigprocmask(SIG_BLOCK, &newmask, &oldmask);
93 }
|
94 kumpf 1.10 #endif
95
|
96 mike 1.5 pid_t pid;
97 if( (pid = fork() ) < 0)
98 return(-1);
99 else if (pid != 0)
|
100 kumpf 1.10 {
101 #ifdef PEGASUS_RETURN_WHEN_READY
|
102 kumpf 1.12 if (signalFlag)
103 {
104 // parent wait for child
105 while (sigflag == 0)
106 sigsuspend(&zeromask);
107
108 // reset signal mask to original value
109 sigprocmask(SIG_SETMASK, &oldmask, NULL);
110 }
111 else
112 {
113 // if there is a problem with signal, parent process terminates
114 // until file cimserver_start.conf exists or maxWaitTime expires
115
116 Uint32 maxWaitTime = MAX_WAIT_TIME;
117
118 while(!FileSystem::exists(fname) && maxWaitTime > 0)
119 {
120 sleep(1);
121 maxWaitTime = maxWaitTime - 1;
122 }
123 kumpf 1.12 }
|
124 kumpf 1.10 #endif
|
125 mike 1.5 exit(0);
|
126 kumpf 1.10 }
|
127 mike 1.5
128 setsid();
129 umask(0);
|
130 kumpf 1.6
131 // get the pid of the cimserver process
132 server_pid = getpid();
133
134 return(0);
|
135 kumpf 1.9 }
136
137 Boolean isCIMServerRunning(void)
138 {
139 FILE *pid_file;
140 pid_t pid = 0;
141
142 // open the file containing the CIMServer process ID
143 pid_file = fopen(fname, "rw");
144 if (!pid_file)
145 {
146 return false;
147 }
148
149 // get the pid from the file
150 fscanf(pid_file, "%ld\n", &pid);
151
152 if (pid == 0)
153 {
154 return false;
155 }
156 kumpf 1.9
157 //
158 // check to see if cimserver process is alive
159 //
160 #if defined(PEGASUS_OS_HPUX)
161 struct pst_status pstru;
162
163 if (pstat_getproc(&pstru, sizeof(struct pst_status), (size_t)0, pid) != -1)
164 {
165 return true;
166 }
167 #endif
168
169 return false;
|
170 kumpf 1.6 }
171
172 int cimserver_kill(void)
173 {
174 FILE *pid_file;
175 pid_t pid = 0;
176
177 // open the file containing the CIMServer process ID
178 pid_file = fopen(fname, "r");
179 if (!pid_file)
180 {
181 return (-1);
182 }
183
184 // get the pid from the file
185 fscanf(pid_file, "%ld\n", &pid);
186
187 if (pid == 0)
188 {
189 System::removeFile(fname);
190 return (-1);
191 kumpf 1.6 }
192
193 //
194 // kill the process if it is still alive
195 //
|
196 kumpf 1.7 #if defined(PEGASUS_OS_HPUX)
|
197 kumpf 1.6 struct pst_status pstru;
198
199 if (pstat_getproc(&pstru, sizeof(struct pst_status), (size_t)0, pid) != -1)
200 {
201 kill(pid, SIGKILL);
202 }
|
203 kumpf 1.7 #endif
|
204 kumpf 1.6
205 // remove the file
206 System::removeFile(fname);
207
|
208 mike 1.5 return(0);
209 }
210
|
211 kumpf 1.10 // notify parent process to terminate so user knows that cimserver
212 // is ready to serve CIM requests.
213 void notify_parent(void)
214 {
215 #ifdef PEGASUS_RETURN_WHEN_READY
216 pid_t ppid = getppid();
|
217 kumpf 1.12
218 // if kill() fails, no signal is sent
219 if (kill(ppid, SIGUSR1) == -1)
220 {
221 signalFlag = false;
222 }
|
223 kumpf 1.10 #endif
224 }
|