It's meant to be a daemon, so it should run in the background. The
-f option will cause it to run in the foreground for debugging etc.
* Copyright 2004 Project Purple
*/
* Copyright 2004 Project Purple
*/
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include "onak-conf.h"
#include "parsekey.h"
#include "onak-conf.h"
#include "parsekey.h"
+void daemonize(void)
+{
+ pid_t pid;
+
+ pid = fork();
+
+ if (pid < 0) {
+ logthing(LOGTHING_CRITICAL,
+ "Failed to fork into background: %d (%s)",
+ errno,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ } else if (pid > 0) {
+ logthing(LOGTHING_INFO, "Backgrounded as pid %d.", pid);
+ exit(EXIT_SUCCESS);
+ }
+
+ pid = setsid();
+
+ freopen("/dev/null", "r", stdin);
+ freopen("/dev/null", "w", stdout);
+ freopen("/dev/null", "w", stderr);
+
+ return;
+}
+
void iteratefunc(void *ctx, struct openpgp_publickey *key)
{
struct openpgp_packet_list *packets = NULL;
void iteratefunc(void *ctx, struct openpgp_publickey *key)
{
struct openpgp_packet_list *packets = NULL;
fd_set rfds;
char sockname[1024];
char *configfile = NULL;
fd_set rfds;
char sockname[1024];
char *configfile = NULL;
+ bool foreground = false;
- while ((optchar = getopt(argc, argv, "c:")) != -1 ) {
+ while ((optchar = getopt(argc, argv, "c:f")) != -1 ) {
switch (optchar) {
case 'c':
configfile = strdup(optarg);
break;
switch (optchar) {
case 'c':
configfile = strdup(optarg);
break;
+ case 'f':
+ foreground = true;
+ break;
configfile = NULL;
initlogthing("keyd", config.logfile);
configfile = NULL;
initlogthing("keyd", config.logfile);
+ if (!foreground) {
+ daemonize();
+ }
+
catchsignals();
snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);
catchsignals();
snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET);