int init_trqauth_log(const char *server_port) { const char *path_home = PBS_SERVER_HOME; int eventclass = PBS_EVENTCLASS_TRQAUTHD; char path_log[MAXPATHLEN + 1]; char *log_file=NULL; char error_buf[MAX_BUF]; int rc; rc = log_init(NULL, NULL); if (rc != PBSE_NONE) return(rc); log_get_set_eventclass(&eventclass, SETV); initialize_globals_for_log(server_port); sprintf(path_log, "%s/%s", path_home, TRQ_LOGFILES); if ((mkdir(path_log, 0755) == -1) && (errno != EEXIST)) { openlog("daemonize_trqauthd", LOG_PID | LOG_NOWAIT, LOG_DAEMON); syslog(LOG_ALERT, "Failed to create client_logs directory: %s errno: %d error message: %s", path_log, errno, strerror(errno)); sprintf(error_buf,"Failed to create client_logs directory: %s, error message: %s",path_log,strerror(errno)); log_err(errno,__func__,error_buf); closelog(); return(PBSE_SYSTEM); } pthread_mutex_lock(&log_mutex); rc = log_open(log_file, path_log); pthread_mutex_unlock(&log_mutex); return(rc); }
int daemonize_trqauthd(const char *server_ip, int server_port, void *(*process_meth)(void *)) { int gid; pid_t pid; int rc; char error_buf[MAX_BUF]; char msg_trqauthddown[MAX_BUF]; char path_log[MAXPATHLEN + 1]; char unix_socket_name[MAXPATHLEN + 1]; char *log_file=NULL; int eventclass = PBS_EVENTCLASS_TRQAUTHD; const char *path_home = PBS_SERVER_HOME; umask(022); gid = getgid(); /* secure supplemental groups */ if(setgroups(1, (gid_t *)&gid) != 0) { fprintf(stderr, "Unable to drop secondary groups. Some MAC framework is active?\n"); snprintf(error_buf, sizeof(error_buf), "setgroups(group = %lu) failed: %s\n", (unsigned long)gid, strerror(errno)); fprintf(stderr, "%s\n", error_buf); return(1); } if (getenv("PBSDEBUG") != NULL) debug_mode = TRUE; if (debug_mode == FALSE) { pid = fork(); if(pid > 0) { /* parent. We are done */ return(0); } else if (pid < 0) { /* something went wrong */ fprintf(stderr, "fork failed. errno = %d\n", errno); return(PBSE_RMSYSTEM); } else { fprintf(stderr, "trqauthd daemonized - port %d\n", server_port); /* If I made it here I am the child */ fclose(stdin); fclose(stdout); fclose(stderr); /* We closed 0 (stdin), 1 (stdout), and 2 (stderr). fopen should give us 0, 1 and 2 in that order. this is a UNIX practice */ if (fopen("/dev/null", "r") == NULL) perror(__func__); if (fopen("/dev/null", "r") == NULL) perror(__func__); if (fopen("/dev/null", "r") == NULL) perror(__func__); } } else { fprintf(stderr, "trqauthd port: %d\n", server_port); } log_init(NULL, NULL); log_get_set_eventclass(&eventclass, SETV); initialize_globals_for_log(server_port); sprintf(path_log, "%s/%s", path_home, TRQ_LOGFILES); if ((mkdir(path_log, 0755) == -1) && (errno != EEXIST)) { openlog("daemonize_trqauthd", LOG_PID | LOG_NOWAIT, LOG_DAEMON); syslog(LOG_ALERT, "Failed to create client_logs directory: errno: %d", errno); log_err(errno,"daemonize_trqauthd", "Failed to create client_logs directory"); closelog(); } pthread_mutex_lock(log_mutex); log_open(log_file, path_log); pthread_mutex_unlock(log_mutex); /* start the listener */ snprintf(unix_socket_name, sizeof(unix_socket_name), "%s/%s", TRQAUTHD_SOCK_DIR, TRQAUTHD_SOCK_NAME); rc = start_domainsocket_listener(unix_socket_name, process_meth); if(rc != PBSE_NONE) { openlog("daemonize_trqauthd", LOG_PID | LOG_NOWAIT, LOG_DAEMON); syslog(LOG_ALERT, "trqauthd could not start: %d\n", rc); log_err(rc, "daemonize_trqauthd", (char *)"trqauthd could not start"); pthread_mutex_lock(log_mutex); log_close(1); pthread_mutex_unlock(log_mutex); if (changed_msg_daem && msg_daemonname) { free(msg_daemonname); } clean_log_init_mutex(); exit(-1); } snprintf(msg_trqauthddown, sizeof(msg_trqauthddown), "TORQUE authd daemon shut down and no longer listening on IP:port %s:%d", server_ip, server_port); log_event(PBSEVENT_SYSTEM | PBSEVENT_FORCE, PBS_EVENTCLASS_TRQAUTHD, msg_daemonname, msg_trqauthddown); pthread_mutex_lock(log_mutex); log_close(1); pthread_mutex_unlock(log_mutex); if (changed_msg_daem && msg_daemonname) { free(msg_daemonname); } clean_log_init_mutex(); exit(0); }