Navigation


Changeset 1410:4ddd4dcd6bb6 in freeDiameter


Ignore:
Timestamp:
Feb 18, 2020, 3:21:07 PM (4 years ago)
Author:
Luke Mewburn <luke@mewburn.net>
Branch:
default
Phase:
public
committer:
Luke Mewburn <luke@mewburn.net> 1582007256 -39600
Message:

freeDiameterd: add pidfile support

Implement -p filename / --pidfile=filename to write
the pidfile to filename after daemonisation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • freeDiameterd/main.c

    r1409 r1410  
    4646#include <syslog.h>
    4747#include <stdarg.h>
     48#include <stdio.h>
    4849#include <stdlib.h>
     50#include <string.h>
    4951#include <unistd.h>
    5052
     
    5759static int daemon_mode = 0;
    5860static int gnutls_debug = 0;
     61static char *pidfile = NULL;
    5962
    6063/* gnutls debug */
    6164static void fd_gnutls_debug(int level, const char * str) {
    6265        fd_log_debug(" [gnutls:%d] %s", level, str);
     66}
     67
     68static void pidfile_cleanup(void)
     69{
     70        if (pidfile != NULL) {
     71                LOG_I("Removing pidfile '%s'", pidfile);
     72                CHECK_SYS_DO( unlink(pidfile), /* ignore */ );
     73                pidfile = NULL;
     74        }
     75}
     76
     77static int pidfile_create(void)
     78{
     79        if (pidfile == NULL) {
     80                return 0;
     81        }
     82
     83        /* Create pidfile */
     84        FILE * fp = fopen(pidfile, "w");
     85        if (fp == NULL) {
     86                int ret = errno;
     87                LOG_F("Unable to write pidfile '%s'; Error: %s",
     88                        pidfile, strerror(ret));
     89                pidfile = NULL; /* disable pidfile_cleanup() */
     90                return ret;
     91        }
     92
     93        /* Cleaup pidfile on exit */
     94        if (atexit(pidfile_cleanup) != 0) {
     95                LOG_F("Unable to setup pidfile cleanup");
     96                CHECK_SYS( fclose(fp) );
     97                pidfile_cleanup();
     98                return EINVAL;
     99        }
     100
     101        /* Write the pid and close pidfile */
     102        fprintf(fp, "%d\n", getpid());
     103        CHECK_SYS_DO( fclose(fp), { pidfile_cleanup(); return __ret__; } );
     104
     105        LOG_I("Created pidfile '%s'", pidfile);
     106        return 0;
    63107}
    64108
     
    119163        }
    120164
     165        CHECK_FCT( pidfile_create() );
     166
    121167        /* Initialize the core library */
    122168        ret = fd_core_initialize();
     
    186232                "                          default location (" DEFAULT_CONF_PATH "/" FD_DEFAULT_CONF_FILENAME ")\n"
    187233                "  -D, --daemon            Start program in background\n"
     234                "  -p, --pidfile=filename  Write PID to filename\n"
    188235                "  -s, --syslog            Write log output to syslog (instead of stdout)\n");
    189236        printf( "\nDebug:\n"
     
    211258                { "syslog",     no_argument,            NULL, 's' },
    212259                { "daemon",     no_argument,            NULL, 'D' },
     260                { "pidfile",    required_argument,      NULL, 'p' },
    213261                { "debug",      no_argument,            NULL, 'd' },
    214262                { "quiet",      no_argument,            NULL, 'q' },
     
    222270        /* Loop on arguments */
    223271        while (1) {
    224                 c = getopt_long (argc, argv, "hVc:Ddql:f:F:g:s", long_options, &option_index);
     272                c = getopt_long (argc, argv, "hVc:Dp:dql:f:F:g:s", long_options, &option_index);
    225273                if (c == -1)
    226274                        break;  /* Exit from the loop.  */
     
    245293                        case 'D':
    246294                                daemon_mode = 1;
     295                                break;
     296
     297                        case 'p':       /* Write pidfile */
     298                                if (optarg == NULL ) {
     299                                        fprintf(stderr, "Missing argument with --pidfile directive\n");
     300                                        return EINVAL;
     301                                }
     302                                pidfile = optarg;
    247303                                break;
    248304
Note: See TracChangeset for help on using the changeset viewer.