unix/mac/APIService.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 <stdlib.h>
44 #include <stdio.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47 #include <fcntl.h>
48 #include <signal.h>
49 #include <pwd.h>
50 #include "APIServer.hpp"
51 #include "stdio.h"
52 #define OOREXX_PIDFILE "/tmp/ooRexx.pid"
53 
54 APIServer apiServer; // the real server instance
55 
56 
57 /*==========================================================================*
58  * Function: Run
59  *
60  * Purpose:
61  *
62  * handles the original RXAPI functions.
63  * Perform the message loop
64  *
65  *
66  *==========================================================================*/
67 void Run (bool asService)
68 {
69  try
70  {
71  apiServer.initServer(); // start up the server
72  apiServer.listenForConnections(); // go into the message loop
73  }
74  catch (ServiceException *)
75  {
76  }
77  apiServer.terminateServer(); // shut everything down
78 }
79 
80 /*==========================================================================*
81  * Function: Stop
82  *
83  * Purpose:
84  *
85  * handles the stop request.
86  *
87  *
88  *==========================================================================*/
89 void Stop(int signo)
90 {
91  apiServer.terminateServer(); // shut everything down
92 
93  exit(1);
94 }
95 
96 
97 /*==========================================================================*
98  * Function: main
99  *
100  * Purpose:
101  *
102  * Main entry point.
103  *
104  *==========================================================================*/
105 int main(int argc, char *argv[])
106 {
107  char pid_buf[256];
108  int pfile, len;
109  pid_t pid = 0;
110  struct stat st;
111  struct sigaction sa;
112  // Get the command line args
113  if (argc > 1) {
114  printf("Error: Invalid command line option(s).\n");
115  printf(" Aborting execution.\n\n");
116  return -1;
117  }
118 
119  // see if we are already running
120  if ((pfile = open(OOREXX_PIDFILE, O_RDONLY)) > 0) {
121  len = read(pfile, pid_buf, sizeof(pid_buf) - 1);
122  close(pfile);
123  pid_buf[len] = '\0';
124  pid = (pid_t)atoi(pid_buf);
125  if (pid && (pid == getpid() || kill(pid, 0) < 0)) {
126  unlink(OOREXX_PIDFILE);
127  } else {
128  // there is already a server running
129  printf("Error: There is already a server running.\n");
130  printf(" Aborting execution.\n");
131  return -1;
132  }
133  }
134 
135  // write the pid file
136  pfile = open(OOREXX_PIDFILE, O_WRONLY | O_CREAT,
137  S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
138  if (pfile == -1) {
139  // cannot open pid file
140  printf("Error: Cannot open PID file %s.\n", OOREXX_PIDFILE);
141  printf(" Aborting execution.\n\n");
142  return -1;
143  }
144  snprintf(pid_buf, sizeof(pid_buf), "%d\n", (int)getpid());
145  write(pfile, pid_buf, strlen(pid_buf));
146  close(pfile);
147 
148  // handle kill -15
149  (void) sigemptyset(&sa.sa_mask);
150  (void) sigaddset(&sa.sa_mask, SIGTERM);
151  sa.sa_flags = SA_RESTART;
152  sa.sa_handler = Stop;
153  if (sigaction(SIGTERM, &sa, NULL) == -1) {
154  exit(1);
155  }
156 
157  Run(false);
158 
159  return 0;
160 }
161 
void initServer()
Definition: APIServer.cpp:53
void terminateServer()
Definition: APIServer.cpp:69
void listenForConnections()
Definition: APIServer.cpp:82
int main(int argc, char *argv[])
void Stop(int signo)
void Run(bool asService)
#define OOREXX_PIDFILE
APIServer apiServer