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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/*
* Utility functions for the logger PMDA.
*
* Copyright (c) 2011 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program 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.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include "pmapi.h"
#include "impl.h"
#include "util.h"
void
rstrip(char *str)
{
char *ptr;
/* Remove all trailing whitespace. Set ptr to last char of
* string. */
ptr = str + strlen(str) - 1;
/* While trailing whitespace, move back. */
while (ptr >= str && isspace((int)*ptr)) {
--ptr;
}
*(ptr+1) = '\0'; /* Now set '\0' as terminal byte. */
}
char *
lstrip(char *str)
{
/* While leading whitespace, move forward. */
char *ptr = str;
while (*ptr != '\0' && isspace((int)*ptr)) {
ptr++;
}
return ptr;
}
int
start_cmd(const char *cmd, pid_t *ppid)
{
pid_t child_pid;
int rc;
int pipe_fds[2];
#define PARENT_END 0 /* parent end of the pipe */
#define CHILD_END 1 /* child end of the pipe */
#define STDOUT_FD 1 /* stdout fd */
/* FIXME items:
* (1) Should we be looking to handle shell metachars
* differently? Perhaps we should just allow isalnum()||isspace()
* chars only.
* (2) Do we need to clean up the environment in the child before
* the exec()? Remove things like IFS, CDPATH, ENV, and BASH_ENV.
*/
#ifdef PCP_DEBUG
if (pmDebug & DBG_TRACE_APPL0)
__pmNotifyErr(LOG_INFO, "%s: Trying to run command: %s", __FUNCTION__,
cmd);
#endif
/* Create the pipes. */
#if defined(HAVE_PIPE2)
rc = pipe2(pipe_fds, O_CLOEXEC|O_NONBLOCK);
if (rc < 0) {
__pmNotifyErr(LOG_ERR, "%s: pipe2() returned %s", __FUNCTION__,
strerror(-rc));
return rc;
}
#else
rc = pipe(pipe_fds);
if (rc < 0) {
__pmNotifyErr(LOG_ERR, "%s: pipe() returned %s", __FUNCTION__,
strerror(-rc));
return rc;
}
/* Set the right flags on the pipes. */
if (fcntl(pipe_fds[PARENT_END], F_SETFL, O_NDELAY) < 0
|| fcntl(pipe_fds[CHILD_END], F_SETFL, O_NDELAY) < 0) {
__pmNotifyErr(LOG_ERR, "%s: fcntl() returned %s", __FUNCTION__,
strerror(-rc));
return rc;
}
if (fcntl(pipe_fds[PARENT_END], F_SETFD, O_CLOEXEC) < 0
|| fcntl(pipe_fds[CHILD_END], F_SETFD, O_CLOEXEC) < 0) {
__pmNotifyErr(LOG_ERR, "%s: fcntl() returned %s", __FUNCTION__,
strerror(-rc));
return rc;
}
#endif
/* Create the new process. */
child_pid = fork();
if (child_pid == 0) { /* child process */
int i;
/* Duplicate our pipe fd onto stdout of the child. Note that
* this clears O_CLOEXEC, so the new stdout should stay open
* when we call exec(). */
if (pipe_fds[CHILD_END] != STDOUT_FD) {
if (dup2(pipe_fds[CHILD_END], STDOUT_FD) < 0) {
__pmNotifyErr(LOG_ERR, "%s: dup2() returned %s", __FUNCTION__,
strerror(errno));
_exit(127);
}
}
/* Close all other fds. */
for (i = 0; i <= pipe_fds[CHILD_END]; i++) {
if (i != STDOUT_FD) {
close(i);
}
}
/* Actually run the command. */
execl ("/bin/sh", "sh", "-c", cmd, (char *)NULL);
_exit (127);
}
else if (child_pid > 0) { /* parent process */
close (pipe_fds[CHILD_END]);
if (ppid != NULL) {
*ppid = child_pid;
}
}
else if (child_pid < 0) { /* fork error */
int errno_save = errno;
__pmNotifyErr(LOG_ERR, "%s: fork() returned %s", __FUNCTION__,
strerror(errno_save));
close (pipe_fds[PARENT_END]);
close (pipe_fds[CHILD_END]);
return -errno_save;
}
return pipe_fds[PARENT_END];
}
int
stop_cmd(pid_t pid)
{
int rc;
pid_t wait_pid;
int wstatus;
__pmNotifyErr(LOG_INFO, "%s: killing pid %" FMT_PID, __FUNCTION__, pid);
/* Send the TERM signal. */
rc = kill(pid, SIGTERM);
__pmNotifyErr(LOG_INFO, "%s: kill returned %d", __FUNCTION__, rc);
/* Wait for the process to go away. */
do {
wait_pid = waitpid (pid, &wstatus, 0);
__pmNotifyErr(LOG_INFO, "%s: waitpid returned %d", __FUNCTION__,
(int)wait_pid);
} while (wait_pid == -1 && errno == EINTR);
/* Return process exit status. */
return wstatus;
}
|