- 论坛徽章:
- 0
|
问题: 为什么我使用守护进程,列表目录不是当前目录?
注销initDaemon函数,一切都正常了,请教大家是怎么回事? 万分感谢
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/types.h>
- #include <netdb.h>
- #include <sys/utsname.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/wait.h>
- #define MAXCLIENT 50
- #define SERVERPORT 80
- #define DEFAULTHTML index.html
- extern int pthreadAction(char * request, int cfd);
- extern int isDir(char *filepath);
- extern void header();
- extern int openFile(char *path,int cfd);
- int main(int argc,char *agrv[]) {
- int serverfd,clientfd;
- FILE * cfp;
- char request[BUFSIZ];
- pid_t pid;
- //initDaemon();
- serverfd = initSocket(SERVERPORT);
- while (1) {
- clientfd = accept(serverfd,NULL,NULL);
- if ((pid =fork()) < 0) {
- die("fork");
- }
- if (pid == 0){
- close(serverfd);
- cfp = fdopen(clientfd,"r");
- fgets(request,BUFSIZ,cfp);
- pthreadAction(request,clientfd);
- fclose(cfp);
- close(clientfd);
- _exit(0);
- }else {
- if(waitpid(pid,NULL,0)!= pid){
- die("wait");
- }
- close(clientfd);
- }
- }
- close(serverfd);
- }
- int pthreadAction(char * request, int cfd) {
- char cmd[BUFSIZ],filepath[BUFSIZ];
- strcpy(filepath,".");
- if((sscanf(request,"%s%s",cmd,(filepath+1))) != 2) {
- // exit;
- }
- printf("request:%s",request);
- printf("cmd:%s\n",cmd);
- printf("filepath:%s\n",filepath);
- if (strcmp(cmd,"GET") != 0){
- return;
- }
- if ((isDir(filepath)) == 0) {
- if ((listDir(filepath,cfd)) == -1) {
- die("listdir");
- }
- }else{
- if ((openFile(filepath,cfd)) != 0){
- die("openFile");
- }
- }
- }
- int openFile(char * path,int cfd) {
- FILE * cfp;
- cfp = fdopen(cfd,"w");
- header(cfp,"text/html;charset=utf-8");
- fprintf(cfp,"<html><title>我爱你,WEBSERVER。</title><body><br><p>我是谁??</p></body></html>");
- fprintf(cfp,"<a href=\"\">test</a>");
- fclose(cfp);
- return 0;
- }
- void choiceType(char * filepath) {
- }
- int isDir(char *filepath) {
- struct stat info;
- if (stat(filepath,&info)!=-1 && S_ISDIR(info.st_mode)) {
- return 0;
- }else{
- return -1;
- }
- }
- int listDir(char *path,int cfd) {
- FILE * cfp;
- cfp=fdopen(cfd,"w");
- header(cfp,"text/plain;charset=utf-8");
- fflush(cfp);
- dup2(cfd,1);
- dup2(cfd,2);
- if (execlp("ls","ls","-l",NULL,NULL) == -1) {
- die("execlp");
- return -1;
- }
- fclose(cfp);
- return 0;
- }
- void header(FILE *cfp,char * type) {
- fprintf(cfp,"HTTP/1.0 200 OK\r\n");
- fprintf(cfp,"Content-type: %s\r\n",type);
- fprintf(cfp,"\r\n");
- }
- void error404() {
- }
- // install daemon service
- int initDaemon(void) {
- pid_t pid;
- int i;
- if ((pid = fork()) < 0) {
- die("System fork");
- }
- if (pid != 0) {
- _exit(0);
- }
- if ((setsid()) == -1){
- die("setsid");
- }
- if ((pid = fork()) < 0) {
- die("System fork");
- }
- if (pid != 0) {
- _exit(0);
- }
- if ((chdir("/tmp")) == -1) {
- die("chdir");
- }
- for (i =0; i < 3; i++ ){
- close(i);
- }
- if ((umask(0)) == -1){
- die("umask");
- }
- return 0;
- }
- // End initdaemon
- // init Socket service
- int initSocket(int ServerPort) {
- int serverfd;
- struct sockaddr_in serveraddr;
- int sockopt = 1;
- memset(&serveraddr,0,sizeof(serveraddr));
- serveraddr.sin_family = AF_INET;
- serveraddr.sin_port = htons(ServerPort);
- serveraddr.sin_addr.s_addr = INADDR_ANY;
- if ((serverfd = socket(AF_INET,SOCK_STREAM,0)) == -1) {
- die("socket");
- }
- setsockopt(serverfd,SOL_SOCKET,SO_REUSEADDR, &sockopt, sizeof(sockopt));
- if ((bind(serverfd,(struct sockaddr * )&serveraddr,sizeof(serveraddr))) == -1) {
- die("bind");
- }
- if ((listen(serverfd,MAXCLIENT)) == -1) {
- die("listen");
- }
- return serverfd;
- }
- // system die & return
- int die(const char * msg) {
- perror(msg);
- exit(1);
- }
复制代码 |
|