#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "config.h" #include "macros.h" #define UNIX_PATH_MAX 108 pid_t fork_proc() { /* Fork off the parent. */ pid_t pid = fork(); /* The process id is non zero, so it must be the parent process. */ if (pid) { /* Exit with EXIT_FAILURE if the parent's pid is * negative, otherwise exit with EXIT_SUCCESS. */ exit((pid < 0) ? EXIT_FAILURE : EXIT_SUCCESS); } return pid; } void child_handler(int sig_num) { signal(SIGCHLD, child_handler); } void hangup_handler(int sig_num) { signal(SIGHUP, hangup_handler); } void init_daemon(int change_dir, char *path) { /* Fork the parent. */ pid_t pid = fork_proc(); /* Exit if the child process isn't the session leader. */ if (setsid() < 0) { exit(EXIT_FAILURE); } /* Catch, ignore, and/or handle signals. */ signal(SIGCHLD, child_handler); signal(SIGHUP, hangup_handler); /* Fork again. */ pid = fork_proc(); /* Set new file permissions. */ umask(0); /* If change_dir is true, change the working directory to path, or * the root directory if path isn't set. */ if (change_dir) { chdir((!path) ? "/" : path); } /* Close any open file descriptors. */ for (int i = sysconf(_SC_OPEN_MAX); i >= 0; close(i--)); /* Open the logfile. */ openlog("pullreqd", LOG_PID, LOG_DAEMON); } int init_config(char *config_file, config **cfg) { log(LOG_INFO, "Reading config file %s.", config_file); *cfg = parse_config(config_file); /* Did the config file parser succeed? */ if (*cfg != NULL) { log(LOG_INFO, "Successfully read %s.", config_file); } else { log(LOG_ERR, "Error reading %s.", config_file); } return (*cfg != NULL); } int init_socket(config *cfg) { union sock_addr { struct sockaddr sa; struct sockaddr_un sa_un; struct sockaddr_in sa_in; } sock_addr; /* Size of the socket address we're using. */ size_t sockaddr_size = 0; /* Set socket type to unix socket, if socket-type was set to unix, otherwise set it to network socket. */ const int sock_type = (strcasecmp(cfg->sock_type, "unix") == 0) ? AF_UNIX : AF_INET; if (sock_type == AF_UNIX) { size_t path_length = strlen(cfg->sock); /* Is the path too long? */ if (path_length >= UNIX_PATH_MAX) { log(LOG_ERR, "The socket path is too long, and exceeded %i characters.", UNIX_PATH_MAX); return -1; } sockaddr_size = sizeof(sock_addr.sa_un); sock_addr.sa_un.sun_family = sock_type; memcpy(sock_addr.sa_un.sun_path, cfg->sock, path_length+1); } else { struct addrinfo hints, *ainfo_root, *ainfo; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_SEQPACKET; /*hints.ai_protocol = IPPROTO_SCTP;*/ hints.ai_flags = AI_PASSIVE; const int gai_ret = getaddrinfo(cfg->sock, cfg->port, &hints, &ainfo_root); /* Did getaddrinfo fail? */ if (gai_ret) { log_reason(LOG_ERR, "getaddrinfo() failed for hostname %s.", cfg->sock, gai_strerror(gai_ret)); return -1; } /* TODO: Create sockets for all resolved addresses, instead of just the first. */ sockaddr_size = ainfo_root->ai_addrlen; sock_addr.sa = *ainfo_root->ai_addr; freeaddrinfo(ainfo_root); } /* Create a new listen socket. */ const int fd = socket(sock_addr.sa.sa_family, SOCK_SEQPACKET, 0); /* Did we fail to create the listen socket? */ if (fd < 0) { log_reason(LOG_ERR, "Failed to create listen socket.", strerror(errno)); return -1; } /* Did we fail to bind the listen socket? */ if (bind(fd, &sock_addr.sa, sockaddr_size) < 0) { log_reason(LOG_ERR, "Failed to bind listen socket.", strerror(errno)); return -1; } int one = 1; /* Did we fail to enable SO_REUSEADDR? */ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) { log_reason(LOG_ERR, "Failed to enable SO_REUSEADDR.", strerror(errno)); return -1; } /* Did we fail to listen to the socket? */ if (listen(fd, 20) < 0) { log_reason(LOG_ERR, "Failed to listen to socket.", strerror(errno)); return -1; } log(LOG_INFO, "Successfully created socket descriptor %i for address %s.", fd, cfg->sock); return fd; } int main_loop(config *cfg, int listen_socket) { int done = 0; for (; !done;) { break; } return 0; } void cleanup(config *cfg, int listen_socket) { if (listen_socket > 0) { close(listen_socket); /* Is this a unix domain socket? */ if (strcasecmp(cfg->sock_type, "unix") == 0) { unlink(cfg->sock); } } if (cfg) { cleanup_config(cfg); } } int main(int argc, char **argv) { config *cfg; char *config_file = "test.conf"; int config_read = 0; int listen_socket = -1; int exit_status = EXIT_FAILURE; init_daemon(1, "/"); log(LOG_NOTICE, "pullreqd started."); /* Read the config file. */ config_read = init_config(config_file, &cfg); /* Did we successfully read the config file? */ if (config_read) { /* Create the Listen socket. */ listen_socket = init_socket(cfg); } /* Did we successfully read the config file, and create the listen socket? */ if (config_read && listen_socket >= 0) { exit_status = main_loop(cfg, listen_socket); } cleanup(cfg, listen_socket); log(LOG_NOTICE, "pullreqd stopped."); closelog(); return exit_status; }