(file) Return to main.c CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Executor

  1 kumpf 1.2 /*
  2 martin 1.14 //%LICENSE////////////////////////////////////////////////////////////////
  3 martin 1.15 //
  4 martin 1.14 // Licensed to The Open Group (TOG) under one or more contributor license
  5             // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  6             // this work for additional information regarding copyright ownership.
  7             // Each contributor licenses this file to you under the OpenPegasus Open
  8             // Source License; you may not use this file except in compliance with the
  9             // License.
 10 martin 1.15 //
 11 martin 1.14 // Permission is hereby granted, free of charge, to any person obtaining a
 12             // copy of this software and associated documentation files (the "Software"),
 13             // to deal in the Software without restriction, including without limitation
 14             // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 15             // and/or sell copies of the Software, and to permit persons to whom the
 16             // Software is furnished to do so, subject to the following conditions:
 17 martin 1.15 //
 18 martin 1.14 // The above copyright notice and this permission notice shall be included
 19             // in all copies or substantial portions of the Software.
 20 martin 1.15 //
 21 martin 1.14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 22 martin 1.15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 23 martin 1.14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 24             // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 25             // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 26             // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 27             // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 28 martin 1.15 //
 29 martin 1.14 //////////////////////////////////////////////////////////////////////////
 30 kumpf  1.2  */
 31 martin 1.14 
 32 kumpf  1.2  #include <stdio.h>
 33             #include <sys/types.h>
 34             #include <sys/stat.h>
 35             #include <unistd.h>
 36             #include <string.h>
 37 kumpf  1.10 #include <signal.h>
 38             #include <time.h>
 39 kumpf  1.2  #include "Config.h"
 40             #include "Child.h"
 41             #include "Parent.h"
 42             #include "User.h"
 43             #include "Fatal.h"
 44             #include "Process.h"
 45             #include "Path.h"
 46             #include "Globals.h"
 47             #include "Socket.h"
 48             #include "Strlcpy.h"
 49             #include "Strlcat.h"
 50             #include "Log.h"
 51             #include "Policy.h"
 52             #include "Macro.h"
 53             #include "Assert.h"
 54             #include "Options.h"
 55 kumpf  1.8  #include <Pegasus/Common/PegasusVersion.h>
 56 kumpf  1.2  
 57 kumpf  1.10 #define CIMSERVER_COMMAND_TIMEOUT_SECONDS 240
 58             
 59 s.kodali 1.17 #ifdef PEGASUS_FLAVOR
 60               # define CIMSERVER_LOG_IDENTITY "cimserver" PEGASUS_FLAVOR
 61               #else
 62               # define CIMSERVER_LOG_IDENTITY "cimserver"
 63               #endif
 64               
 65 kumpf    1.2  /*
 66               **==============================================================================
 67               **
 68               ** GetServerUser
 69               **
 70               **     Determine which user to run CIMSERVERMAIN as.
 71               **
 72               **==============================================================================
 73               */
 74               
 75 kumpf    1.3  int GetServerUser(const char** userName, int* uid, int* gid)
 76 kumpf    1.2  {
 77 kumpf    1.3      *userName = PEGASUS_CIMSERVERMAIN_USER;
 78 kumpf    1.2  
 79 kumpf    1.3      if (GetUserInfo(*userName, uid, gid) != 0)
 80 kumpf    1.2      {
 81                       Fatal(FL,
 82                           "The %s user \"%s\" does not exist.",
 83 kumpf    1.3              CIMSERVERMAIN, *userName);
 84 kumpf    1.2      }
 85               
 86                   return 0;
 87               }
 88               
 89               /*
 90               **==============================================================================
 91               **
 92               ** ExecShutdown()
 93               **
 94               **==============================================================================
 95               */
 96               
 97 kumpf    1.5  void ExecShutdown(void)
 98 kumpf    1.2  {
 99                   char* tmpArgv[3];
100                   const char* cimshutdownPath;
101                   const char* shutdownTimeout;
102               
103                   /* Get shutdownTimeout configuration parameter. */
104               
105                   if ((shutdownTimeout = FindMacro("shutdownTimeout")) == NULL)
106                       Fatal(FL, "failed to resolve shutdownTimeout");
107               
108                   /* Get absolute CIMSHUTDOWN program name. */
109               
110                   if ((cimshutdownPath = FindMacro("cimshutdownPath")) == NULL)
111                       Fatal(FL, "failed to resolve cimshutdownPath");
112               
113                   /* Create argument list. */
114               
115                   tmpArgv[0] = CIMSHUTDOWN;
116                   tmpArgv[1] = (char*)shutdownTimeout;
117                   tmpArgv[2] = 0;
118               
119 kumpf    1.2      /* Exec CIMSHUTDOWN program. */
120               
121                   /* Flawfinder: ignore */
122                   execv(cimshutdownPath, tmpArgv);
123                   Fatal(FL, "failed to exec %s", cimshutdownPath);
124               }
125               
126               /*
127               **==============================================================================
128               **
129               ** DefineExecutorMacros()
130               **
131               **     Define macros used by the executor.
132               **
133               **==============================================================================
134               */
135               
136 kumpf    1.5  void DefineExecutorMacros(void)
137 kumpf    1.2  {
138                   /* Define ${internalBinDir} */
139                   {
140                       char path[EXECUTOR_BUFFER_SIZE];
141               
142                       if (GetPegasusInternalBinDir(path) != 0)
143                           Fatal(FL, "failed to resolve internal pegasus bin directory");
144               
145                       DefineMacro("internalBinDir", path);
146                   }
147               
148                   /* Define ${cimservermain} */
149               
150                   DefineMacro("cimservermain", CIMSERVERMAIN);
151               
152                   /* Define ${cimprovagt} */
153               
154                   DefineMacro("cimprovagt", CIMPROVAGT);
155               
156                   /* Define ${cimshutdown} */
157               
158 kumpf    1.2      DefineMacro("cimshutdown", CIMSHUTDOWN);
159               
160                   /* Define ${cimservera} */
161               
162                   DefineMacro("cimservera", CIMSERVERA);
163               
164                   /* Define ${cimservermainPath} */
165                   {
166                       char path[EXECUTOR_BUFFER_SIZE];
167               
168                       if (ExpandMacros("${internalBinDir}/${cimservermain}", path) != 0)
169                           Fatal(FL, "failed to resolve cimservermainPath");
170               
171                       DefineMacro("cimservermainPath", path);
172                   }
173               
174                   /* Define ${cimprovagtPath} */
175                   {
176                       char path[EXECUTOR_BUFFER_SIZE];
177               
178                       if (ExpandMacros("${internalBinDir}/${cimprovagt}", path) != 0)
179 kumpf    1.2              Fatal(FL, "failed to resolve cimprovagtPath");
180               
181                       DefineMacro("cimprovagtPath", path);
182                   }
183               
184                   /* Define ${cimshutdownPath} */
185                   {
186                       char path[EXECUTOR_BUFFER_SIZE];
187               
188                       if (ExpandMacros("${internalBinDir}/${cimshutdown}", path) != 0)
189                           Fatal(FL, "failed to resolve cimshutdownPath");
190               
191                       DefineMacro("cimshutdownPath", path);
192                   }
193               
194                   /* Define ${cimserveraPath} */
195                   {
196                       char path[EXECUTOR_BUFFER_SIZE];
197               
198                       if (ExpandMacros("${internalBinDir}/${cimservera}", path) != 0)
199                           Fatal(FL, "failed to resolve cimserveraPath");
200 kumpf    1.2  
201                       DefineMacro("cimserveraPath", path);
202                   }
203               
204                   /* Define ${shutdownTimeout} */
205               
206                   {
207                       char buffer[EXECUTOR_BUFFER_SIZE];
208               
209                       if (GetConfigParam("shutdownTimeout", buffer) != 0)
210 kumpf    1.7          {
211                           Strlcpy(
212                               buffer,
213                               PEGASUS_DEFAULT_SHUTDOWN_TIMEOUT_SECONDS_STRING,
214                               sizeof(buffer));
215                       }
216 kumpf    1.2  
217                       DefineMacro("shutdownTimeout", buffer);
218                   }
219               
220                   /* Define ${currentConfigFilePath} */
221                   {
222                       char path[EXECUTOR_BUFFER_SIZE];
223               
224                       if (GetHomedPath(PEGASUS_CURRENT_CONFIG_FILE_PATH, path) != 0)
225                       {
226                           Fatal(FL, "GetHomedPath() failed on \"%s\"",
227                               PEGASUS_CURRENT_CONFIG_FILE_PATH);
228                       }
229               
230                       DefineMacro("currentConfigFilePath", path);
231                   }
232               
233                   /* Define ${plannedConfigFilePath} */
234                   {
235                       char path[EXECUTOR_BUFFER_SIZE];
236               
237 kumpf    1.2          if (GetHomedPath(PEGASUS_PLANNED_CONFIG_FILE_PATH, path) != 0)
238                       {
239                           Fatal(FL, "GetHomedPath() failed on \"%s\"",
240                               PEGASUS_PLANNED_CONFIG_FILE_PATH);
241                       }
242               
243                       DefineMacro("plannedConfigFilePath", path);
244                   }
245               
246                   /* Define ${passwordFilePath} */
247               
248                   if (DefineConfigPathMacro("passwordFilePath", "cimserver.passwd") != 0)
249                       Fatal(FL, "missing \"passwordFilePath\" configuration parameter.");
250               
251                   /* Define ${sslKeyFilePath} */
252               
253                   if (DefineConfigPathMacro("sslKeyFilePath", "file.pem") != 0)
254                       Fatal(FL, "missing \"sslKeyFilePath\" configuration parameter.");
255               
256                   /* Define ${sslTrustStore} */
257               
258 kumpf    1.2      if (DefineConfigPathMacro("sslTrustStore", "cimserver_trust") != 0)
259                       Fatal(FL, "missing \"sslTrustStore\" configuration parameter.");
260               
261                   /* Define ${crlStore} */
262               
263                   if (DefineConfigPathMacro("crlStore", "crl") != 0)
264                       Fatal(FL, "missing \"crlStore\" configuration parameter.");
265               }
266               
267               /*
268               **==============================================================================
269               **
270               ** main()
271               **
272               **==============================================================================
273               */
274               
275 s.kodali 1.17 
276 kumpf    1.2  int main(int argc, char** argv)
277               {
278                   const char* cimservermainPath;
279 kumpf    1.11     const char* workingDirectory;
280 kumpf    1.2      int pair[2];
281 kumpf    1.10     int initCompletePipe[2];
282                   int pid;
283 kumpf    1.2      char username[EXECUTOR_BUFFER_SIZE];
284 kumpf    1.6      const char* childUserName;
285                   int childUid;
286                   int childGid;
287 kumpf    1.2      int childPid;
288                   struct Options options;
289               
290                   /* Get options. */
291               
292                   GetOptions(&argc, &argv, &options);
293               
294                   /* Save argc-argv as globals. */
295               
296                   globals.argc = argc;
297                   globals.argv = argv;
298               
299 kumpf    1.6      /* Open the log. */
300               
301 s.kodali 1.17     OpenLog(CIMSERVER_LOG_IDENTITY);
302 kumpf    1.6  
303 kumpf    1.2      /* Define macros needed by the executor. */
304               
305                   DefineExecutorMacros();
306               
307 kumpf    1.6      /* If shutting down, then run CIMSHUTDOWN client. */
308 kumpf    1.2  
309                   if (options.shutdown)
310                       ExecShutdown();
311               
312                   /* Process --policy and --macros options. */
313               
314                   if (options.dump)
315                   {
316 kumpf    1.4          DumpPolicy(stdout, 1);
317                       DumpMacros(stdout);
318 kumpf    1.2          exit(0);
319                   }
320               
321                   /* Get absolute CIMSERVERMAIN program name. */
322               
323                   if ((cimservermainPath = FindMacro("cimservermainPath")) == NULL)
324                       Fatal(FL, "Failed to locate %s program", CIMSERVERMAIN);
325               
326                   /* If CIMSERVERMAIN is already running, warn and exit now, unless one of
327                    * the following options are given: -v, --version, -h, --help (these are
328                    * passed through to CIMSERVERMAIN).
329                    */
330               
331 denise.eckstein 1.16     if (!options.version && !options.help)
332                          {
333                              int isRunning = (TestProcessRunning(
334                                  PEGASUS_CIMSERVER_START_FILE, CIMSERVERMAIN) == 0);
335                              if (options.status)
336                              {
337                                  if (isRunning)
338                                  {
339                                      fprintf(stderr, "CIM Server is running.\n");
340                                      exit(0);
341                                  } 
342                                  else
343                                  {
344                                      fprintf(stderr, "CIM Server is not running.\n");
345                                      exit(2);
346                                  }
347                              }
348                              else if (isRunning)
349                              {
350                                  fprintf(stderr,
351                                      "%s: cimserver is already running (the PID found in the file "
352 denise.eckstein 1.16                 "\"%s\" corresponds to an existing process named \"%s\").\n\n",
353                                      globals.argv[0], PEGASUS_CIMSERVER_START_FILE, CIMSERVERMAIN);  
354                                  exit(1);
355                              }
356                          } 
357 kumpf           1.2  
358                          /* Get enableAuthentication configuration option. */
359                      
360                          {
361                              char buffer[EXECUTOR_BUFFER_SIZE];
362                      
363                              if (GetConfigParam("enableAuthentication", buffer) == 0 &&
364                                  strcasecmp(buffer, "true") == 0)
365                              {
366                                  globals.enableAuthentication = 1;
367                              }
368                          }
369                      
370 kumpf           1.4      /* Initialize the log-level from the configuration parameter. */
371 kumpf           1.2  
372 kumpf           1.4      InitLogLevel();
373 kumpf           1.2  
374 denise.eckstein 1.13     Log(LL_TRACE, "starting");
375 kumpf           1.2  
376                          /* Be sure this process is running as root (otherwise fail). */
377                      
378                          if (setuid(0) != 0 || setgid(0) != 0)
379                              Fatal(FL, "attempted to run program as non-root user");
380                      
381                          /* Warn if authentication not enabled (suspicious use of executor). */
382                      
383                          if (!globals.enableAuthentication)
384                              Log(LL_WARNING, "authentication is NOT enabled");
385                          else
386                              Log(LL_TRACE, "authentication is enabled");
387                      
388                          /* Print user info. */
389                      
390                          if (GetUserName(getuid(), username) != 0)
391                              Fatal(FL, "cannot resolve user from uid=%d", getuid());
392                      
393                          Log(LL_TRACE, "running as %s (uid=%d, gid=%d)",
394                              username, (int)getuid(), (int)getgid());
395                      
396 mateus.baur     1.9      /* Change the current directory */
397                      
398 kumpf           1.11 #if defined(PEGASUS_USE_RELEASE_DIRS) && defined(PEGASUS_CORE_DIR)
399                          workingDirectory = PEGASUS_CORE_DIR;
400 denise.eckstein 1.12 #elif defined(PEGASUS_DEBUG)
401                          workingDirectory = getenv("PEGASUS_TMP");
402 mateus.baur     1.9  #endif
403 denise.eckstein 1.12     if (workingDirectory == NULL)
404                          {
405                              workingDirectory = "/";
406                          }
407 kumpf           1.11     if (chdir(workingDirectory) != 0)
408                          {
409                              Log(LL_WARNING, "Failed to change working directory to %s",
410                                  workingDirectory);
411                          }
412                      
413 kumpf           1.10     /* Create a pipe for communicating with cimserver daemon process. */
414                      
415                          if (pipe(initCompletePipe) != 0)
416                              Fatal(FL, "Failed to create pipe");
417                      
418                          CloseOnExec(initCompletePipe[0]);
419                          CloseOnExec(initCompletePipe[1]);
420                      
421                          /* Fork to ensure we are not a session leader so setsid() will succeed. */
422                      
423                          pid = fork();
424                      
425                          if (pid < 0)
426                          {
427                              Fatal(FL, "fork() failed");
428                          }
429                      
430                          if (pid > 0)
431                          {
432                              /* Wait until daemon writes an exit code or closes the pipe. */
433                      
434 kumpf           1.10         char exitRC;
435                              ssize_t result;
436                              time_t startTime;
437                              time_t now;
438                      
439                              close(initCompletePipe[1]);
440                              SetNonBlocking(initCompletePipe[0]);
441                              time(&startTime);
442                      
443                              do
444                              {
445                                  time(&now);
446                                  result = WaitForReadEnable(
447                                      initCompletePipe[0],
448                                      (CIMSERVER_COMMAND_TIMEOUT_SECONDS - (now - startTime)) * 1000);
449                              } while (result == -1 && errno == EINTR);
450                      
451                              if (result == 0)
452                              {
453                                  fprintf(stderr,
454                                      "The cimserver command timed out waiting for the CIM server "
455 kumpf           1.10                     "to start.");
456                                  _exit(0);
457                              }
458                      
459                              EXECUTOR_RESTART(read(initCompletePipe[0], &exitRC, 1), result);
460                              if (result <= 0)
461                              {
462                                  exitRC = 1;
463                              }
464                              _exit(exitRC);
465                          }
466                      
467                          close(initCompletePipe[0]);
468                      
469                          /* Become session leader (so that our child process will not be one) */
470                      
471                          if (setsid() < 0)
472                          {
473                              Fatal(FL, "setsid() failed");
474                          }
475                      
476 kumpf           1.10     /* Ignore SIGHUP: */
477                      
478                          signal(SIGHUP, SIG_IGN);
479                      
480                          /* Ignore SIGCHLD: */
481                      
482                          signal(SIGCHLD, SIG_IGN);
483                      
484                          /* Ignore SIGPIPE: */
485                      
486                          signal(SIGPIPE, SIG_IGN);
487                      
488                          /* Fork cimserver daemon process (not a session leader since parent is). */
489                      
490                          pid = fork();
491                      
492                          if (pid < 0)
493                          {
494                              Fatal(FL, "fork() failed");
495                          }
496                      
497 kumpf           1.10     if (pid > 0)
498                          {
499                              _exit(0);
500                          }
501                      
502 kumpf           1.2      /* Determine user for running CIMSERVERMAIN. */
503                      
504 kumpf           1.6      GetServerUser(&childUserName, &childUid, &childGid);
505 kumpf           1.2  
506 kumpf           1.10     /* Create a socket pair for communicating with the child process. */
507                      
508                          if (CreateSocketPair(pair) != 0)
509                              Fatal(FL, "Failed to create socket pair");
510                      
511                          CloseOnExec(pair[1]);
512                      
513 kumpf           1.2      /* Fork child process. */
514                      
515                          childPid = fork();
516                      
517                          if (childPid == 0)
518                          {
519                              /* Child. */
520                              close(pair[1]);
521 kumpf           1.3          Child(
522                                  argc,
523                                  argv,
524                                  cimservermainPath,
525 kumpf           1.6              childUserName,
526                                  childUid,
527                                  childGid,
528 kumpf           1.3              pair[0]);
529 kumpf           1.2      }
530                          else if (childPid > 0)
531                          {
532                              /* Parent. */
533                              close(pair[0]);
534 kumpf           1.10         Parent(pair[1], initCompletePipe[1], childPid, options.bindVerbose);
535 kumpf           1.2      }
536                          else
537                          {
538                              Fatal(FL, "fork() failed");
539                          }
540                      
541                          return 0;
542                      }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2