-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.c
More file actions
151 lines (114 loc) · 2.7 KB
/
command.c
File metadata and controls
151 lines (114 loc) · 2.7 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
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
void ignore_handler(int sig){
(void)printf("sig %d ignored\n", sig);
}
int command(const char *string, char *outbuf, int outlen, char *errbuf, int errlen)
{
//ignore SIGINT and SIGQUIT
// signal(SIGINT, ignore_handler);
// signal(SIGQUIT, ignore_handler);
struct sigaction act;
memset(&act, 0, sizeof(struct sigaction));
act.sa_handler = SIG_IGN;
if(sigaction(SIGINT, &act, NULL) == -1){
perror("sigaction SIGINT");
return -1;
}
if(sigaction(SIGQUIT, &act, NULL) == -1){
perror("sigaction SIGQUIT");
return -1;
}
//block SIGCHLD
sigset_t newmask, oldmask;
sigemptyset(&newmask);
sigaddset(&newmask, SIGCHLD);
if( (sigprocmask(SIG_BLOCK, &newmask, &oldmask)) < 0) {
perror("SIG_BLOCK error");
return -1;
}
if(string == NULL){
return -1;
}
int out_fd[2];
int err_fd[2];
pid_t pid;
//FILE *fp;
//fp = popen(string, "r+");
if(pipe(out_fd) == -1 || pipe(err_fd) == -1){
return -1;
}
if((pid = fork()) == -1){
close(out_fd[0]);
close(out_fd[1]);
close(err_fd[0]);
close(err_fd[1]);
return -1;
}
if(pid == 0){ //child process
//close write ends
close(out_fd[0]);
close(err_fd[0]);
//duplicate the file descriptors of the read end of the pipes
//onto the respective stdin & stdout file numbers
dup2(out_fd[1], STDOUT_FILENO);
dup2(err_fd[1], STDERR_FILENO);
//tokenize string into argument for execvp
char *args[2];
int i = 0;
char *token = strtok(strdup(string), " "); //strdup for a const string
while (token != NULL && i < 2){
args[i++] = token;
token = strtok(NULL, " ");
}
args[i] = NULL;
//execute command
if(execvp(args[0], args) == -1){
perror("execvp");
return -1;
}
}else if (pid > 0) { //parent process
close(out_fd[1]);
close(err_fd[1]);
ssize_t n;
if((n = read(out_fd[0], outbuf, outlen)) >= 0){
outbuf[n] = '\0';
} else{
outbuf[0] = '\0';
}
close(out_fd[0]);
if((n = read(err_fd[0], errbuf, errlen)) >= 0){
errbuf[n] = '\0';
} else{
errbuf[0] = '\0';
}
close(err_fd[0]);
int status;
if(waitpid(pid, &status, 0) == -1){
return status;
}
/*
* return outside of if statement so that process will remove the signal block and ignore
*/
//return 0;
}
//restore signal mask and unblock SIGCHILD
if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0){
perror("SIG_STMASK error");
}
//restore SIGINT and SIGQUIT to default
act.sa_handler = SIG_DFL;
if(sigaction(SIGINT, &act, NULL) == -1){
perror("sigaction SIGINT DFL");
return -1;
}
if(sigaction(SIGINT, &act, NULL) == -1){
perror("sigaction SIGINT DFL");
return -1;
}
return 0;
}