diff options
Diffstat (limited to 'src/dmon.c')
-rw-r--r-- | src/dmon.c | 184 |
1 files changed, 84 insertions, 100 deletions
@@ -1,7 +1,7 @@ /* * Calcurse - text-based organizer * - * Copyright (c) 2004-2011 calcurse Development Team <misc@calcurse.org> + * Copyright (c) 2004-2012 calcurse Development Team <misc@calcurse.org> * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -49,7 +49,7 @@ #define DMON_LOG(...) do { \ if (dmon.log) \ - (void)io_fprintln (path_dmon_log, __VA_ARGS__); \ + io_fprintln (path_dmon_log, __VA_ARGS__); \ } while (0) #define DMON_ABRT(...) do { \ @@ -64,26 +64,22 @@ static unsigned data_loaded; -static void -dmon_sigs_hdlr (int sig) +static void dmon_sigs_hdlr(int sig) { if (data_loaded) - free_user_data (); + free_user_data(); - DMON_LOG (_("terminated at %s with signal %d\n"), nowstr (), sig); + DMON_LOG(_("terminated at %s with signal %d\n"), nowstr(), sig); - if (unlink (path_dpid) != 0) - { - DMON_LOG (_("Could not remove daemon lock file: %s\n"), - strerror (errno)); - exit (EXIT_FAILURE); - } + if (unlink(path_dpid) != 0) { + DMON_LOG(_("Could not remove daemon lock file: %s\n"), strerror(errno)); + exit(EXIT_FAILURE); + } - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); } -static unsigned -daemonize (int status) +static unsigned daemonize(int status) { int fd; @@ -93,16 +89,15 @@ daemonize (int status) * First need to fork in order to become a child of the init process, * once the father exits. */ - switch (fork ()) - { - case -1: /* fork error */ - EXIT (_("Could not fork: %s\n"), strerror (errno)); - break; - case 0: /* child */ - break; - default: /* parent */ - exit (status); - } + switch (fork()) { + case -1: /* fork error */ + EXIT(_("Could not fork: %s\n"), strerror(errno)); + break; + case 0: /* child */ + break; + default: /* parent */ + exit(status); + } /* * Process independency. @@ -110,110 +105,99 @@ daemonize (int status) * Obtain a new process group and session in order to get detached from the * controlling terminal. */ - if (setsid () == -1) - { - DMON_LOG (_("Could not detach from the controlling terminal: %s\n"), - strerror (errno)); - return 0; - } + if (setsid() == -1) { + DMON_LOG(_("Could not detach from the controlling terminal: %s\n"), + strerror(errno)); + return 0; + } /* * Change working directory to root directory, * to prevent filesystem unmounts. */ - if (chdir ("/") == -1) - { - DMON_LOG (_("Could not change working directory: %s\n"), - strerror (errno)); - return 0; - } + if (chdir("/") == -1) { + DMON_LOG(_("Could not change working directory: %s\n"), strerror(errno)); + return 0; + } /* Redirect standard file descriptors to /dev/null. */ - if ((fd = open (_PATH_DEVNULL, O_RDWR, 0)) != -1) - { - (void)dup2 (fd, STDIN_FILENO); - (void)dup2 (fd, STDOUT_FILENO); - (void)dup2 (fd, STDERR_FILENO); - if (fd > 2) - (void)close (fd); - } + if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { + dup2(fd, STDIN_FILENO); + dup2(fd, STDOUT_FILENO); + dup2(fd, STDERR_FILENO); + if (fd > 2) + close(fd); + } /* Write access for the owner only. */ - (void)umask (0022); + umask(0022); - if (!sigs_set_hdlr (SIGINT, dmon_sigs_hdlr) - || !sigs_set_hdlr (SIGTERM, dmon_sigs_hdlr) - || !sigs_set_hdlr (SIGALRM, dmon_sigs_hdlr) - || !sigs_set_hdlr (SIGQUIT, dmon_sigs_hdlr) - || !sigs_set_hdlr (SIGCHLD, SIG_IGN)) + if (!sigs_set_hdlr(SIGINT, dmon_sigs_hdlr) + || !sigs_set_hdlr(SIGTERM, dmon_sigs_hdlr) + || !sigs_set_hdlr(SIGALRM, dmon_sigs_hdlr) + || !sigs_set_hdlr(SIGQUIT, dmon_sigs_hdlr) + || !sigs_set_hdlr(SIGCHLD, SIG_IGN)) return 0; return 1; } -void -dmon_start (int parent_exit_status) +void dmon_start(int parent_exit_status) { - struct conf conf; - - if (!daemonize (parent_exit_status)) - DMON_ABRT (_("Cannot daemonize, aborting\n")); - - if (!io_dump_pid (path_dpid)) - DMON_ABRT (_("Could not set lock file\n")); - - if (!io_file_exist (path_conf)) - DMON_ABRT (_("Could not access \"%s\": %s\n"), - path_conf, strerror (errno)); - custom_load_conf (&conf, 0); - - if (!io_file_exist (path_apts)) - DMON_ABRT (_("Could not access \"%s\": %s\n"), - path_apts, strerror (errno)); - apoint_llist_init (); - recur_apoint_llist_init (); - event_llist_init (); - todo_init_list (); - io_load_app (); + if (!daemonize(parent_exit_status)) + DMON_ABRT(_("Cannot daemonize, aborting\n")); + + if (!io_dump_pid(path_dpid)) + DMON_ABRT(_("Could not set lock file\n")); + + if (!io_file_exist(path_conf)) + DMON_ABRT(_("Could not access \"%s\": %s\n"), path_conf, strerror(errno)); + config_load(); + + if (!io_file_exist(path_apts)) + DMON_ABRT(_("Could not access \"%s\": %s\n"), path_apts, strerror(errno)); + apoint_llist_init(); + recur_apoint_llist_init(); + event_llist_init(); + todo_init_list(); + io_load_app(); data_loaded = 1; - DMON_LOG (_("started at %s\n"), nowstr ()); - for (;;) - { - int left; - - if (!notify_get_next_bkgd ()) - DMON_ABRT (_("error loading next appointment\n")); - - left = notify_time_left (); - if (left > 0 && left < nbar.cntdwn && notify_needs_reminder ()) - { - DMON_LOG (_("launching notification at %s for: \"%s\"\n"), - nowstr (), notify_app_txt ()); - if (!notify_launch_cmd ()) - DMON_LOG (_("error while sending notification\n")); - } - - DMON_LOG (_("sleeping at %s for %d seconds\n"), nowstr (), - DMON_SLEEP_TIME); - psleep (DMON_SLEEP_TIME); - DMON_LOG (_("awakened at %s\n"), nowstr ()); + DMON_LOG(_("started at %s\n"), nowstr()); + for (;;) { + int left; + + if (!notify_get_next_bkgd()) + DMON_ABRT(_("error loading next appointment\n")); + + left = notify_time_left(); + if (left > 0 && left < nbar.cntdwn && notify_needs_reminder()) { + DMON_LOG(_("launching notification at %s for: \"%s\"\n"), + nowstr(), notify_app_txt()); + if (!notify_launch_cmd()) + DMON_LOG(_("error while sending notification\n")); } + + DMON_LOG(ngettext("sleeping at %s for %d second\n", + "sleeping at %s for %d seconds\n", + DMON_SLEEP_TIME), nowstr(), DMON_SLEEP_TIME); + psleep(DMON_SLEEP_TIME); + DMON_LOG(_("awakened at %s\n"), nowstr()); + } } /* * Check if calcurse is running in background, and if yes, send a SIGINT * signal to stop it. */ -void -dmon_stop (void) +void dmon_stop(void) { int dpid; - dpid = io_get_pid (path_dpid); + dpid = io_get_pid(path_dpid); if (!dpid) return; - if (kill ((pid_t)dpid, SIGINT) < 0 && errno != ESRCH) - EXIT (_("Could not stop calcurse daemon: %s\n"), strerror (errno)); + if (kill((pid_t) dpid, SIGINT) < 0 && errno != ESRCH) + EXIT(_("Could not stop calcurse daemon: %s\n"), strerror(errno)); } |