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
|
62 kumpf 1.9 const char *fname = "/etc/opt/wbem/cimserver_start.conf";
|
63 kumpf 1.6 pid_t server_pid;
64
|
65 kumpf 1.10 #ifdef PEGASUS_RETURN_WHEN_READY
66 static void sig_usr(int signo)
67 {
68 sigflag = 1;
69 return;
70 }
71 #endif
72
|
73 mike 1.5 // daemon_init , RW Stevens, "Advance UNIX Programming"
74
75 int cimserver_fork(void)
76 {
|
77 kumpf 1.10 #ifdef PEGASUS_RETURN_WHEN_READY
78 // set up things
|
79 kumpf 1.12 if (signal(SIGUSR1, sig_usr) == SIG_ERR)
80 {
81 signalFlag = false;
82 }
83 else
84 {
85 sigemptyset(&zeromask);
86 sigemptyset(&newmask);
87 sigaddset(&newmask, SIGUSR1);
88
89 // block SIGUSR1 and save current signal mask
90 sigprocmask(SIG_BLOCK, &newmask, &oldmask);
91 }
|
92 kumpf 1.10 #endif
93
|
94 mike 1.5 pid_t pid;
95 if( (pid = fork() ) < 0)
96 return(-1);
97 else if (pid != 0)
|
98 kumpf 1.10 {
99 #ifdef PEGASUS_RETURN_WHEN_READY
|
100 kumpf 1.12 if (signalFlag)
101 {
102 // parent wait for child
103 while (sigflag == 0)
104 sigsuspend(&zeromask);
105
106 // reset signal mask to original value
107 sigprocmask(SIG_SETMASK, &oldmask, NULL);
108 }
109 else
110 {
111 // if there is a problem with signal, parent process terminates
112 // until file cimserver_start.conf exists or maxWaitTime expires
113
114 Uint32 maxWaitTime = MAX_WAIT_TIME;
115
116 while(!FileSystem::exists(fname) && maxWaitTime > 0)
117 {
118 sleep(1);
119 maxWaitTime = maxWaitTime - 1;
120 }
121 kumpf 1.12 }
|
122 kumpf 1.10 #endif
|
123 mike 1.5 exit(0);
|
124 kumpf 1.10 }
|
125 mike 1.5
126 setsid();
127 umask(0);
|
128 kumpf 1.6
129 // get the pid of the cimserver process
130 server_pid = getpid();
131
132 return(0);
|
133 kumpf 1.9 }
134
135 Boolean isCIMServerRunning(void)
136 {
137 FILE *pid_file;
138 pid_t pid = 0;
139
140 // open the file containing the CIMServer process ID
141 pid_file = fopen(fname, "rw");
142 if (!pid_file)
143 {
144 return false;
145 }
146
147 // get the pid from the file
148 fscanf(pid_file, "%ld\n", &pid);
149
150 if (pid == 0)
151 {
152 return false;
153 }
154 kumpf 1.9
155 //
156 // check to see if cimserver process is alive
157 //
158 #if defined(PEGASUS_OS_HPUX)
159 struct pst_status pstru;
160
161 if (pstat_getproc(&pstru, sizeof(struct pst_status), (size_t)0, pid) != -1)
162 {
163 return true;
164 }
165 #endif
166
167 return false;
|
168 kumpf 1.6 }
169
170 int cimserver_kill(void)
171 {
172 FILE *pid_file;
173 pid_t pid = 0;
174
175 // open the file containing the CIMServer process ID
176 pid_file = fopen(fname, "r");
177 if (!pid_file)
178 {
179 return (-1);
180 }
181
182 // get the pid from the file
183 fscanf(pid_file, "%ld\n", &pid);
184
185 if (pid == 0)
186 {
187 System::removeFile(fname);
188 return (-1);
189 kumpf 1.6 }
190
191 //
192 // kill the process if it is still alive
193 //
|
194 kumpf 1.7 #if defined(PEGASUS_OS_HPUX)
|
195 kumpf 1.6 struct pst_status pstru;
196
197 if (pstat_getproc(&pstru, sizeof(struct pst_status), (size_t)0, pid) != -1)
198 {
199 kill(pid, SIGKILL);
200 }
|
201 kumpf 1.7 #endif
|
202 kumpf 1.6
203 // remove the file
204 System::removeFile(fname);
205
|
206 mike 1.5 return(0);
207 }
208
|
209 kumpf 1.10 // notify parent process to terminate so user knows that cimserver
210 // is ready to serve CIM requests.
211 void notify_parent(void)
212 {
213 #ifdef PEGASUS_RETURN_WHEN_READY
214 pid_t ppid = getppid();
|
215 kumpf 1.12
216 // if kill() fails, no signal is sent
217 if (kill(ppid, SIGUSR1) == -1)
218 {
219 signalFlag = false;
220 }
|
221 kumpf 1.10 #endif
222 }
|