~ubuntu-branches/ubuntu/precise/code-saturne/precise

« back to all changes in this revision

Viewing changes to salome/cfd_proxy/cfd_proxy_child.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2011-11-24 00:00:08 UTC
  • mfrom: (6.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20111124000008-2vo99e38267942q5
Tags: 2.1.0-3
Install a missing file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//============================================================================
 
2
// Spawn a child process and establish a connection.
 
3
//============================================================================
 
4
 
 
5
/*
 
6
  This file is part of Code_Saturne, a general-purpose CFD tool.
 
7
 
 
8
  Copyright (C) 1998-2011 EDF S.A.
 
9
 
 
10
  This program is free software; you can redistribute it and/or modify it under
 
11
  the terms of the GNU General Public License as published by the Free Software
 
12
  Foundation; either version 2 of the License, or (at your option) any later
 
13
  version.
 
14
 
 
15
  This program is distributed in the hope that it will be useful, but WITHOUT
 
16
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
17
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
18
  details.
 
19
 
 
20
  You should have received a copy of the GNU General Public License along with
 
21
  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
 
22
  Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
23
*/
 
24
 
 
25
#include "cs_config.h"
 
26
 
 
27
// System headers
 
28
 
 
29
#include <errno.h>
 
30
#include <stdio.h>
 
31
#include <stdlib.h>
 
32
#include <string.h>
 
33
#include <sys/types.h>
 
34
#include <sys/stat.h>
 
35
#include <unistd.h>
 
36
#include <sys/wait.h>
 
37
 
 
38
#if defined(HAVE_POSIX_SPAWN)
 
39
#include <spawn.h>
 
40
#endif
 
41
 
 
42
// Local headers
 
43
 
 
44
#include "cfd_proxy_defs.h"
 
45
#include "cfd_proxy_comm.h"
 
46
#include "cfd_proxy_forward.h"
 
47
 
 
48
#include "cfd_proxy_child.h"
 
49
 
 
50
//----------------------------------------------------------------------------
 
51
 
 
52
#ifdef __cplusplus
 
53
extern "C" {
 
54
#if 0
 
55
} /* Fake brace to force back Emacs auto-indentation back to column 0 */
 
56
#endif
 
57
#endif /* __cplusplus */
 
58
 
 
59
//============================================================================
 
60
//                      Local structure definitions
 
61
//============================================================================
 
62
 
 
63
struct _cfd_proxy_child_t {
 
64
 
 
65
  pid_t                  pid;     // Child pid
 
66
  cfd_proxy_comm_t      *comm;    // Associated communicator
 
67
 
 
68
};
 
69
 
 
70
//============================================================================
 
71
//                      Private Function Definitions
 
72
//============================================================================
 
73
 
 
74
//----------------------------------------------------------------------------
 
75
// Wait for child process and print status info
 
76
//----------------------------------------------------------------------------
 
77
 
 
78
static int
 
79
_wait_child(cfd_proxy_child_t  *child,
 
80
            bool                verbose)
 
81
{
 
82
  int status = 0;
 
83
 
 
84
  if (verbose) {
 
85
    cfd_proxy_printf(_("Waiting for child process (%lu) to finish ..."),
 
86
                     (unsigned long)(child->pid));
 
87
    cfd_proxy_printf_flush();
 
88
  }
 
89
 
 
90
  child->pid = waitpid(child->pid, &status, 0);
 
91
 
 
92
  if (status != 0) {
 
93
 
 
94
    if (verbose)
 
95
      cfd_proxy_printf(_(" [error]\n"));
 
96
 
 
97
#if defined(WIFEXITED)
 
98
    if (WIFEXITED(status))
 
99
      cfd_proxy_printf(_("Child exited with status %d\n"),
 
100
                       WEXITSTATUS(status));
 
101
    else if (WIFSIGNALED(status))
 
102
      cfd_proxy_printf(_("Child killed by signal %d\n"), WTERMSIG(status));
 
103
    else if (WIFSTOPPED(status))
 
104
      cfd_proxy_printf(_("Child stopped by signal %d\n"), WSTOPSIG(status));
 
105
#else
 
106
    cfd_proxy_printf(_("Child exited or killed with status %d\n"), status);
 
107
#endif
 
108
    cfd_proxy_printf_flush();
 
109
  }
 
110
  else if (verbose)
 
111
    cfd_proxy_printf(_(" [ok]\n"));
 
112
 
 
113
  cfd_proxy_printf_flush();
 
114
 
 
115
  return status;
 
116
}
 
117
 
 
118
//============================================================================
 
119
//                      Public Function Definitions
 
120
//============================================================================
 
121
 
 
122
//----------------------------------------------------------------------------
 
123
// Spawn a child process and establish a connection
 
124
//
 
125
// returns:
 
126
//   child process handle in case of success, NULL in case of error.
 
127
//----------------------------------------------------------------------------
 
128
 
 
129
cfd_proxy_child_t *
 
130
cfd_proxy_child_start(const char                   *path,
 
131
                      char                   *const argv[restrict],
 
132
                      char                   *const envp[restrict],
 
133
                      cfd_proxy_comm_type_t         comm_type,
 
134
                      int                           comm_verbosity)
 
135
{
 
136
  int i;
 
137
  int retval = 0;
 
138
 
 
139
  cfd_proxy_child_t *child = NULL;
 
140
  char ** _argv = NULL;
 
141
  char *socketopt = NULL;
 
142
  char *keyopt = NULL;
 
143
  const char socketoptbase[] = "--proxy-socket=";
 
144
  const char keyoptbase[] = "--proxy-key=";
 
145
 
 
146
  // Check path
 
147
 
 
148
  {
 
149
    struct stat s;
 
150
 
 
151
    if (stat(path, &s) != 0) {
 
152
      cfd_proxy_error(__FILE__, __LINE__, errno,
 
153
                      _("Impossible to run file: %s"), path);
 
154
      return NULL;
 
155
    }
 
156
 
 
157
    else if (S_ISDIR(s.st_mode)) {
 
158
      cfd_proxy_error(__FILE__, __LINE__, 0,
 
159
                      _("File is a directory: %s"), path);
 
160
      return NULL;
 
161
    }
 
162
 
 
163
    else if (!((S_IXUSR | S_IXGRP | S_IXOTH) & s.st_mode)) {
 
164
      cfd_proxy_error(__FILE__, __LINE__, 0,
 
165
                      _("File is not executable: %s"), path);
 
166
      return NULL;
 
167
    }
 
168
  }
 
169
 
 
170
  // Initialize communicator
 
171
 
 
172
  CFDP_MALLOC(child, 1, cfd_proxy_child_t);
 
173
 
 
174
  child->pid = 0;
 
175
  child->comm = cfd_proxy_comm_initialize(comm_type, comm_verbosity);
 
176
 
 
177
  if (child->comm == NULL) {
 
178
    CFDP_FREE(child);
 
179
    return NULL;
 
180
  }
 
181
 
 
182
  // Add communication info to argv
 
183
 
 
184
  CFDP_MALLOC(socketopt,
 
185
              (  strlen(socketoptbase)
 
186
               + strlen(cfd_proxy_comm_get_name(child->comm)) + 1),
 
187
             char);
 
188
  sprintf(socketopt, "%s%s",
 
189
          socketoptbase,
 
190
          cfd_proxy_comm_get_name(child->comm));
 
191
 
 
192
  CFDP_MALLOC(keyopt, strlen(keyoptbase) + sizeof(int)*8 + 1, char);
 
193
  sprintf(keyopt, "%s%d", keyoptbase, cfd_proxy_comm_get_key(child->comm));
 
194
 
 
195
  for (i = 0; argv[i] != NULL; i++);
 
196
 
 
197
  CFDP_MALLOC(_argv, i + 3, char *);
 
198
 
 
199
  for (i = 0; argv[i] != NULL; i++)
 
200
    _argv[i] = argv[i];
 
201
 
 
202
  _argv[i++] = socketopt;
 
203
  _argv[i++] = keyopt;
 
204
  _argv[i++] = NULL;
 
205
 
 
206
#if defined(HAVE_POSIX_SPAWN)
 
207
 
 
208
  posix_spawn_file_actions_t file_actions;
 
209
  posix_spawnattr_t attrp;
 
210
 
 
211
  posix_spawn_file_actions_init(&file_actions);
 
212
  posix_spawnattr_init(&attrp);
 
213
 
 
214
  retval = posix_spawn(&(child->pid), path, &file_actions, &attrp,
 
215
                       _argv, envp);
 
216
 
 
217
  if (retval != 0)
 
218
    cfd_proxy_error(__FILE__, __LINE__, retval,
 
219
                    _("Error spawning child process %s."), path);
 
220
 
 
221
  posix_spawnattr_destroy(&attrp);
 
222
  posix_spawn_file_actions_destroy(&file_actions);
 
223
 
 
224
#elif defined(HAVE_FORK_EXECVE)
 
225
 
 
226
  child->pid = fork();
 
227
 
 
228
  if (child->pid < 0)
 
229
    cfd_proxy_error(__FILE__, __LINE__, errno,
 
230
                    _("Error spawning child process %s."), path);
 
231
 
 
232
  if (child->pid == 0) {
 
233
 
 
234
    fclose(stderr);
 
235
 
 
236
    retval = execve(path, (char *const *)_argv, envp);
 
237
 
 
238
    if (retval == -1) {
 
239
      cfd_proxy_error(__FILE__, __LINE__, errno,
 
240
                      _("Error spawning child process %s."), path);
 
241
      exit(EXIT_FAILURE);
 
242
    }
 
243
 
 
244
  }
 
245
 
 
246
#endif
 
247
 
 
248
  // Free additional command-line arguments
 
249
 
 
250
  CFDP_FREE(_argv);
 
251
  CFDP_FREE(keyopt);
 
252
  CFDP_FREE(socketopt);
 
253
 
 
254
  // Wait for connection
 
255
 
 
256
  if (child->pid != 0) {
 
257
    retval = cfd_proxy_comm_connect(child->comm,
 
258
                                    "CFD_Proxy_comm_socket");
 
259
    if (retval != 0) {
 
260
      _wait_child(child, false);
 
261
      child->comm = cfd_proxy_comm_finalize(child->comm);
 
262
      CFDP_FREE(child);
 
263
    }
 
264
  }
 
265
 
 
266
  return child;
 
267
}
 
268
 
 
269
//----------------------------------------------------------------------------
 
270
// End connection with a child process and free associated structure
 
271
//----------------------------------------------------------------------------
 
272
 
 
273
int
 
274
cfd_proxy_child_stop(cfd_proxy_child_t **child)
 
275
{
 
276
  cfd_proxy_child_t *_child = *child;
 
277
 
 
278
  int retval = 0;
 
279
 
 
280
  if (_child->comm != NULL)
 
281
    _child->comm = cfd_proxy_comm_finalize(_child->comm);
 
282
 
 
283
  // Wait for child to finish if not already finished
 
284
 
 
285
  retval = _wait_child(_child, true);
 
286
 
 
287
  if (_child->pid == -1) {
 
288
    cfd_proxy_printf(_("\n"));
 
289
    cfd_proxy_error(__FILE__, __LINE__, errno,
 
290
                    _("Error waiting for child process."));
 
291
    retval = 1;
 
292
  }
 
293
 
 
294
  // User info
 
295
 
 
296
  CFDP_FREE(_child);
 
297
 
 
298
  *child = NULL;
 
299
 
 
300
  return retval;
 
301
}
 
302
 
 
303
//----------------------------------------------------------------------------
 
304
// Forward all calls from the client and their responses.
 
305
//----------------------------------------------------------------------------
 
306
 
 
307
void
 
308
cfd_proxy_child_forward_all(cfd_proxy_child_t  *child)
 
309
{
 
310
  if (child->comm != NULL)
 
311
    cfd_proxy_forward_all(child->comm);
 
312
}
 
313
 
 
314
//----------------------------------------------------------------------------
 
315
 
 
316
#ifdef __cplusplus
 
317
}
 
318
#endif /* __cplusplus */
 
319