void check_sig(int signr) { int nr; char buffer[MAX_LOGMESSAGE_SIZE]; switch (signr) { case SIGINT: LOG(0, "Caught SIGINT - Good bye\n"); exit(EXIT_SUCCESS); break; case SIGTERM: LOG(0, "Caught Termination Signal - Astalavista... baby\n"); exit(EXIT_SUCCESS); break; case SIGHUP: LOG(0, "Caught SIGHUP - reloading configuration\n"); sighup_detected = TRUE; process_configfile(config_file); if (check_needed_config_options() != 0) { LOG(0, "There is an error in the config! Exiting...\n"); exit(EXIT_FAILURE); } prepare_vars(); LOG(0, "Configuration reload succesfull.\n"); break; default: snprintf(buffer, sizeof(buffer - 1), "Caught the Signal '%d' but don't care about this.\n", signr); LOG(2, buffer); break; } }
int main(int argc, char **argv) { int i = 0; int filecounter = 0, pthread_ret = 0; double load; char buffer[MAX_LOGMESSAGE_SIZE]; FILE *fppid = NULL; struct dirent **namelist; load = 0.0; if (process_arguments(argc, argv) == EXIT_FAILURE) exit(EXIT_FAILURE); process_configfile(config_file); if (loglevel == -1) { printf("DEBUG: Config File = %s\n", config_file); printf("CONFIG_OPT_LOGTYPE = %s\n", macro_x[CONFIG_OPT_LOGTYPE]); printf("CONFIG_OPT_LOGFILE = %s\n", macro_x[CONFIG_OPT_LOGFILE]); printf("CONFIG_OPT_LOGFILESIZE = %s\n", macro_x[CONFIG_OPT_LOGFILESIZE]); printf("CONFIG_OPT_LOGLEVEL = %s\n", macro_x[CONFIG_OPT_LOGLEVEL]); printf("CONFIG_OPT_SCANDIR = %s\n", macro_x[CONFIG_OPT_SCANDIR]); printf("CONFIG_OPT_RUNCMD = %s\n", macro_x[CONFIG_OPT_RUNCMD]); printf("CONFIG_OPT_RUNCMD_ARG = %s\n", macro_x[CONFIG_OPT_RUNCMD_ARG]); printf("CONFIG_OPT_MAXTHREADS = %s\n", macro_x[CONFIG_OPT_MAXTHREADS]); printf("CONFIG_OPT_LOAD = %s\n", macro_x[CONFIG_OPT_LOAD]); printf("CONFIG_OPT_USER = %s\n", macro_x[CONFIG_OPT_USER]); printf("CONFIG_OPT_GROUP = %s\n", macro_x[CONFIG_OPT_GROUP]); printf("CONFIG_OPT_PIDFILE = %s\n", macro_x[CONFIG_OPT_PIDFILE]); printf("CONFIG_OPT_SLEEPTIME = %s\n", macro_x[CONFIG_OPT_SLEEPTIME]); printf("CONFIG_OPT_IDENTMYSELF = %s\n", macro_x[CONFIG_OPT_IDENTMYSELF]); printf("---------------------------\n"); if (check_needed_config_options() != 0) { printf("There is an Error! Exiting...\n"); exit(EXIT_FAILURE); } } if (prepare_vars() != 0) exit(EXIT_FAILURE); if (loglevel == -1) printf("DEBUG: load_threshold is %s - ('%f')\n", use_load_threshold ? "enabled" : "disabled", load_threshold); pthread_t th[max_threads]; for (i=0;i<max_threads;i++){ th[i] = (pthread_t) NULL; } i = 0; /* Nice point for another function to set * the internal vars from macro_x[] */ /* Start in Daemon Mode or in foreground? */ if (daemon_mode == TRUE) start_daemon("NPCD", LOG_LOCAL0); else if (use_syslog) openlog("NPCD", LOG_PID | LOG_CONS | LOG_NDELAY, LOG_LOCAL0); /* Create PID File or exit on failure */ if (daemon_mode == TRUE && sighup_detected == FALSE) { fppid = fopen(pidfile, "w"); if (fppid == NULL) { printf("Could not open pidfile '%s': %s\n", pidfile, strerror(errno)); exit(EXIT_FAILURE); } else { fprintf(fppid, "%d", getpid()); fclose(fppid); } } /* Try to drop the privileges */ if (drop_privileges(user, group) == EXIT_FAILURE) exit(EXIT_FAILURE); snprintf(buffer, sizeof(buffer) - 1, "%s Daemon (%s) started with PID=%d\n", progname, PACKAGE_VERSION, getpid()); LOG(0, buffer); snprintf(buffer, sizeof(buffer) - 1, "Please have a look at '%s -V' to get license information\n", progname); LOG(0, buffer); //sigemptyset(); handle_signal(SIGINT, check_sig); handle_signal(SIGHUP, check_sig); handle_signal(SIGTERM, check_sig); snprintf(buffer, sizeof(buffer) - 1, "HINT: load_threshold is %s - ('%f')\n", use_load_threshold ? "enabled" : "disabled", load_threshold); LOG(0, buffer); /* beginn main loop */ while (1) { if (chdir(directory) != 0) exit(EXIT_FAILURE); /* is_file() filter may cause trouble on some systems * like Solaris or HP-UX that don't have a d-type * member in struct dirent */ /* #ifdef HAVE_STRUCT_DIRENT_D_TYPE if ( ( filecounter = scandir( directory, &namelist, is_file, alphasort ) ) < 0 ) { #else */ if ((filecounter = scandir(directory, &namelist, 0, alphasort)) < 0) { /* #endif */ snprintf(buffer, sizeof(buffer) - 1, "Error while get file list from spooldir (%s) - %s\n", directory, strerror(errno)); LOG(0, buffer); snprintf(buffer, sizeof(buffer) - 1, "Exiting...\n"); LOG(0, buffer); if (daemon_mode != TRUE) printf("Error while get file list from spooldir (%s) - %s\n", directory, strerror(errno)); break; } snprintf(buffer, sizeof(buffer) - 1, "Found %d files in %s\n", filecounter, directory); LOG(2, buffer); for (i = 0, namelist; i < filecounter; i++) { #ifdef HAVE_GETLOADAVG if (use_load_threshold == TRUE) { load = getload(1); snprintf(buffer, sizeof(buffer) - 1, "DEBUG: load %f/%f\n", load, load_threshold); LOG(2, buffer); } if (use_load_threshold && (load > load_threshold)) { snprintf(buffer, sizeof(buffer) - 1, "WARN: MAX load reached: load %f/%f at i=%d", load, load_threshold, i); LOG(0, buffer); if (i > 0) i--; sleep(sleeptime); continue; } #endif snprintf(buffer, sizeof(buffer) - 1, "ThreadCounter %d/%d File is %s\n", thread_counter, max_threads, namelist[i]->d_name); LOG(2, buffer); struct stat attribute; if (stat(namelist[i]->d_name, &attribute) == -1) { LOG(0, "Error while getting file status"); break; } if (strstr((namelist[i]->d_name), "-PID-") != NULL) { snprintf( buffer, sizeof(buffer) - 1, "File '%s' is an already in process PNP file. Leaving it untouched.\n", namelist[i]->d_name); LOG(1, buffer); continue; } if (S_ISREG(attribute.st_mode)) { snprintf(buffer, sizeof(buffer) - 1, "Regular File: %s\n", namelist[i]->d_name); LOG(2, buffer); /* only start new threads if the max_thread config option is not reached */ if (thread_counter < max_threads && we_should_stop == FALSE) { if ((pthread_ret = pthread_create(&th[thread_counter], NULL, processfile, namelist[i]->d_name)) != 0) { snprintf(buffer, sizeof(buffer) - 1, "Could not create thread... exiting with error '%s'\n", strerror(errno)); LOG(0, buffer); exit(EXIT_FAILURE); } snprintf(buffer, sizeof(buffer) - 1, "A thread was started on thread_counter = %d\n", thread_counter); LOG(2, buffer); thread_counter++; } else if (we_should_stop == TRUE) break; else { snprintf( buffer, sizeof(buffer) - 1, "WARN: MAX Thread reached: %s comes later with ThreadCounter: %d\n", namelist[i]->d_name, thread_counter); LOG(2, buffer); i--; for (thread_counter = thread_counter; thread_counter > 0; thread_counter--) { snprintf(buffer, sizeof(buffer) - 1, "DEBUG: Will wait for th['%d']\n", thread_counter - 1); LOG(2, buffer); pthread_join(th[thread_counter - 1], NULL); } } } } if (thread_counter > 0) { /* Wait for open threads before working on the next run */ snprintf(buffer, sizeof(buffer) - 1, "Have to wait: Filecounter = %d - thread_counter = %d\n", filecounter - 2, thread_counter); LOG(2, buffer); for (thread_counter = thread_counter; thread_counter > 0; thread_counter--) pthread_join(th[thread_counter - 1], NULL); } if (we_should_stop == TRUE) break; for (i = 0, namelist; i < filecounter; i++) { free(namelist[i]); } free(namelist); snprintf(buffer, sizeof(buffer) - 1, "No more files to process... waiting for %d seconds\n", sleeptime); LOG(1, buffer); sleep(sleeptime); } snprintf(buffer, sizeof(buffer) - 1, "Daemon ended. PID was '%d'\n", getpid()); LOG(0, buffer); if (use_syslog) closelog(); return EXIT_SUCCESS; }