diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2021-10-22 09:54:32 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2021-10-22 09:54:32 +0300 |
commit | d6b71c7f55cb552cc2564e7af01293aa26189bde (patch) | |
tree | e8a775b9f83557738373ac0a5fae1ea1b16b06b7 | |
parent | 24c93eea9aa605addb11d4575a1baf825c36ab75 (diff) | |
download | micron-outfile.tar.gz micron-outfile.tar.bz2 |
Simplify output file managementoutfile
* src/micrond.c (fatal_signals): Restore SIGHUP.
(main): Remove cron_thr_outfile_mgr.
Upon SIGHUP call outfiles_close, terminate on other signals.
* src/micrond.h (cron_thr_outfile_mgr): Remove.
(outfiles_close): New proto.
* src/runner.c (cron_thr_outfile_mgr): Remove.
(outfiles_close): New function.
-rw-r--r-- | src/micrond.c | 15 | ||||
-rw-r--r-- | src/micrond.h | 2 | ||||
-rw-r--r-- | src/runner.c | 54 |
3 files changed, 30 insertions, 41 deletions
diff --git a/src/micrond.c b/src/micrond.c index 7b618e9..9a56e35 100644 --- a/src/micrond.c +++ b/src/micrond.c @@ -165,6 +165,7 @@ stderr_log(int prio, char const *fmt, ...) void (*micron_logger)(int prio, char const *, ...) = stderr_log; int fatal_signals[] = { + SIGHUP, SIGINT, SIGQUIT, SIGTERM, @@ -339,14 +340,9 @@ main(int argc, char **argv) #else .init = crontab_scanner_schedule #endif - }, - { - .start = cron_thr_outfile_mgr, - .stop = default_stop_thread, } }; static int nthr = sizeof(thread_info) / sizeof(thread_info[0]); - set_progname(argv[0]); @@ -497,7 +493,6 @@ main(int argc, char **argv) sigaddset(&sigs, SIGPIPE); sigaddset(&sigs, SIGALRM); sigaddset(&sigs, SIGCHLD); - sigaddset(&sigs, SIGHUP); pthread_sigmask(SIG_BLOCK, &sigs, NULL); micron_log(LOG_NOTICE, "cron (%s) started", PACKAGE_STRING); @@ -519,7 +514,13 @@ main(int argc, char **argv) pthread_sigmask(SIG_UNBLOCK, &sigs, NULL); /* Wait for signal to arrive */ - sigwait(&sigs, &i); + while (1) { + sigwait(&sigs, &i); + if (i == SIGHUP) + outfiles_close(); + else + break; + } micron_log(LOG_NOTICE, "cron shutting down on signal \"%s\"", strsignal(i)); diff --git a/src/micrond.h b/src/micrond.h index 1da917b..1cce2a7 100644 --- a/src/micrond.h +++ b/src/micrond.h @@ -210,7 +210,6 @@ void usercrongroup_delete(struct crongroup *host, char const *name); void *cron_thr_runner(void *ptr); void *cron_thr_cleaner(void *ptr); void stop_thr_cleaner(pthread_t tid); -void *cron_thr_outfile_mgr(void *ptr); void default_stop_thread(pthread_t tid); void restore_default_signals(void); @@ -226,4 +225,5 @@ char const *env_get(char *name, char **env); struct output_file *outfile_find(char const *name); void outfile_release(struct output_file *ofile); +void outfiles_close(void); diff --git a/src/runner.c b/src/runner.c index 606f372..5c384bf 100644 --- a/src/runner.c +++ b/src/runner.c @@ -894,12 +894,10 @@ outfile_lock(struct output_file *ofile) if (!ofile) return NULL; pthread_mutex_lock(&ofile->mutex); - ofile->refcnt++; if (!ofile->fp) { if ((ofile->fp = fopen(ofile->name, "a")) == NULL) { micron_log(LOG_ERR, "can't open output file %s: %s", ofile->name, strerror(errno)); - ofile->refcnt--; } } return ofile->fp; @@ -908,50 +906,40 @@ outfile_lock(struct output_file *ofile) static void outfile_unlock(struct output_file *ofile) { - --ofile->refcnt; pthread_mutex_unlock(&ofile->mutex); - - if (ofile->refcnt == 0) { - pthread_mutex_lock(&outfile_mutex); - LIST_REMOVE(ofile, link); - pthread_mutex_unlock(&outfile_mutex); - if (ofile->fp) - fclose(ofile->fp); - pthread_mutex_destroy(&ofile->mutex); - free(ofile); - } } void outfile_release(struct output_file *ofile) { if (ofile) { - outfile_lock(ofile); - --ofile->refcnt; - outfile_unlock(ofile); + pthread_mutex_lock(&ofile->mutex); + if (--ofile->refcnt == 0) { + pthread_mutex_lock(&outfile_mutex); + LIST_REMOVE(ofile, link); + pthread_mutex_unlock(&outfile_mutex); + if (ofile->fp) + fclose(ofile->fp); + pthread_mutex_destroy(&ofile->mutex); + free(ofile); + } + pthread_mutex_unlock(&ofile->mutex); } } -void * -cron_thr_outfile_mgr(void *ptr) +void +outfiles_close(void) { - sigset_t sigs; - sigemptyset(&sigs); - sigaddset(&sigs, SIGHUP); - while (1) { - int i; - struct output_file *ofp; - - sigwait(&sigs, &i); - pthread_mutex_lock(&outfile_mutex); - LIST_FOREACH(ofp, &outfile_head, link) { - if (ofp->fp) { - fclose(ofp->fp); - ofp->fp = NULL; - } + struct output_file *ofp; + + pthread_mutex_lock(&outfile_mutex); + LIST_FOREACH(ofp, &outfile_head, link) { + if (ofp->fp) { + fclose(ofp->fp); + ofp->fp = NULL; } - pthread_mutex_unlock(&outfile_mutex); } + pthread_mutex_unlock(&outfile_mutex); } |