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
|
/**********************************************************************
Copyright 2019 Andrey V.Kosteltsev
Licensed under the Radix.pro License, Version 1.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://radix.pro/licenses/LICENSE-1.0-en_US.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
**********************************************************************/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <error.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdarg.h>
#include <unistd.h>
#include <msglog.h>
static void xexec( const char *cmd )
{
char *argv[4];
const char *shell = getenv ("SHELL");
if( !shell ) shell = "/bin/sh";
argv[0] = (char *) shell;
argv[1] = (char *) "-c";
argv[2] = (char *) cmd;
argv[3] = NULL;
execv( shell, argv );
/******************************************
xexec() is called by child process, and
here child process faced to FATAL error:
*/
logmsg( errlog, MSG_FATAL, "%s: Cannot exec", cmd );
exit( EXIT_FAILURE );
}
static pid_t xfork( void )
{
pid_t p = fork();
if( p == (pid_t) -1 )
{
FATAL_ERROR( "Cannot %s", "fork" );
}
return p;
}
pid_t sys_exec_command( const char *cmd )
{
pid_t pid = xfork();
if( pid != 0 )
{
return pid;
}
xexec( cmd );
return pid; /* only to avoid compilaton warning */
}
/*****************************************************************
sys_wait_command() - Wait for pid.
Return values:
-------------
0 - SUCCESS
>=1 - status returned by child process
-1 - Child terminated on signal
-2 - Child terminated on unknown reason
-3 - Cannot waitpid: waitpid() retusrs -1
Error message with SIZE length saved into *ERRMSG buffer.
*****************************************************************/
int sys_wait_command( pid_t pid, char *errmsg, size_t size )
{
int status;
if( pid < 0 ) return (pid_t) -1;
while( waitpid( pid, &status, 0 ) == -1 )
if( errno != EINTR )
{
if( errmsg && size ) {
(void)snprintf( errmsg, size, "PID %lu: Cannot %s", (unsigned long)pid, "waitpid" );
}
return (int) -3;
}
if( WIFEXITED( status ) )
{
if( WEXITSTATUS (status) )
{
if( errmsg && size ) {
(void)snprintf( errmsg, size, "PID %lu: Child returned status %d", (unsigned long)pid, WEXITSTATUS( status ) );
}
return (int) WEXITSTATUS( status );
}
}
else if( WIFSIGNALED( status ) )
{
if( errmsg && size ) {
(void)snprintf( errmsg, size, "PID %lu: Child terminated on signal %d", (unsigned long)pid, WTERMSIG( status ) );
}
return (int) -1;
}
else
{
if( errmsg && size ) {
(void)snprintf( errmsg, size, "PID %lu: Child terminated on unknown reason", (unsigned long)pid );
}
return (int) -2;
}
return 0;
}
|