Changeset 1410:4ddd4dcd6bb6 in freeDiameter
- Timestamp:
- Feb 18, 2020, 3:21:07 PM (4 years ago)
- Branch:
- default
- Phase:
- public
- committer:
- Luke Mewburn <luke@mewburn.net> 1582007256 -39600
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
freeDiameterd/main.c
r1409 r1410 46 46 #include <syslog.h> 47 47 #include <stdarg.h> 48 #include <stdio.h> 48 49 #include <stdlib.h> 50 #include <string.h> 49 51 #include <unistd.h> 50 52 … … 57 59 static int daemon_mode = 0; 58 60 static int gnutls_debug = 0; 61 static char *pidfile = NULL; 59 62 60 63 /* gnutls debug */ 61 64 static void fd_gnutls_debug(int level, const char * str) { 62 65 fd_log_debug(" [gnutls:%d] %s", level, str); 66 } 67 68 static 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 77 static 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; 63 107 } 64 108 … … 119 163 } 120 164 165 CHECK_FCT( pidfile_create() ); 166 121 167 /* Initialize the core library */ 122 168 ret = fd_core_initialize(); … … 186 232 " default location (" DEFAULT_CONF_PATH "/" FD_DEFAULT_CONF_FILENAME ")\n" 187 233 " -D, --daemon Start program in background\n" 234 " -p, --pidfile=filename Write PID to filename\n" 188 235 " -s, --syslog Write log output to syslog (instead of stdout)\n"); 189 236 printf( "\nDebug:\n" … … 211 258 { "syslog", no_argument, NULL, 's' }, 212 259 { "daemon", no_argument, NULL, 'D' }, 260 { "pidfile", required_argument, NULL, 'p' }, 213 261 { "debug", no_argument, NULL, 'd' }, 214 262 { "quiet", no_argument, NULL, 'q' }, … … 222 270 /* Loop on arguments */ 223 271 while (1) { 224 c = getopt_long (argc, argv, "hVc:D dql: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); 225 273 if (c == -1) 226 274 break; /* Exit from the loop. */ … … 245 293 case 'D': 246 294 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; 247 303 break; 248 304
Note: See TracChangeset
for help on using the changeset viewer.