-
Notifications
You must be signed in to change notification settings - Fork 13
/
popen_noshell_examples.c
154 lines (132 loc) · 4.61 KB
/
popen_noshell_examples.c
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
/*
* popen_noshell: A faster implementation of popen() and system() for Linux.
* Copyright (c) 2009 Ivan Zahariev (famzah)
* Version: 1.0
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; under version 3 of the License.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>.
*/
#include "popen_noshell.h"
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <alloca.h>
/***********************************
* popen_noshell use-case examples *
***********************************
*
* Compile and test via:
* gcc -Wall popen_noshell.c popen_noshell_examples.c -o popen_noshell_examples && ./popen_noshell_examples
*
* If you want to profile using Valgrind, then compile and run via:
* gcc -Wall -g -DPOPEN_NOSHELL_VALGRIND_DEBUG popen_noshell.c popen_noshell_examples.c -o popen_noshell_examples && \
* valgrind -q --tool=memcheck --leak-check=yes --show-reachable=yes --track-fds=yes ./popen_noshell_examples
*/
void satisfy_open_FDs_leak_detection_and_exit() {
/* satisfy Valgrind FDs leak detection for the parent process */
if (fflush(stdout) != 0) err(EXIT_FAILURE, "fflush(stdout)");
if (fflush(stderr) != 0) err(EXIT_FAILURE, "fflush(stderr)");
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
exit(0);
}
void example_reading(int use_compat) {
FILE *fp;
char buf[256];
int status;
struct popen_noshell_pass_to_pclose pclose_arg;
char *cmd = "ls -la /proc/self/fd"; /* used only by popen_noshell_compat(), we discourage this type of providing a command */
/* the command arguments used by popen_noshell() */
char *exec_file = "ls";
char *arg1 = "-la";
char *arg2 = "/proc/self/fd";
char *arg3 = (char *) NULL; /* last element */
char *argv[] = {exec_file, arg1, arg2, arg3}; /* NOTE! The first argv[] must be the executed *exec_file itself */
if (use_compat) {
fp = popen_noshell_compat(cmd, "r", &pclose_arg);
if (!fp) {
err(EXIT_FAILURE, "popen_noshell_compat()");
}
} else {
fp = popen_noshell(exec_file, (const char * const *)argv, "r", &pclose_arg, 0);
if (!fp) {
err(EXIT_FAILURE, "popen_noshell()");
}
}
while (fgets(buf, sizeof(buf)-1, fp)) {
printf("Got line: %s", buf);
}
status = pclose_noshell(&pclose_arg);
if (status == -1) {
err(EXIT_FAILURE, "pclose_noshell()");
} else {
printf("The status of the child is %d. Note that this is not only the exit code. See man waitpid().\n", status);
}
}
void example_writing(int use_compat) {
FILE *fp;
int status;
struct popen_noshell_pass_to_pclose pclose_arg;
char *cmd = "tee -a /tmp/popen-noshell.txt"; /* used only by popen_noshell_compat(), we discourage this type of providing a command */
/* the command arguments used by popen_noshell() */
char *exec_file = "tee";
char *arg1 = "-a";
char *arg2 = "/tmp/popen-noshell.txt";
char *arg3 = (char *) NULL; /* last element */
char *argv[] = {exec_file, arg1, arg2, arg3}; /* NOTE! The first argv[] must be the executed *exec_file itself */
if (use_compat) {
fp = popen_noshell_compat(cmd, "w", &pclose_arg);
if (!fp) {
err(EXIT_FAILURE, "popen_noshell_compat()");
}
} else {
fp = popen_noshell(exec_file, (const char * const *)argv, "w", &pclose_arg, 0);
if (!fp) {
err(EXIT_FAILURE, "popen_noshell()");
}
}
if (fprintf(fp, "This is a test line, my pid is %d\n", (int)getpid()) < 0) {
err(EXIT_FAILURE, "fprintf()");
}
status = pclose_noshell(&pclose_arg);
if (status == -1) {
err(EXIT_FAILURE, "pclose_noshell()");
} else {
printf("The status of the child is %d. Note that this is not only the exit code. See man waitpid().\n", status);
}
printf("Done, you can see the results by executing: cat %s\n", arg2);
}
int main() {
int try_compat;
int try_read;
/*
* Demonstrate all possible options.
*/
for (try_compat = 0; try_compat <= 1; ++try_compat) {
for (try_read = 0; try_read <= 1; ++try_read) {
printf(
"\n** Test: compat=%d, operation='%s'. Press any key to start the test...\n",
try_compat, try_read ? "read" : "write"
);
getchar();
if (try_read) {
example_reading(try_compat);
} else {
example_writing(try_compat);
}
}
}
satisfy_open_FDs_leak_detection_and_exit();
return 0;
}