Back to index

avfs  1.0.1
Functions
server.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int server_init ()

Function Documentation

int server_init ( )

Definition at line 153 of file server.c.

{
    int res;
    int pip[2];
    int pid;
    int nullfd;
    int i;
    char sockname[128];
    int numfd;
    sigset_t newset;
    struct sigaction act;

    sigemptyset(&newset);
    sigprocmask(SIG_SETMASK, &newset, NULL);

    for(i = 0; i < 3; i++) {
        nullfd = open("/dev/null", O_RDONLY);
        if(nullfd == -1) {
            perror("avfs_server: open(/dev/null)");
            exit(1);
        }
    }

    res = pipe(pip);
    if(res == -1) {
        perror("avfs_server: pipe()");
        exit(1);
    }

    pid = fork();
    if(pid == -1) {
        perror("avfs_server: fork()");
        exit(1);
    }

    if(pid != 0) {
        int res;
        char c;

        close(pip[1]);
        res = read(pip[0], &c, 1);
        if(res == -1) {
            perror("avfs_server: read()");
            exit(1);
        }
        if(res == 0)
            exit(1);
        
        exit(0);
    }
    
    setuid(getuid());
    setgid(getgid());
//    chdir("/");
    chdir("/tmp");
    setsid();

    numfd = getdtablesize();
    for(i = 0; i < numfd; i++) {
        if(i != pip[1])
            close(i);
    }

    nullfd = open("/dev/null", O_RDWR);
    if(nullfd == -1)
        exit(1);
    
    for(i = 0; i < 3; i++)
        res = dup2(nullfd, i);

    res = get_sockname(sockname);
    if(res != -1)
        res = socket_init(sockname);

    if(write(pip[1], "m", 1) != 1)
        exit(1);

    if(res == -1)
        exit(0);
    
    close(pip[1]);

    act.sa_handler = exit_handler;
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;
    sigaction(SIGTERM, &act, NULL);
    sigaction(SIGHUP, &act, NULL);
    sigaction(SIGINT, &act, NULL);

    act.sa_handler = SIG_IGN;
    sigaction(SIGPIPE, &act, NULL);

#if 0
    act.sa_handler = bad_handler;
    sigfillset(&act.sa_mask);
    sigaction(SIGQUIT, &act, NULL);
    sigaction(SIGILL, &act, NULL);
    sigaction(SIGFPE, &act, NULL);
    sigaction(SIGABRT, &act, NULL);
    sigaction(SIGBUS, &act, NULL);
    sigaction(SIGSEGV, &act, NULL);
#endif

    return res;
}

Here is the call graph for this function:

Here is the caller graph for this function: