void get_self_executable_directory_test()
{
    puts(get_self_executable_directory());
}
Beispiel #2
0
Datei: main.c Projekt: sktwj/var
int main (int argc, char* const argv[])
{
	struct in_addr local_address;
	uint16_t port;
	int next_option;
	/* Store the program name, which we'll use in error messages.*/
	program_name = argv[0];
	/* Set defaults for options. Bind the server to all local addresses,
	   and assign an unused port automatically. */
	local_address.s_addr = INADDR_ANY;
	port = htons(8080);//default port 8080

	/* Don't print verbose messages. */
	verbose = 0;
	/* Load modules from the directory containing this executable. */
	module_dir = get_self_executable_directory ();
	assert (module_dir != NULL);
	/* Parse options. */
	do {
		next_option =
			getopt_long (argc, argv, short_options, long_options, NULL);
		switch (next_option) {
			case 'a':
				/* User specified -a or --address. */
				{
					struct hostent* local_host_name;
					/* Look up the hostname the user specified. */
					local_host_name = gethostbyname (optarg);
					if (local_host_name == NULL || local_host_name->h_length == 0)
						/* Could not resolve the name. */
						error (optarg, "invalid host name");
					else
						/* Hostname is OK, so use it. */
						local_address.s_addr =
							*((int*) (local_host_name->h_addr_list[0]));
				}
				break;
			case 'h':
				/* User specified -h or --help.*/
				   print_usage (0);
				   break;
			case 'm':
				/* User specified -m or --module-dir.*/
				{
				   struct stat dir_info;
				/* Check that it exists. */
				if (access (optarg, F_OK) != 0)
					error (optarg, "module directory does not exist");
				/* Check that it is accessible. */
				if (access (optarg, R_OK | X_OK) != 0)
					error (optarg, "module directory is not accessible");
				/* Make sure that it is a directory. */
				if (stat (optarg, &dir_info) != 0 || !S_ISDIR (dir_info.st_mode))
					error (optarg, "not a directory");
				/* It looks OK, so use it. */
				module_dir = strdup (optarg);
				}
				break;
			case 'p':
				/* User specified -p or --port. */
				{
					long value;
					char* end;

					value = strtol (optarg, &end, 10);
					if (*end != '\0')
						/* The user specified nondigits in the port number. */
						print_usage (1);
					/* The port number needs to be converted to network (big endian)
					   byte order. */
					port = (uint16_t) htons (value);
				}
				break;
			case 'v':
				/* User specified -v or --verbose.*/
				verbose = 1;
				break;
			case '?':
		/* User specified an unrecognized option.*/
				print_usage (1);
			case -1:
		/* Done with options. */
				break;
			default:
				abort ();
		}
	} while (next_option != -1);
	/* This program takes no additional arguments.
	   user specified any. */
	if (optind != argc)
		print_usage (1);
	/*Issue an error if the Print the module directory, if we're running verbose. */
		if (verbose)
			printf ("modules will be loaded from %s\n", module_dir);
	/* Run the server. */
	server_run (local_address, port);
	return 0;
}
Beispiel #3
0
int main (int argc, char* const argv[])
{
    struct in_addr local_address;
    uint16_t port;
    int next_option;

    /* Store the program name, which we'll use in error messages.  */
    program_name = argv[0];

    char str_tmp[128];
    /* create run dir */
    if(mkdir(RUN_DIR, 0755)){
        /* sprintf(str_tmp, "create %s", RUN_DIR); */
        /* system_error(str_tmp); */
    }
    /* create pid file to prevent run another instance */
    sprintf(str_tmp, "%s/server.pid", RUN_DIR);
    pid_fd = open(str_tmp, O_CREAT|O_WRONLY, 0644);
    if (pid_fd <= 0) {
        system_error("create pid file");
    }
    memset(&lock, 0, sizeof(struct flock));
    lock.l_type = F_WRLCK;
    /* Place a write lock on the file.  */
    if(-1 == fcntl(pid_fd, F_SETLK, &lock)){
        fprintf(stdout, "The program %s is already running.\n", program_name);
        exit(1);
    }

    /* install a signal handler for Ctrl-C */
    struct sigaction signal_kill;
    memset(&signal_kill, 0, sizeof(struct sigaction));
    signal_kill.sa_handler = &signal_kill_handler;
    sigaction(SIGINT, &signal_kill, NULL);

    sprintf(str_tmp, "%d", getpid());
    write(pid_fd, str_tmp, strlen(str_tmp));

    /* Set defaults for options.  Bind the server to all local addresses,
       and assign an unused port automatically.  */
    local_address.s_addr = INADDR_ANY;
    port = htons(1234);
    /* Don't print verbose messages.  */
    verbose = 0;
    /* Load modules from the directory containing this executable.  */
    module_dir = get_self_executable_directory ();
    assert (module_dir != NULL);

    /* Parse options.  */
    do {
        next_option = 
            getopt_long (argc, argv, short_options, long_options, NULL);
        switch (next_option) {
            case 'a':  
                /* User specified -a or --address.  */
                {
                    struct hostent* local_host_name;

                    /* Look up the host name the user specified.  */
                    local_host_name = gethostbyname (optarg);
                    if (local_host_name == NULL || local_host_name->h_length == 0)
                        /* Could not resolve the name.  */
                        error (optarg, "invalid host name");
                    else
                        /* Host name is OK, so use it.  */
                        local_address.s_addr = 
                            *((int*) (local_host_name->h_addr_list[0]));
                }
                break;      

            case 'h':  
                /* User specified -h or --help.  */
                print_usage (0);

            case 'm':
                /* User specified -m or --module-dir.  */
                {
                    struct stat dir_info;

                    /* Check that it exists.  */
                    if (access (optarg, F_OK) != 0)
                        error (optarg, "module directory does not exist");
                    /* Check that it is accessible.  */
                    if (access (optarg, R_OK | X_OK) != 0)
                        error (optarg, "module directory is not accessible");
                    /* Make sure that it is a directory.  */
                    if (stat (optarg, &dir_info) != 0 || !S_ISDIR (dir_info.st_mode))
                        error (optarg, "not a directory");
                    /* It looks OK, so use it.  */
                    module_dir = strdup (optarg);
                }
                break;

            case 'p':  
                /* User specified -p or --port.  */
                {
                    long value;
                    char* end;

                    value = strtol (optarg, &end, 10);
                    if (*end != '\0')
                        /* The user specified non-digits in the port number.  */
                        print_usage (1);
                    /* The port number needs to be converted to network (big endian)
                       byte order.  */
                    port = (uint16_t) htons (value);
                }
                break;

            case 'v':  
                /* User specified -v or --verbose.  */
                verbose = 1;
                break;

            case '?':  
                /* User specified an nrecognized option.  */
                print_usage (1);

            case -1:  
                /* Done with options.  */
                break;

            default:
                abort ();
        }
    } while (next_option != -1);

    /* This program takes no additional arguments.  Issue an error if the
       user specified any.  */
    if (optind != argc){
        print_usage (1);
    }

    /* Print the module directory, if we're running verbose.  */
    if (verbose)
        printf ("modules will be loaded from %s\n", module_dir);

    xlog_record(LOG_INFO, "Start server.");

    /* Run the server.  */
    server_run (local_address, port);
    
    return 0;
}