小蝦寫的cgi進(jìn)程管理工具_(dá)第1頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、小蝦寫的cgi進(jìn)程管理工具ilude sys/socket.h include sys/wait.h include fcntl.h include arpa/inet.h include grp.h include .h include unistd.h define closesocket close include stdio.h include stdlib.h include getopt.h include sing.h include pthread.h include errno.h define max_processes 1024 ic const char version

2、 = "$revision: 0.01 $" static char* prog_name; int number = 1; int port = 8000; char *ip = "" char *user = "" char *root = "" char *path = "" char *group = "" int listen_fd; struct sockar_in listen_addr; int process_fpmax_processes

3、; int process_x = 0; pthread_t threadsmax_processes; static struct option longopts = "help", no_argument, null, 'h', "version", no_argument, null, 'v', "number", requir_argument, null, 'n', "ip", required_argument, null, 'i', &q

4、uot;port", required_argument, null, 'p', "user", required_argument, null, 'u', "group", required_argument, null, 'g', "root", required_argument, null, 'r', null, 0, null, 0 static char opts = "hvnipugr" static void usage(fi

5、le* where) fprintf(where, "" "usage: %s path -n number -i ip -p portn" "manage fastcgi processes.n" "n" " -n, -number number of processes to keepn" " -i, -ip ip address to bindn" " -p, -port port to bind, default is 8000n" "

6、-u, -user start processes using specified linux usern" " -g, -group start processes using specified linux groupn" " -r, -root change root direcotry for the processesn" " -h, -help output usage infoation and itn" " -v, -version output version information and ex

7、itn" "", prog_name); exit(where = stderr ? 1:0); static void print_version() printf("%s %sn fastcgi process managern copyright 2010 xn compiled on %sn ", prog_name, version, _date_); exit(0); static int try_to_bind() listen_addr.sin_family = pf_inet; listen_addr.si

8、n_addr.s_addr = inet_addr( ip ); listen_addr.sin_port = htons( port ); listen_fd = socket(af_inet, sock_stream, 0); if (-1 = bind(listen_fd, (struct sockaddr*) listen_addr, sizeof(struct sockaddr_in) ) fprintf(stderr, "failed to bind %s:%dn", ip, port ); return -1; listen(listen_fd, max_pr

9、ocesses); return 0; static void* spawn_process(void* arg) int idx = process_idx +, ret; while(1) ret = fork(); switch(ret) case 0: /child /* change uid from root to other user */ if(getuid()=0) struct group *grp = null; struct *pwd = null; if (*user) if (null = (pwd = getpwnam(user) fprintf(stderr,

10、"fcgi %s %sn", "can't username", user); exit(-1); if (pwd- pw_uid = 0) fprintf(stderr, "fcgi %sn", "what? dest uid = 0?" ); exit(-1); if (*group) if (null = (grp = getgrnam(group) fprintf(stderr, "fcgi %s %sn", "can't find groupname"

11、;, group); exit(1); if (grp- gr_gid = 0) fprintf(stderr, "fcgi %sn", "what? dest gid = 0?" ); exit(1); /* do the change before we do the chroot() */ gid(grp- gr_gid); setgroups(0, null); if (user) initgroups(user, grp- gr_gid); if (*root) if (-1 = chroot(root) fprintf(stderr, &qu

12、ot;fcgi %s %sn", "can't change root", root); exit(1); if (-1 = chdir("/") fprintf(stderr, "fcgi %s %sn", "can't change dir to", root); exit(1); /* drop root privs */ if (*user) setuid(pwd- pw_uid); int max_fd = 0, i=0; / set stdin to listen_fd clo

13、se(stdin_fileno); p2(listen_fd, stdin_fileno); close(listen_fd); / set stdout and stderr to dummy fd max_fd = open("/dev/null", o_rdwr); close(stderr_fileno); dup2(max_fd, stderr_fileno); close(max_fd); max_fd = open("/dev/null", o_rdwr); close(stdout_fileno); dup2(max_fd, stdout

14、_fileno); close(max_fd); / close other handles for(i=3; i max_fd; i+) close(i); char *b = malloc(strlen("exec ") + strlen(path) + 1); stry(b, "exec "); str(b, path); /* exec the cgi */ execl("/bin/sh", "sh", "-c", b, (char *)null); exit(errno); break

15、; case -1: fprintf(stderr, "fcgi fork failedn"); return null; default: struct val tv = 0, 100 * 1000 ; int status; select(0, null, null, null, tv); switch(waitpid(ret, status, wnohang) case 0: printf("fcg spawned process %s: %dn", path, ret); break; case -1: fprintf(stderr, "

16、;fcgi waitpid failedn"); return null; default: if (wifexited(status) fprintf(stderr, "fcgi child exited with: %dn", wexitstatus(status); ee if (wifsignaled(status) fprintf(stderr, "fcgi child signaled: %dn", wtermsig(status); else fprintf(stderr, "fcgi child died somehow: %dn", status); return null; /wait for child process to exit process_fpidx = ret; waitpid(ret, status, 0); process_fpidx = 0; static int start_processes() int i; pthread_attr_t attr; pthread_attr_init( attr); pth

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論