Log In
New Account
  
 
Home My Page Project Tree Code Snippets Project Openings NUTSS
 
 
Summary Tracker Lists CVS Files
 

CVS | Administration


File:  [GForge:cvsroot/nutss] / libnutss / examples / nutssd.c
Revision 1.3: download - view: text, annotated - select for diffs
Fri May 4 09:08:18 2007 UTC (9 years, 11 months ago) by saikat
Branches: MAIN
CVS tags: HEAD
javasock now builds under linux

configure.ac, include/transport.h, src/Makefile.am, src/javasock.c,
src/transport.c, src/java/net/nutss/Makefile.am:
- Hook java support into linux build

examples/nutssd.c, doc/nutssd.1, examples/nutssd.init:
- Redirect stdout and srderr to a logfile

/**
 * $Id: nutssd.c,v 1.3 2007/05/04 09:08:18 saikat Exp $
 * Copyright (C) 2007  Saikat Guha <saikat@cs.cornell.edu>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 **/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <semaphore.h>
#include <string.h>
#include <errno.h>

#define _GNU_SOURCE
#include <getopt.h>

#include "nutss.h"

sem_t sem;

void usage(char *argv0);

void
usage(char *argv0)
{
    printf("Usage:\n"
        "\t%s [OPTIONS] [SERVICES] user@domain\n"
        "\n"
        "Services:\n"
        "  --stuntd,-s\t\tEnable STUNT server\n"
        "  --turnd,-t\t\tEnable TURN server\n"
        "  --swaplinksd,-w\tEnable Swaplinks server\n"
        "\n"
        "Options:\n"
        "  --registrar,-r server:port\tUse sip registrar at server\n"
        "  --password,-p pass\t\tUse password for registering with proxy\n"
        "  --logfile,-l file\t\tSend log output to file\n"
        "  --nofork,-n\t\t\tDon't fork\n",
        "nutssd");
    exit(1);
}

void signal_handler(int sig)
{
    switch(sig) {
        case SIGHUP:
            break;
        case SIGTERM:
            exit(0);
            break;
    }
}

int
main(int argc, char **argv)
{
    int c, nofork = 0, l = -1;
    int idx = 0;
    char *tmp;
    static int one = 1;
    static struct option opts[] = {
        {"stuntd", 0, 0, 's'},
        {"turnd", 0, 0, 't'},
        {"swaplinksd", 0, 0, 'w'},
        {"registrar", 1, 0, 'r'},
        {"password", 1, 0, 'p'},
        {"nofork", 0, 0, 'n'},
        {"help", 0, 0, 'h'},
        {"logfile", 1, 0, 'l'},
        {0, 0, 0, 0}
    };

    nutss_config_set(NUTSS_CONFIG_OPT_BIND, &one, sizeof(one));
    while (1) {
        c = getopt_long(argc, argv, "hp:r:l:stwn", opts, &idx);

        if (c == -1)
            break;

        switch (c) {
            case 's':
                nutss_config_set(NUTSS_CONFIG_OPT_STUNTD, &one,
                    sizeof(one));
                break;
            case 't':
                nutss_config_set(NUTSS_CONFIG_OPT_TURND, &one, sizeof(one));
                break;
            case 'w':
                nutss_config_set(NUTSS_CONFIG_OPT_SWAPLINKSD, &one,
                    sizeof(one));
                break;
            case 'r':
                nutss_config_set(NUTSS_CONFIG_SIGPROXY, optarg,
                    strlen(optarg) + 1);
                break;
            case 'p':
                nutss_config_set(NUTSS_CONFIG_PROXYPASSWORD, optarg,
                    strlen(optarg) + 1);
                break;
            case 'n':
                nofork = 1;
                break;
            case 'l':
                if (l != -1)
                    close(l);
                l = open(optarg, O_RDWR|O_APPEND|O_CREAT|O_SYNC, S_IRUSR|S_IWUSR);
                break;
            case 'h':
            default:
                usage(argv[0]);
        }
    }

    if (optind >= argc)
        usage(argv[0]);

    tmp = strchr(argv[optind], '@');
    if (!tmp)
        usage(argv[0]);
    tmp[0] = '\0';
    tmp++;
    nutss_config_set(NUTSS_CONFIG_USERNAME, argv[optind],
        strlen(argv[optind]) + 1);
    nutss_config_set(NUTSS_CONFIG_PROXYUSERNAME, argv[optind],
        strlen(argv[optind]) + 1);
    nutss_config_set(NUTSS_CONFIG_DOMAINNAME, tmp, strlen(tmp) + 1);
    optind++;

    if (!nofork && daemon(0,0)) {
        perror("nutssd");
        exit(1);
    }

    if (l != -1) {
        // Connect stdin and stdout to log file
        dup2(l,1);
        dup2(l,2);
        close(l);
    }

    signal(SIGHUP, signal_handler);
    signal(SIGTERM, signal_handler);

    sem_init(&sem, 0, 0);
    nutss_init();
    while (sem_wait(&sem) == -1 && errno == EINTR);

    exit(0);
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>
 

GForge.cis.cornell.edu is brought to you by

Cornell Computing and Information Science


Powered By GForge Collaborative Development Environment