在Linux系统中,如果需要在应用层执行系统命令并读取系统输出,通常需要使用popen系列函数或system系列函数,详见我的另一篇博客:Linux中的shell命令执行-CSDN博客
一个简单的例子如下:
static int systemcmd(const char *cmd, char *result, int res_len) {
// 参数检查
if (cmd == NULL || strlen(cmd) >= sizeof(result) || result == NULL || res_len <= 0) {
printf("Invalid input parameters\n");
return -1;
}
// 使用popen和fgets读取命令输出
char buf_ps[1024] = {0};
FILE *ptr;
if ((ptr = popen(cmd, "r")) != NULL) {
while (fgets(buf_ps, 1024, ptr) != NULL) {
if (strlen(result) + strlen(buf_ps) > (unsigned int)res_len)
break;
strcat(result, buf_ps);
}
pclose(ptr);
}
// 检查输出长度
if (strlen(result) == 0) {
printf("Command produced no output\n");
return 1;
}
return 0;
}
这个用popen和fgets封装的systemcmd函数看起来没有问题,但是在cmd为某些特定的命令条件下,fgets函数可以正常读取到输出,但是fgets子进程无法关闭,导致pclose函数释放ptr失败;
通常,pclose函数的执行并不会加LOG打印,因此当pclose失败时其实也不影响函数执行系统命令、读取输出的功能,但当系统中大量调用该接口函数时,未正常关闭的指针过多,就会造成无法预知的错误(如系统崩溃),因此,在实际开发中,建议增加pclose的执行结果打印;
但是,这只能发现问题,并不能规避问题,那么,怎么规避问题,解决阻塞呢?
看一下pclose函数的底层实现:
int pclose(FILE *stream)
{
int stat;
pid_t pid;
pid = <pid for process created for stream by popen()>
(void) fclose(stream);
while (waitpid(pid, &stat, 0) == -1) {
if (errno != EINTR){
stat = -1;
break;
}
}
return(stat);
}
实际上,pclose函数不能正常关闭正是由于waitpid并不能成功执行,那为什么不把pclose拆解,自己用waitpid函数,用更细的粒度来判断子进程的结束状态呢?按照这个思路,重新实现一下systemcmd函数,有几个需要注意的问题:
- 如果自己实现更细粒度的pclose函数,那popen函数是不是也需要对应的更细粒度的调整?
- popen函数的细粒度控制如何实现?
- waitpid无期限等待的方法是否合理?如何增加超时机制?
按照这些思考,重新实现systemcmd函数,修改如下:
- 使用了
fork
、pipe
、dup2
、execlp
和select
系统调用来实现更细粒度的popen控制。 - 创建了一个子进程来执行命令,并通过管道将命令的输出传递给父进程。
- 使用
select
系统调用设置了一个超时,以便在命令输出长时间没有到达时,函数不会无限期地等待。 - 使用
waitpid
等待子进程结束,并检查子进程的退出状态。
static int systemcmd(const char *cmd, char *result, int res_len) {
char buf_ps[1024] = {0};
pid_t pid;
int status;
int pipefd[2];
if (cmd == NULL || strlen(cmd) >= sizeof(buf_ps) || result == NULL || res_len <= 0) {
fprintf(stderr, "Invalid input parameters\n");
return -1;
}
if (pipe(pipefd) == -1) {
fprintf(stderr, "Failed to create pipe\n");
return -1;
}
pid = fork();
if (pid == -1) {
fprintf(stderr, "Failed to fork process\n");
close(pipefd[0]);
close(pipefd[1]);
return -1;
}
if (pid == 0) { // Child process
close(pipefd[0]); // Close read end of the pipe
// Redirect stdout to the write end of the pipe
if (dup2(pipefd[1], STDOUT_FILENO) == -1) {
fprintf(stderr, "Failed to duplicate file descriptor\n");
exit(EXIT_FAILURE);
}
// Execute the command
execlp("/bin/sh", "sh", "-c", cmd, NULL);
fprintf(stderr, "Failed to execute command: %s\n", cmd);
exit(EXIT_FAILURE);
} else { // Parent process
close(pipefd[1]); // Close write end of the pipe
// Set up file descriptor set for select
fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(pipefd[0], &read_fds);
struct timeval timeout;
timeout.tv_sec = 2; // Timeout for 2 seconds
timeout.tv_usec = 0;
// Wait for data to become available on the pipe or timeout
int ready = select(pipefd[0] + 1, &read_fds, NULL, NULL, &timeout);
if (ready == -1) {
fprintf(stderr, "Error in select()\n");
close(pipefd[0]);
return -1;
} else if (ready == 0) {
fprintf(stderr, "Timeout waiting for data on pipe\n");
close(pipefd[0]);
return -1;
}
// Read data from the pipe
int bytes_read = read(pipefd[0], buf_ps, sizeof(buf_ps));
if (bytes_read == -1) {
fprintf(stderr, "Error reading from pipe\n");
close(pipefd[0]);
return -1;
}
// Check if there's enough space in result for the new data
int remaining_space = res_len - strlen(result) - 1;
if (bytes_read > remaining_space) {
fprintf(stderr, "Not enough space in result buffer for all output\n");
close(pipefd[0]);
return -1;
}
// Append read data to the result buffer
strncat(result, buf_ps, bytes_read);
// Close read end of the pipe
close(pipefd[0]);
}
// Wait for the child process to finish
waitpid(pid, &status, 0);
printf("Child process finished, status: %d\n", status);
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) != 0) {
fprintf(stderr, "Command exited with status: %d\n", WEXITSTATUS(status));
return -1;
}
} else if (WIFSIGNALED(status)) {
fprintf(stderr, "Command terminated by signal: %d\n", WTERMSIG(status));
return -1;
}
if (strlen(result) == 0) {
fprintf(stderr, "Command produced no output\n");
return 1; // Return a non-zero value to indicate no output
}
printf("Command executed successfully\n");
return 0; // Success
}