- 论坛徽章:
- 0
|
启动apache ssl站点的时候要输入密码,不方便做到脚本里面,所以写个程序来启动,但是子进程(也就是apache)无法获取输入。后来发现可以用openssl把带密码的私钥转化成不带密码的私钥。但是还是想知道这个程序的问题在哪里,对多进程编程不是太熟悉,请各位指点一下,谢谢!
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
char *read_str="Enter pass phrase", *write_str="ssl-private-key";
char buf[1024];
int p2s_fds[2], s2p_fds[2], sub_read, sub_write, n;
if(pipe(p2s_fds) || pipe(s2p_fds)){
printf("pipe err, %s\n", strerror(errno));
exit(-1);
}
int rt = fork();
if(rt == -1){
printf("fork err, %s\n", strerror(errno));
exit(-1);
}else if(rt == 0){
close(p2s_fds[1]);
close(s2p_fds[0]);
dup2(s2p_fds[1], STDOUT_FILENO);
dup2(s2p_fds[1], STDERR_FILENO);
dup2(p2s_fds[0], STDIN_FILENO);
close(s2p_fds[1]);
close(p2s_fds[0]);
printf("sub before exec\n");
if(execl("/usr/sbin/apache2", "apache2", "-k", "start", (char*)0) == -1){
printf("execl err, %s\n", strerror(errno));
exit(-1);
}
}else{
close(p2s_fds[0]);
close(s2p_fds[1]);
sub_read = s2p_fds[0];
sub_write = p2s_fds[1];
while( (n=read(sub_read, buf, sizeof(buf)))){
buf[n] = '\0';
printf("parent read:%s\n", buf);
if(strstr(buf, read_str)){
printf("parent write:%s\n", write_str);
if(write(sub_write, write_str, strlen(write_str))==-1){
printf("parent write err, %s\n", strerror(errno));
exit(-1);
}
}
}
sleep(1);
}
return 0;
} |
|
|