本文整理汇总了C++中write_pidfile函数的典型用法代码示例。如果您正苦于以下问题:C++ write_pidfile函数的具体用法?C++ write_pidfile怎么用?C++ write_pidfile使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了write_pidfile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ev_irc_connect
/** internal functions implementation starts here **/
int ev_irc_connect(void* dummy1, void* dummy2)
{
int cr;
stdlog(L_INFO, "Connecting to IRC server %s:%i", RemoteServer, RemotePort);
cr = irc_FullConnect(RemoteServer, RemotePort, ServerPass, 0);
if(cr < 0)
{
errlog("Could not connect to IRC server: %s", irc_GetLastMsg());
exit(1);
}
stdlog(L_INFO, "Netjoin complete, %.1d Kbs received", irc_InByteCount()/1024);
/* not sure if this fork should be on the irc module
for now lets leave it like this to make sure we only fork after the
connection is fully established
*/
if( nofork == 0 )
{
fork_process();
write_pidfile();
}
irc_LoopWhileConnected();
errlog("Disconnected:%s\n", irc_GetLastMsg());
/* stdlog(L_INFO, "PTlink IRC Services Terminated"); */
return 0;
}
示例2: start
void
start(const char *cmd, monitor_t *monitor) {
exec: {
pid = fork();
int status;
switch (pid) {
case -1:
perror("fork()");
exit(1);
case 0:
log("sh -c \"%s\"", cmd);
execl("/bin/bash", "bash", "-c", cmd, 0);
perror("execl()");
exit(1);
default:
log("pid %d", pid);
// write pidfile
if (monitor->pidfile) {
log("write pid to %s", monitor->pidfile);
write_pidfile(monitor->pidfile, pid);
}
// wait for exit
waitpid(pid, &status, 0);
// signalled
if (WIFSIGNALED(status)) {
log("signal(%s)", strsignal(WTERMSIG(status)));
log("sleep(%d)", monitor->sleepsec);
sleep(monitor->sleepsec);
goto error;
}
// check status
if (WEXITSTATUS(status)) {
log("exit(%d)", WEXITSTATUS(status));
log("sleep(%d)", monitor->sleepsec);
sleep(monitor->sleepsec);
goto error;
}
// alerts
error: {
if (monitor->on_error) {
log("on error \"%s\"", monitor->on_error);
int status = system(monitor->on_error);
if (status) {
log("exit(%d)", status);
log("shutting down");
exit(status);
}
}
goto exec;
}
}
}
}
示例3: daemonize
int daemonize()
{
pid_t pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS);
}
if (write_pidfile() != 0) {
return -1;
}
int ret = setsid();
if (ret == -1) {
XLOG_ERR("failed to set session id: %s", strerror(errno));
unlink(PIDFILE);
return -1;
}
for (int fd = getdtablesize(); fd >= 0; --fd) {
close(fd);
}
chdir("/");
int fd = open("/dev/null", O_RDWR);
dup(fd);
dup(fd);
return 0;
}
示例4: client_background
static void client_background(void)
{
bb_daemonize(0);
logmode &= ~LOGMODE_STDIO;
/* rewrite pidfile, as our pid is different now */
write_pidfile(client_config.pidfile);
}
示例5: daemonize
void daemonize(const char *pidfile)
{
pid_t pid, sid;
/* Return if already a daemon */
if (getppid() == 1)
return;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
PERROR("fork", errno);
exit(EXIT_FAILURE);
}
/* If we got a good PID, then we can exit the parent process. */
if (pid > 0) {
exit(EXIT_SUCCESS);
}
/* At this point we are executing as the child process */
write_pidfile(pidfile);
/* Change the file mode mask */
umask(0);
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
PERROR("setsid", errno);
exit(EXIT_FAILURE);
}
/* Change the current working directory. This prevents the current
directory from being locked; hence not being able to remove it. */
if ((chdir("/")) < 0) {
PERROR("chdir", errno);
exit(EXIT_FAILURE);
}
/* Redirect standard files to /dev/null */
if (freopen( "/dev/null", "r", stdin) == NULL) {
PERROR("freopen: stdin", errno);
}
if (freopen( "/dev/null", "w", stdout) == NULL) {
PERROR("freopen: stdout", errno);
}
if (freopen( "/dev/null", "w", stderr) == NULL) {
PERROR("freopen: stderr", errno);
}
}
示例6: main
int main(int argc, char *argv[])
{
char *password;
int port = DEFAULT_LISTEN_PORT;
parameters_t pars;
struct sched_param sched_par;
openlog("tcpconsole", LOG_CONS|LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_DAEMON);
if (getuid())
error_exit("This program must be invoked with root-rights.");
password = read_password("/etc/tcpconsole.pw");
if (signal(SIGTERM, SIG_IGN) == SIG_ERR)
error_exit("signal(SIGTERM) failed");
if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
error_exit("signal(SIGHUP) failed");
pars.sysrq_fd = open_file("/proc/sysrq-trigger", O_WRONLY);
pars.vcsa0_fd = open_file("/dev/vcsa", O_RDONLY);
if (setpriority(PRIO_PROCESS, 0, -10) == -1)
error_exit("Setpriority failed");
if (nice(-20) == -1)
error_exit("Failed to set nice-value to -20");
if (mlockall(MCL_CURRENT) == -1 || mlockall(MCL_FUTURE) == -1)
error_exit("Failed to lock program in core");
memset(&sched_par, 0x00, sizeof(sched_par));
sched_par.sched_priority = sched_get_priority_max(SCHED_RR);
if (sched_setscheduler(0, SCHED_RR, &sched_par) == -1)
error_exit("Failed to set scheduler properties for this process");
syslog(LOG_INFO, "tcpconsole started");
write_pidfile("/var/run/tcpconsole.pid");
if ((pars.dmesg_buffer_size = klogctl(10, NULL, 0)) == -1)
error_exit("klogctl(10) failed");
pars.dmesg_buffer = (char *)malloc(pars.dmesg_buffer_size + 1);
if (!pars.dmesg_buffer)
error_exit("malloc failure");
listen_on_socket(port, &pars, password);
return 1;
}
示例7: main
int
main(int argc, char **argv) {
struct Config *config = NULL;
const char *config_file = "/etc/sniproxy.conf";
int background_flag = 1;
pid_t pid;
int opt;
while ((opt = getopt(argc, argv, "fc:")) != -1) {
switch (opt) {
case 'c':
config_file = optarg;
break;
case 'f': /* foreground */
background_flag = 0;
break;
default:
usage();
exit(EXIT_FAILURE);
}
}
config = init_config(config_file);
if (config == NULL) {
fprintf(stderr, "Unable to load %s\n", config_file);
return 1;
}
init_server(config);
if (background_flag) {
pid = daemonize(config->user ? config->user : DEFAULT_USERNAME);
if (pid != 0 && config->pidfile != NULL) {
/* parent */
write_pidfile(config->pidfile, pid);
free_config(config);
return 0;
}
}
run_server();
free_config(config);
return 0;
}
示例8: become_daemon
/* Become a SysV daemon */
int become_daemon(void)
{
int fd;
long maxfd, i;
switch (fork()) {
case -1: return -1; /* error */
case 0: break; /* child process created */
default: exit(EXIT_FAILURE); /* parent receives child's PID */
}
/* Detach from any terminal and create an independent session. */
if (setsid() == -1) return -1;
/* Ensure that the daemon can never re-acquire a terminal again. */
switch (fork()) {
case -1: return -1;
case 0: break;
default: exit(EXIT_FAILURE);
}
/* Reset file mode creation mask. */
umask(0);
/* Change current directory to root directory (/) */
if (chdir("/") == -1) return -1;
/* Find maximum open file descriptors */
maxfd = sysconf(_SC_OPEN_MAX);
if (maxfd == -1) maxfd = MAX_OPEN;
/* Close all open file descriptors */
for (i = 0; i < maxfd; i++) close(i);
/* Connect /dev/null to stdin, stdout, stderr */
close(STDIN_FILENO);
fd = open("/dev/null", O_RDWR);
if (fd != STDIN_FILENO) return -1;
if (dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO) return -1;
if (dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO) return -1;
/* Write PID file in /var/run/ */
if (write_pidfile() == -1) return -1;
return 0;
}
示例9: main
int
main(int argc, char **argv){
monitor_t monitor;
monitor.pidfile = NULL;
monitor.mon_pidfile = NULL;
monitor.on_error = NULL;
monitor.logfile = "mon.log";
monitor.daemon = 0;
monitor.sleepsec = 1;
command_t program;
program.data = &monitor;
command_init(&program, "mon", VERSION);
command_option(&program, "-l", "--log <path>", "specify logfile [mon.log]", on_log);
command_option(&program, "-s", "--sleep <sec>", "sleep seconds before re-executing [1]", on_sleep);
command_option(&program, "-S", "--status", "check status of --pidfile", on_status);
command_option(&program, "-p", "--pidfile <path>", "write pid to <path>", on_pidfile);
command_option(&program, "-m", "--mon-pidfile <path>", "write mon(1) pid to <path>", on_mon_pidfile);
command_option(&program, "-P", "--prefix <str>", "add a log prefix", on_prefix);
command_option(&program, "-d", "--daemonize", "daemonize the program", on_daemonize);
command_option(&program, "-e", "--on-error <cmd>", "execute <cmd> on errors", on_error);
command_parse(&program, argc, argv);
// command required
if (!program.argc) error("<cmd> required");
const char *cmd = program.argv[0];
// signals
signal(SIGTERM, graceful_exit);
signal(SIGQUIT, graceful_exit);
// daemonize
if (monitor.daemon) {
daemonize();
redirect_stdio_to(monitor.logfile);
}
// write mon pidfile
if (monitor.mon_pidfile) {
log("write mon pid to %s", monitor.mon_pidfile);
write_pidfile(monitor.mon_pidfile, getpid());
}
start(cmd, &monitor);
return 0;
}
示例10: main
int
main(void)
{
const pid_t pid = getpid();
write_pidfile(pid);
wait_for_peer_invocation();
static const char dir[] = "attach-p-cmd.test cmd";
int rc = chdir(dir);
printf("%-5d chdir(\"%s\") = %s\n"
"%-5d +++ exited with 0 +++\n",
pid, dir, sprintrc(rc), pid);
return 0;
}
示例11: do_work
/// <summary>Our raspicomm daemon specific work gets done.</summary>
void do_work(void)
{
/* setup the syslog for logging */
init_syslog();
/* read the settings from the settings file */
Settings settings; int ret;
if ((ret = load_settings(&settings)) < 0)
{
//syslog(LOG_ERR, "loading settings failed (%i)", ret);
exit(EXIT_FAILURE);
}
if (settings.log)
{
syslog(LOG_INFO, "starting...");
syslog(LOG_INFO, "using %s: port '%i' and pidfile '%s' and log=%i",
ret == 0 ? "default configuration" : "configurationfile",
settings.port,
settings.pidfile,
settings.log);
}
/* write the pidfile */
if ( write_pidfile(settings.pidfile) != 0)
if (settings.log)
syslog(LOG_ERR, "writing PIDFILE '%s' failed", settings.pidfile);
/* use the settings to initialize the raspicomm */
if (daemon_init_raspicomm(&settings) != SUCCESS)
exit(EXIT_FAILURE);
/* init the pipe */
if (init_pipe() != 0)
if (settings.log)
syslog(LOG_ERR, "init_pipe failed");
/* block and work */
enter_main_loop(&settings);
/* tear down raspicomm */
daemon_shutdown();
}
示例12: detach
/*
Detach from current terminal, write pidfile, kill parent
*/
bool detach(void) {
setup_signals();
/* First check if we can open a fresh new pidfile */
#ifndef HAVE_MINGW
if(!write_pidfile())
return false;
/* If we succeeded in doing that, detach */
closelogger();
#endif
if(do_detach) {
#ifndef HAVE_MINGW
if(daemon(0, 0)) {
fprintf(stderr, "Couldn't detach from terminal: %s",
strerror(errno));
return false;
}
/* Now UPDATE the pid in the pidfile, because we changed it... */
if(!write_pid(pidfilename)) {
fprintf(stderr, "Could not write pid file %s: %s\n", pidfilename, strerror(errno));
return false;
}
#else
if(!statushandle)
exit(install_service());
#endif
}
openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR));
logger(LOG_NOTICE, "tincd %s (%s %s) starting, debug level %d",
VERSION, __DATE__, __TIME__, debug_level);
xalloc_fail_func = memory_full;
return true;
}
示例13: client_background
static void client_background(void)
{
#ifdef __uClinux__
bb_error_msg("cannot background in uclinux (yet)");
/* ... mainly because udhcpc calls client_background()
* in _the _middle _of _udhcpc _run_, not at the start!
* If that will be properly disabled for NOMMU, client_background()
* will work on NOMMU too */
#else
// chdir(/) is problematic. Imagine that e.g. pidfile name is RELATIVE! what will unlink do then, eh?
bb_daemonize(DAEMON_CHDIR_ROOT);
/* rewrite pidfile, as our pid is different now */
if (client_config.pidfile)
write_pidfile(client_config.pidfile);
logmode &= ~LOGMODE_STDIO;
#endif
client_config.foreground = 1; /* Do not fork again. */
client_config.background_if_no_lease = 0;
}
示例14: daemonize
bool daemonize(const char* pid_path)
{
/* Double fork. */
pid_t ret;
ret = fork();
if (ret == -1) {
err(EXIT_FAILURE, "fork()");
} else if (ret > 0) {
waitpid(ret, NULL, 0);
return false;
}
ret = fork();
if (ret == -1) {
err(EXIT_FAILURE, "fork()");
} else if (ret > 0) {
exit(EXIT_SUCCESS);
}
/* Write PID. */
if (!write_pidfile(pid_path))
err(EXIT_FAILURE, "%s", pid_path);
/* Change directory. */
if (chdir("/") == -1)
warn("%s", "/");
/* Create new session ID. */
if (setsid() == -1)
warn("setsid()");
/* Close standard streams. */
if (int fd = open("/dev/null", O_RDWR) > 0)
for (int i = 0; i < 3; ++i)
dup2(fd, i);
return true;
}
示例15: main
//.........这里部分代码省略.........
if (daemon_mode) {
#ifndef __APPLE__
daemon (1, 0);
#else /* __APPLE */
/* daemon is deprecated under APPLE
* use fork() instead
* */
switch (fork ()) {
case -1:
seaf_warning ("Failed to daemonize");
exit (-1);
break;
case 0:
/* all good*/
break;
default:
/* kill origin process */
exit (0);
}
#endif /* __APPLE */
}
#endif /* !WIN32 */
cdc_init ();
#if !GLIB_CHECK_VERSION(2, 35, 0)
g_type_init();
#endif
#if !GLIB_CHECK_VERSION(2,32,0)
g_thread_init (NULL);
#endif
if (!debug_str)
debug_str = g_getenv("SEAFILE_DEBUG");
seafile_debug_set_flags_string (debug_str);
if (seafile_dir == NULL)
seafile_dir = g_build_filename (config_dir, "seafile", NULL);
if (logfile == NULL)
logfile = g_build_filename (seafile_dir, "seafile.log", NULL);
if (seafile_log_init (logfile, ccnet_debug_level_str,
seafile_debug_level_str) < 0) {
seaf_warning ("Failed to init log.\n");
exit (1);
}
client = ccnet_init (central_config_dir, config_dir);
if (!client)
exit (1);
register_processors (client);
start_rpc_service (client, cloud_mode);
create_sync_rpc_clients (central_config_dir, config_dir);
create_async_rpc_clients (client);
seaf = seafile_session_new (central_config_dir, seafile_dir, client);
if (!seaf) {
seaf_warning ("Failed to create seafile session.\n");
exit (1);
}
seaf->is_master = is_master;
seaf->ccnetrpc_client = ccnetrpc_client;
seaf->async_ccnetrpc_client = async_ccnetrpc_client;
seaf->ccnetrpc_client_t = ccnetrpc_client_t;
seaf->async_ccnetrpc_client_t = async_ccnetrpc_client_t;
seaf->client_pool = ccnet_client_pool_new (central_config_dir, config_dir);
seaf->cloud_mode = cloud_mode;
load_history_config ();
g_free (seafile_dir);
g_free (logfile);
set_signal_handlers (seaf);
/* init seaf */
if (seafile_session_init (seaf) < 0)
exit (1);
if (seafile_session_start (seaf) < 0)
exit (1);
if (pidfile) {
if (write_pidfile (pidfile) < 0) {
ccnet_message ("Failed to write pidfile\n");
return -1;
}
}
atexit (on_seaf_server_exit);
/* Create a system default repo to contain the tutorial file. */
schedule_create_system_default_repo (seaf);
ccnet_main (client);
return 0;
}