unix/SysLocalAPIManager.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.ibm.com/developerworks/oss/CPLv1.0.htm */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 
39 #ifdef HAVE_CONFIG_H
40 # include "config.h"
41 #endif
42 
43 #include "SysLocalAPIManager.hpp"
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <unistd.h>
47 #include <signal.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 
51 
53 {
54  char apiExeName[] = "rxapi";
55  char *apiExeArg[2];
56  apiExeArg[0] = apiExeName;
57  apiExeArg[1] = NULL;
58 
59  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
60  return;
61  }
62 
63  pid_t pid = fork();
64  if (pid < 0) {
65  throw new ServiceException(API_FAILURE, "Unable to start API server");
66  }
67  if (pid != 0) {
68  // we are the parent process
69  return;
70  }
71  // if we get here we are the child process
72 
73  // become the session leader
74  setsid();
75 
76  // housekeeping - chdir to the root subdir and close all open files
77  int ignore = chdir("/");
78  umask(0);
79  for(int i = 0; i < 1024; i++) {
80  close(i);
81  }
82 
83  // now start rxapi
84  if (execvp(apiExeName, apiExeArg) == -1) {
85  throw new ServiceException(API_FAILURE, "Unable to start API server");
86  }
87 
88  return;
89 }
90 
91 
92 /**
93  * Check to see if we've inherited a session queue from a calling process. This shows
94  * up as an environment variable value.
95  *
96  * @param sessionQueue
97  * The returned session queue handle, if it exists.
98  *
99  * @return true if the session queue is inherited, false if a new once needs to be created.
100  */
102 {
103  // check to see if we have an env variable set...if we do we
104  // inherit from our parent session
105  char *envbuffer = getenv("RXQUEUESESSION");
106  if (envbuffer != NULL)
107  {
108  sscanf(envbuffer, "%p", (char **)&sessionQueue);
109  return true;
110  }
111  return false;
112 }
113 
114 /**
115  * Set the active session queue as an environment variable.
116  *
117  * @param sessionQueue
118  * The session queue handle.
119  */
121 {
122  char envbuffer[MAX_QUEUE_NAME_LENGTH+1];
123  // set this as an environment variable for programs we call
124  snprintf(envbuffer, sizeof envbuffer, "%p", (void *)sessionQueue);
125  setenv("RXQUEUESESSION", envbuffer, 1); // overwrite the old value
126 }
127 
uintptr_t QueueHandle
@ API_FAILURE
static void setActiveSessionQueue(QueueHandle sessionQueue)
static bool getActiveSessionQueue(QueueHandle &sessionQueue)
#define MAX_QUEUE_NAME_LENGTH
Definition: rexx.h:780