当前位置: IT大杂烩 > 人工智能  > qmail源代码分析之qmail-start.c

qmail源代码分析之qmail-start.c

www.someabcd.com  网友分享于:May 18, 2018 10:50:22 PM
qmail-start.c 分析

PRogrammer:夜未眠
Comefrom:ChongQing Gearbox co.,ltd

qmail-start 是很简单的一个程式,他完成qmail-send,qmail-clean,qmail-lspawn,qmail-rspawn,splogger的启动,并通过管道将他们联系在一起,当然不是网状连接.具体如下

=====================================
qmail-lspawn fd0 <-------- qmail-send fd1
qmail-lspawn fd1 ------ qmail-send fd2
qmail-rspawn fd0 <-------- qmail-send fd3
qmail-rspawn fd1 ------ qmail-send fd4
qmail-clean fd0 <-------- qmail-send fd5
qmail-clean fd1 ------ qmail-send fd6
=====================================
[/code:1:e7358d3ff6]
理解他们之间的关系(注意方向)对于理解qmail-send源代码非常重要。仔细再看一次。

因为其比较简单,所以这里就不对他的源代码作过细的分析:
[code:1:e7358d3ff6]
char *(qsargs[]) = { "qmail-send", 0 };
char *(qcargs[]) = { "qmail-clean", 0 };
char *(qlargs[]) = { "qmail-lspawn", "./Mailbox", 0 };
char *(qrargs[]) = { "qmail-rspawn", 0 };

void die() { _exit(111); }

int pi0[2]; //splogger qmail
int pi1[2]; //qmail-lspawn fd0 <-------- qmail-send fd1
int pi2[2]; //qmail-lspawn fd1 ------ qmail-send fd2
int pi3[2]; //qmail-rspawn fd0 <-------- qmail-send fd3
int pi4[2]; //qmail-rspawn fd1 ------ qmail-send fd4
int pi5[2]; //qmail-clean fd0 <-------- qmail-send fd5
int pi6[2]; //qmail-clean fd1 ------ qmail-send fd6

void close23456() { close(2); close(3); close(4); close(5); close(6); }


//****************//
//因为没有关闭pi0.
//所以所有的子进程都可以通过写pi0来记录maillog.

void closep ipes() {
close(pi1[0]); close(pi1[1]); close(pi2[0]); close(pi2[1]);
close(pi3[0]); close(pi3[1]); close(pi4[0]); close(pi4[1]);
close(pi5[0]); close(pi5[1]); close(pi6[0]); close(pi6[1]);
}

void main(argc,argv)
int argc;
char **argv;
{
if (chdir("/") == -1) die();
umask(077);
if (prot_gid(auto_gidq) == -1) die();

if (fd_copy(2,0) == -1) die();
if (fd_copy(3,0) == -1) die();
if (fd_copy(4,0) == -1) die();
if (fd_copy(5,0) == -1) die();
if (fd_copy(6,0) == -1) die();

if (argv[1]) {
qlargs[1] = argv[1];
++argv;
}

if (argv[1]) {
if (pipe(pi0) == -1) die();
switch(fork()) {
case -1:
die();
case 0:
if (prot_gid(auto_gidn) == -1) die();
if (prot_uid(auto_uidl) == -1) die();
close(pi0[1]);
if (fd_move(0,pi0[0]) == -1) die();//重定向pi0[0]到splogger的fd0
close23456();
execvp(argv[1],argv + 1);//启动splogger
die();
}
close(pi0[0]);
if (fd_move(1,pi0[1]) == -1) die();
}

if (pipe(pi1) == -1) die();
if (pipe(pi2) == -1) die();
if (pipe(pi3) == -1) die();
if (pipe(pi4) == -1) die();
if (pipe(pi5) == -1) die();
if (pipe(pi6) == -1) die();

switch(fork()) {//启动qmail-lspawn
case -1: die();
case 0:
if (fd_copy(0,pi1[0]) == -1) die();
if (fd_copy(1,pi2[1]) == -1) die();
close23456();
closepipes();
execvp(*qlargs,qlargs);
die();
}

switch(fork()) {//启动qmail-rspawn
case -1: die();
case 0:
if (prot_uid(auto_uidr) == -1) die();
if (fd_copy(0,pi3[0]) == -1) die();
if (fd_copy(1,pi4[1]) == -1) die();
close23456();
closepipes();
execvp(*qrargs,qrargs);
die();
}

switch(fork()) {//启动qmail-clean
case -1: die();
case 0:
if (prot_uid(auto_uidq) == -1) die();
if (fd_copy(0,pi5[0]) == -1) die();
if (fd_copy(1,pi6[1]) == -1) die();
close23456();
closepipes();
execvp(*qcargs,qcargs);
die();
}

if (prot_uid(auto_uids) == -1) die();
if (fd_copy(0,1) == -1) die(); //重定向管道,把qmail-send 与上面各进程联系起来。
if (fd_copy(1,pi1[1]) == -1) die();
if (fd_copy(2,pi2[0]) == -1) die();
if (fd_copy(3,pi3[1]) == -1) die();
if (fd_copy(4,pi4[0]) == -1) die();
if (fd_copy(5,pi5[1]) == -1) die();
if (fd_copy(6,pi6[0]) == -1) die();
closepipes();
execvp(*qsargs,qsargs);//最后启动qmail-send
die();
}
,

发布此文章仅为传递网友分享,不代表本站观点,若侵权请联系我们删除,本站将不对此承担任何责任。
Copyright ©2018  IT大杂烩  版权所有  京ICP备11030978号-1 网站地图