Tolta opzione inutile dal web server e messa opzione per far andare bene
[gapil.git] / sources / wwwd.c
index 5d22a875b5ddaada1024fb6cb25b900ef5172972..f144eb0bcaff90face8eb8a429a2c5ccddc52f3d 100644 (file)
@@ -68,8 +68,9 @@ int main(int argc, char *argv[])
  * Variables definition  
  */
     int list_fd, conn_fd;
-    int waiting = 0;
     int compat = 0;
+    int reroot = 0;
+    char * rootdir;
     pid_t pid;
     struct sockaddr_in cli_add;
     socklen_t len;
@@ -80,7 +81,7 @@ int main(int argc, char *argv[])
      */
     int i;
     opterr = 0;         /* don't want writing to stderr */
-    while ( (i = getopt(argc, argv, "hdicw:")) != -1) {
+    while ( (i = getopt(argc, argv, "hdicw:r:")) != -1) {
        switch (i) {
        /* 
         * Handling options 
@@ -99,8 +100,9 @@ int main(int argc, char *argv[])
        case 'd':
            debugging = 1;
            break;
-       case 'w':
-           waiting = strtol(optarg, NULL, 10);
+       case 'r':
+           reroot = 1;
+           rootdir = optarg;
            break;
        case '?':   /* unrecognized options */
            printf("Unrecognized options -%c\n",optopt);
@@ -126,6 +128,17 @@ int main(int argc, char *argv[])
     if ( (list_fd = sockbind2(argv[optind], "www", 6, SOCK_STREAM)) < 0) {
        return 1;
     }   
+    /* chroot if requested */
+    if (reroot) {
+       if (chdir(rootdir)) {
+           perror("Cannot find directory to chroot");
+           exit(1);
+       }
+       if (chroot(rootdir)) {
+           perror("Cannot chroot");
+           exit(1);
+       }
+    }
     /* release privileges and go daemon */
     if (setgid(65534) !=0) { /* first give away group privileges */
        perror("cannot give away group privileges");
@@ -147,7 +160,6 @@ int main(int argc, char *argv[])
        PrintErr("listen error");
        exit(1);
     }
-    if (waiting) sleep(waiting);
     /* handle echo to client */
     while (1) {
        /* accept connection */
@@ -282,7 +294,6 @@ void ServPage(int sockfd)
 
     j = 0;
     while (!feof(file)) {
-       printf("Loop %d\n", j++);
        if ( (nleft = full_fread(file, outbuf, 1024)) != 0) {
            if (ferror(file)) {
                fprintf(sock, "HTTP/1.0 %s\n", codes[3]);
@@ -292,7 +303,6 @@ void ServPage(int sockfd)
                return;
            }
        }
-       printf("Loop %d rimasti %d\n", j, nleft);
        if (full_fwrite(sock, outbuf, 1024-nleft) != 0) {
            if (ferror(file)) {
                fprintf(sock, "HTTP/1.0 %s\n", codes[3]);
@@ -303,8 +313,6 @@ void ServPage(int sockfd)
            }   
        }
     }
-    printf("Ok fin qui!");
-//    line = fgets(buffer, MAXLINE, sock);
     fclose(file);
     fclose(sock);
     return;