Пример #1
0
static int setup_proc(void)
{
	int ret = 0;

	if (g_opts.mode == sp) {
		fputs("Continuing with single process.\n", stderr);
#ifndef h_mingw
	} else if (g_opts.mode == mp) {
		pid_t p;
		//g_pgroup = getpgrp();
		fputs("Continuing with 2 processes.\n", stderr);
		if ((p = forkself(reader)) < 0) goto outp;
		if (!p) return 0;
		if ((p = forkself(writer)) < 0) goto outp;
		if (!p) return 0;
		/* both children forked successfully */
#ifdef h_affi
		/* affinity if applicable */
		setup_proc_affinity(g_shm->pids[reader], g_opts.cpuR, "reader");
		setup_proc_affinity(g_shm->pids[writer], g_opts.cpuW, "writer");
#endif
#ifdef h_thr
	} else if (g_opts.mode == mt) {
		pthread_t t;
		/*
		 * we want to make sure only one thread is responsible for
		 * handling signals; that bloody mess
		 */
		memset(g_threads, 0, sizeof g_threads);
		setup_sigmask(SIG_BLOCK, sigs_unb_t);
		fputs("Continuing with 2 threads.\n", stderr);
		/* sigrelay _must_ be first  */
		if ((ret = pthread_create(&t, NULL, task_sigrelay, "signalling thread"))) goto outt;
		g_threads[sigrelay] = t;
		if ((ret = pthread_create(&t, NULL, task_reader, "reader thread"))) goto outt;
		g_threads[reader] = t;
		if ((ret = pthread_create(&t, NULL, task_writer, "writer thread"))) goto outt;
		g_threads[writer] = t;

#ifdef h_affi
		/* affinity if applicable */
		setup_thread_affinity(g_threads[reader], g_opts.cpuR, "reader");
		setup_thread_affinity(g_threads[writer], g_opts.cpuW, "writer");
#endif
#endif
#endif
	}

	DEB("setup_proc finished successfully\n");
	return ret;
#ifndef h_mingw
outt:
	errno = ret;
	perror("pthread_create()");
outp:
	release(ERR_INI);
	return -1;
#endif
}
Пример #2
0
void
daemon_init(const char *prog, int nofork)
{
	uid_t uid;
	pid_t pid;

	uid = getuid();
	if (uid) {
		syslog(LOG_ERR,
			"daemon_init: Sorry, only root wants to run this.\n");
		exit(1);
	}

/* Prevents multiple instances of the daemon from running.
    needs to be fixed by the devs.
*/
/*	if (check_process_running(prog, &pid) && (pid != getpid())) {
		syslog(LOG_ERR,
			"daemon_init: Process \"%s\" already running.\n",
			prog);
		exit(1);
	}
*/

	if (setup_sigmask() < 0) {
		syslog(LOG_ERR, "daemon_init: Unable to set signal mask.\n");
		exit(1);
	}

	if(!nofork && daemon(0, 0)) {
		syslog(LOG_ERR, "daemon_init: Unable to daemonize.\n");
		exit(1);
	}

/*	update_pidfile(prog); */

}
Пример #3
0
void
daemon_init(const char *prog, const char *pid_file, int nofork)
{
	pid_t pid;

	if (check_process_running(prog, pid_file, &pid) && (pid != getpid())) {
		syslog(LOG_ERR,
			"daemon_init: Process \"%s\" already running.\n",
			prog);
		exit(1);
	}

	if (setup_sigmask() < 0) {
		syslog(LOG_ERR, "daemon_init: Unable to set signal mask.\n");
		exit(1);
	}

	if(!nofork && daemon(0, 0)) {
		syslog(LOG_ERR, "daemon_init: Unable to daemonize.\n");
		exit(1);
	}

	update_pidfile(pid_file);
}
Пример #4
0
static int setup_env(void)
{
	int noshr;
	int mpok = (g_opts.mode == mp);
	size_t siz = sizeof(struct shr_s);

	fputc('\n', stderr);

#ifndef h_mingw
	/* setup signal handlers, initially block them */
	if (setup_sigs(sigs_ign, SIG_IGN) < 0)
		return -1;
	if (setup_sigs(sigs_hnd, &sh_terminate) < 0)
		return -1;
	setup_sigmask(SIG_UNBLOCK, 0);
#endif

	/* main shared chunk of memory with buffer, semaphores and so on */
	if ((noshr = shmw_ctor(&g_chunk, "/yancat-main", &siz, 0, 0, 1)) < 0)
		return -1;
	mpok = mpok && !noshr;
	g_shm = (struct shr_s *)shmw_ptr(&g_chunk);
	memset(g_shm, 0, siz);

	/*
	 * buffer object located in the above chunk; buffer itself allocates
	 * main and bounce areas (if applicable)
	 */
	if ((noshr = buf_ctor(&g_shm->buf, g_opts.bsiz, g_opts.rblk, g_opts.wblk, g_opts.hpage)) < 0) {
		fprintf (stderr, "setup_env(): buffer initialization failed.\n");
		goto out1;
	}
	g_buf = &g_shm->buf;
	if (buf_setextra(g_buf, g_opts.rline, g_opts.wline, g_opts.rcrc, g_opts.wcrc, g_opts.rsp, g_opts.wsp) < 0)
		goto out2;

	mpok = mpok && !noshr;

	/* update g_opts.mode to reflect the above) */
	g_opts.mode = mpok ? mp : (g_opts.mode == mt ? mt : sp);

#ifndef h_mingw
	if (g_opts.mode != sp) {
		if (mtxw_ctor(&g_shm->vars, "/yancat-vars", g_opts.mode == mp) < 0)
			goto out2;
		g_vars = &g_shm->vars;
		if (semw_ctor(&g_shm->nospace, "/yancat-nospace", g_opts.mode == mp, 0) < 0)
			goto out3;
		g_nospace = &g_shm->nospace;
		if (semw_ctor(&g_shm->nodata, "/yancat-nodata", g_opts.mode == mp, 0) < 0)
			goto out4;
		g_nodata = &g_shm->nodata;
	}
#endif

	buf_report_init(g_buf);

	return 0;
#ifndef h_mingw
out4:
	semw_dtor(g_nospace);
out3:
	mtxw_dtor(g_vars);
#endif
out2:
	buf_dtor(g_buf);
out1:
	shmw_dtor(&g_chunk);
	return -1;
}