[go: up one dir, main page]

Menu

[b0c624]: / src / flom_exec.c  Maximize  Restore  History

Download this file

157 lines (138 with data), 5.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright (c) 2013-2016, Christian Ferrari <tiian@users.sourceforge.net>
* All rights reserved.
*
* This file is part of FLoM.
*
* FLoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*
* FLoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FLoM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#include "flom_errors.h"
#include "flom_exec.h"
#include "flom_trace.h"
/* set module trace flag */
#ifdef FLOM_TRACE_MODULE
# undef FLOM_TRACE_MODULE
#endif /* FLOM_TRACE_MODULE */
#define FLOM_TRACE_MODULE FLOM_TRACE_MOD_EXEC
int flom_exec(gchar **const command_argv, const char *element,
int *child_status)
{
enum Exception { COMMAND_ARGV_IS_NULL
, FORK_ERROR
, MALLOC_ERROR
, EXECVP_ERROR
, WAIT_ERROR
, NONE } excp;
int ret_cod = FLOM_RC_INTERNAL_ERROR;
pid_t pid = -1;
gchar *local_element = NULL;
FLOM_TRACE(("flom_exec\n"));
TRY {
if (NULL == command_argv) {
FLOM_TRACE(("flom_exec: command_argv cannot be NULL\n"));
THROW(COMMAND_ARGV_IS_NULL);
}
/* fork */
if (-1 == (pid = fork())) {
THROW(FORK_ERROR);
} else if (0 == pid) {
/* child process, preparing for execv... */
const char *path = command_argv[0];
char **argv;
guint i, num, el;
num = g_strv_length(command_argv);
/* check additional option (resource set locked element) **/
if (NULL == element)
el = 0;
else {
el = 1;
local_element = g_strdup((const gchar *)element);
}
for (i=0; i<num; ++i)
FLOM_TRACE(("flom_exec-child: command_argv[%u]='%s'\n",
i, command_argv[i]));
if (el)
FLOM_TRACE(("flom_exec-child: element='%s'\n", local_element));
if (NULL == (argv = (char **)malloc((++num+el)* sizeof(char *))))
THROW(MALLOC_ERROR);
for (i=0; i<num-1; ++i) {
argv[i] = command_argv[i];
}
if (el)
argv[num-1] = local_element;
argv[num-1+el] = (char *)NULL;
FLOM_TRACE(("flom_exec-child: path='%s'\n", path));
for (i=0; i<num-1+el; ++i)
FLOM_TRACE(("flom_exec-child: argv[%u]='%s'\n", i, argv[i]));
/* execvp */
if (-1 == execvp(path, argv)) {
/* print a warning on terminal */
g_warning("Unable to execute command '%s'\n", path);
THROW(EXECVP_ERROR);
}
} else {
int status;
pid_t child_pid;
/* father process */
FLOM_TRACE(("flom_exec-father: child pid=" PID_T_FORMAT "\n",
pid));
/* waiting child termination */
FLOM_TRACE(("flom_exec-father: waiting child termination\n"));
if (-1 == (child_pid = wait(&status)))
THROW(WAIT_ERROR);
*child_status = WEXITSTATUS(status);
FLOM_TRACE(("flom_exec-father: child exit status is %d\n",
*child_status));
}
THROW(NONE);
} CATCH {
switch (excp) {
case COMMAND_ARGV_IS_NULL:
ret_cod = FLOM_RC_NULL_OBJECT;
break;
case FORK_ERROR:
ret_cod = FLOM_RC_FORK_ERROR;
break;
case MALLOC_ERROR:
ret_cod = FLOM_RC_MALLOC_ERROR;
break;
case EXECVP_ERROR:
ret_cod = FLOM_RC_EXECVP_ERROR;
break;
case WAIT_ERROR:
ret_cod = FLOM_RC_WAIT_ERROR;
break;
case NONE:
ret_cod = FLOM_RC_OK;
break;
default:
ret_cod = FLOM_RC_INTERNAL_ERROR;
} /* switch (excp) */
} /* TRY-CATCH */
FLOM_TRACE(("flom_exec-%s/excp=%d/"
"ret_cod=%d/errno=%d\n",
(0 == pid ? "child" : "father"),
excp, ret_cod, errno));
/* free dynamic memory */
g_free(local_element);
if (0 == pid && FLOM_RC_OK != ret_cod)
exit(FLOM_ES_UNABLE_TO_EXECUTE_COMMAND);
return ret_cod;
}