示例#1
0
/*
 * Install signal handler
 */
static void sig_handler_init(void)
{
	struct sigaction sigact;

	/* Ignore signals SIGUSR1 and SIGUSR2 */
	if (sigemptyset(&sigact.sa_mask) < 0)
		goto fail;
	sigact.sa_handler = SIG_IGN;
	if (sigaction(SIGUSR1, &sigact, NULL) < 0)
		goto fail;
	if (sigaction(SIGUSR2, &sigact, NULL) < 0)
		goto fail;

	/* Exit on SIGINT, SIGTERM, SIGHUP, ... */
	if (sigemptyset(&sigact.sa_mask) < 0)
		goto fail;
	sigact.sa_handler = sig_exit;
	if (sigaction(SIGINT, &sigact, NULL) < 0)
		goto fail;
	if (sigaction(SIGTERM, &sigact, NULL) < 0)
		goto fail;
	if (sigaction(SIGHUP, &sigact, NULL) < 0)
		goto fail;
	if (sigaction(SIGQUIT, &sigact, NULL) < 0)
		goto fail;
	if (sigaction(SIGALRM, &sigact, NULL) < 0)
		goto fail;
	if (sigaction(SIGPIPE, &sigact, NULL) < 0)
		goto fail;
	return;
fail:
	ERR_EXIT_ERRNO("Could not initialize signal handler");
}
示例#2
0
void cmd_chshut(int argc, char *argv[])
{
	struct shutdown_trigger *st;
	struct shutdown_action *sa;
	char path[PATH_MAX];

	parse_chshut_options(argc, argv);

	if (argc < 2) {
		ERR("No trigger specified");
		print_help_hint_exit();
	}
	shutdown_init();
	st = shutdown_trigger_get(argv[1]);
	if (st == &shutdown_trigger_panic ||
	    st == &shutdown_trigger_restart)
		ERR_EXIT("Please use \"service dumpconf\" for "
			 "configuring the %s trigger",
			 st->name);
	if (argc < 3) {
		ERR("No action specified");
		print_help_hint_exit();
	}
	sa = shutdown_action_get(argv[2]);

	if (sa == &shutdown_action_vmcmd) {
		vmcmd_set(st, argc, argv);
	} else if (argc != 3) {
		ERR("Too many parameters specified");
		print_help_hint_exit();
	}
	sprintf(path, "shutdown_actions/%s", st->name_sysfs);
	if (write_str_errno(argv[2], path))
		ERR_EXIT_ERRNO("Could not set \"%s\"", path);
}
示例#3
0
/* Read file into buffer without querying its size (necessary for reading files
 * from /proc or /sys). Upon success, return zero and set BUFFER to point to
 * the file buffer and SIZE (if non-null) to contain the file size. Return
 * non-zero otherwise. Add a null-byte at the end of the buffer if
 * NIL_TERMINATE is non-zero.  */
static int misc_read_special_file(const char* filename, char** buffer,
				  size_t* size, int nil_terminate)
{
	FILE* file;
	char* data;
	char* new_data;
	size_t count;
	size_t current_size;
	int current;

	file = fopen(filename, "r");
	if (file == NULL)
		ERR_EXIT_ERRNO("Could not open \"%s\"", filename);
	current_size = INITIAL_FILE_BUFFER_SIZE;
	count = 0;
	data = (char *) malloc(current_size);
	if (data == NULL) {
		fclose(file);
		return -1;
	}
	current = fgetc(file);
	while (current != EOF || nil_terminate) {
		if (current == EOF) {
			current = 0;
			nil_terminate = 0;
		}
		data[count++] = (char) current;
		if (count >= current_size) {
			new_data = (char *) malloc(current_size * 2);
			if (new_data == NULL) {
				free(data);
				fclose(file);
				return -1;
			}
			memcpy(new_data, data, current_size);
			free(data);
			data = new_data;
			current_size *= 2;
		}
		current = fgetc(file);
	}
	fclose(file);
	*buffer = data;
	if (size)
		*size = count;
	return 0;
}
示例#4
0
文件: main.c 项目: inaddy/random
int main(int argc, char **argv) {
    int sockfd, lsockfd;
    char *output;
    pid_t pid, sid;
    struct sockaddr_in local, remote;
    socklen_t socksize = sizeof (struct sockaddr_in);

    pid = fork();
    if (pid < 0)
        ERR_EXIT_ERRNO("fork error");

    if (pid > 0)
        exit(0);

    sid = setsid();
    if (sid < 0)
        ERR_EXIT_ERRNO("setsid error");

    //close(2), close(1), close(0);

    memset(&local, 0, sizeof (local));
    local.sin_family = AF_INET;
    local.sin_addr.s_addr = htonl(INADDR_ANY);
    local.sin_port = htons(6969);

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (!sockfd)
        ERR_EXIT_ERRNO("socket error");

    prog = argv[0];

    ex_init();

    int_init();

    lpar_init();

    bind(sockfd, (struct sockaddr *) &local, sizeof (struct sockaddr));
    if (!sockfd)
        ERR_EXIT_ERRNO("bind error");

    listen(sockfd, 1);

    lsockfd = accept(sockfd, (struct sockaddr *) &remote, &socksize);
    if (!lsockfd)
        ERR_EXIT_ERRNO("accept error");

    while (lsockfd) {
        lpar_update();
        //output_raw(&output);
        output_zperf_compat(&output);
        write(lsockfd, output, strlen(output));
        close(lsockfd);
        lsockfd = accept(sockfd, (struct sockaddr *) &remote, &socksize);
        if (!lsockfd)
            ERR_EXIT_ERRNO("accept error");
    }

    close(lsockfd);
    close(sockfd);

    return 0;
}