/** * main() * * Initializes the program configuration and state, then starts the beacon thread before * finally entering the main loop that reads stdin and outputs log entries to any hosts * on the target list. **/ int main (int argc, char **argv) { pthread_t beacon_thread; pthread_attr_t beacon_thread_attr; int fd = 0; int result = 0; result = arguments_parse(argc, argv); if (result <= 0) { return result; } #ifdef __DEBUG__ printf("udploggerd.c debug: parameter listen_port = '%u'\n", conf.listen_port); printf("udploggerd.c debug: parameter minimum_target_age = '%lu'\n", conf.maximum_target_age); printf("udploggerd.c debug: parameter prune_target_interval = '%ld'\n", conf.prune_target_interval); if (conf.tag) { printf("udploggerd.c debug: parameter tag = '%s'\n", conf.tag); } else { printf("udploggerd.c debug: parameter tag = NULL\n"); } #endif /* Set up the socket that will be used to send logging data. */ fd = bind_socket(conf.listen_port, 0); if (fd < 0) { fprintf(stderr, "udploggerd.c could not setup logging socket\n"); return -1; } /* Initialize our send targets mutex. */ pthread_mutex_init(&targets_mutex, NULL); /* Start our beacon-listener thread. */ pthread_attr_init(&beacon_thread_attr); pthread_attr_setdetachstate(&beacon_thread_attr, PTHREAD_CREATE_DETACHED); result = pthread_create(&beacon_thread, &beacon_thread_attr, beacon_main, (void *)&fd); pthread_attr_destroy(&beacon_thread_attr); if (result) { fprintf(stderr, "udploggerd.c could not start beacon thread.\n"); return -1; } logging_loop(fd); /* Don't bother cleaning up our mutex, threads, etc. The exit procedures will take care of it. */ exit(0); }
int main(int argc, char **argv) { if (arguments_parse(argc, argv) < 0) return EXIT_FAILURE; if (arguments.help) { arguments_usage(argv[0]); return EXIT_SUCCESS; } helper hlp(arguments); return EXIT_SUCCESS; }
void INIT_CODE multiboot_init (void) { char *target = (char *) BASE_MODULE_NAME; unsigned module; memory_copy(multiboot_module_info, (multiboot_module_info_type *) multiboot_info.module_base, multiboot_info.number_of_modules * sizeof (multiboot_module_info_type)); // First of all, make sure module names and parameters are stored in a safe place. Otherwise, we may overwrite them later on // in the boot process. for (module = 0; module < multiboot_info.number_of_modules; module++) { string_copy(target, multiboot_module_info[module].name); multiboot_module_info[module].name = target; target += string_length (target) + 1; } // Now, lets parse the kernel command line. */ arguments_parse ((char *) multiboot_info.command_line, arguments_kernel, 0); }
int main (int argc, char *argv[]) { Arguments args; int i, use_taskstat; int in_initrd, clean_environment = 1; int stat_fd, disk_fd, uptime_fd, meminfo_fd, pid, ret = 1; PidScanner *scanner = NULL; unsigned long reltime = 0; BufferFile *stat_file, *disk_file, *per_pid_file, *meminfo_file; PidEventClosure pid_ev_cl; int *fds[] = { &stat_fd, &disk_fd, &uptime_fd, &meminfo_fd, NULL }; const char *fd_names[] = { "/stat", "/diskstats", "/uptime", "/meminfo", NULL }; StackMap map = STACK_MAP_INIT; /* make me findable */ arguments_set_defaults (&args); arguments_parse (&args, argc, argv); if (args.usleep_time > 0) { usleep (args.usleep_time); return 0; } if (enter_environment (args.console_debug)) return 1; fprintf (stderr, "bootchart-collector started as pid %d with %d args: ", (int) getpid(), argc - 1); for (i = 1; i < argc; i++) fprintf (stderr, "'%s' ", argv[i]); fprintf (stderr, "\n"); if (args.dump_path) { Arguments remote_args; ret = buffers_extract_and_dump (args.dump_path, &remote_args); ret |= dump_header (args.dump_path); if (!remote_args.relative_time) ret |= dump_dmsg (args.dump_path); if (!ret) cleanup_dev (); goto exit; } if (!args.relative_time) { /* manually started */ in_initrd = am_in_initrd (); if (in_initrd && sanity_check_initrd ()) goto exit; } pid = bootchart_find_running_pid (NULL); if (args.probe_running) { clean_environment = pid < 0; ret = pid < 0; goto exit; } else { if (pid >= 0) { clean_environment = 0; fprintf (stderr, "bootchart collector already running as pid %d, exiting...\n", pid); goto exit; } } /* defaults */ if (!args.hz) args.hz = 50; for (i = 0; fds [i]; i++) { char *path = malloc (strlen (PROC_PATH) + strlen (fd_names[i]) + 1); strcpy (path, PROC_PATH); strcat (path, fd_names[i]); *fds[i] = open (path, O_RDONLY); if (*fds[i] < 0) { fprintf (stderr, "error opening '%s': %s'\n", path, strerror (errno)); exit (1); } } stat_file = buffer_file_new (&map, "proc_stat.log"); disk_file = buffer_file_new (&map, "proc_diskstats.log"); if ( (use_taskstat = init_taskstat()) ) per_pid_file = buffer_file_new (&map, "taskstats.log"); else per_pid_file = buffer_file_new (&map, "proc_ps.log"); meminfo_file = buffer_file_new (&map, "proc_meminfo.log"); pid_ev_cl.cmdline_file = buffer_file_new (&map, "cmdline2.log"); pid_ev_cl.paternity_file = buffer_file_new (&map, "paternity.log"); if (!stat_file || !disk_file || !per_pid_file || !meminfo_file || !pid_ev_cl.cmdline_file || !pid_ev_cl.paternity_file) { fprintf (stderr, "Error allocating output buffers\n"); return 1; } scanner = pid_scanner_new_netlink (pid_event_cb, &pid_ev_cl); if (!scanner) scanner = pid_scanner_new_proc (PROC_PATH, pid_event_cb, &pid_ev_cl); if (!scanner) return 1; if (args.relative_time) { reltime = get_uptime (uptime_fd); if (! reltime) exit (1); } while (1) { pid_t pid; char uptime[80]; size_t uptimelen; unsigned long u; if (in_initrd) { if (have_dev_tmpfs ()) { if (chroot_into_dev ()) { fprintf (stderr, "failed to chroot into /dev - exiting so run_init can proceed\n"); return 1; } in_initrd = 0; } } u = get_uptime (uptime_fd); if (!u) return 1; uptimelen = sprintf (uptime, "%lu\n", u - reltime); buffer_file_dump_frame_with_timestamp (stat_file, stat_fd, uptime, uptimelen); buffer_file_dump_frame_with_timestamp (disk_file, disk_fd, uptime, uptimelen); buffer_file_dump_frame_with_timestamp (meminfo_file, meminfo_fd, uptime, uptimelen); /* output data for each pid */ buffer_file_append (per_pid_file, uptime, uptimelen); pid_scanner_restart (scanner); while ((pid = pid_scanner_next (scanner))) { if (use_taskstat) dump_taskstat (per_pid_file, scanner); else dump_proc_stat (per_pid_file, pid); } buffer_file_append (per_pid_file, "\n", 1); usleep (1000000 / args.hz); } /* * In reality - we are always killed before we reach * this point */ if (use_taskstat) { if (close (netlink_socket) < 0) { perror ("failed to close netlink socket"); exit (1); } } for (i = 0; fds [i]; i++) { if (close (*fds[i]) < 0) { fprintf (stderr, "error closing file '%s': %s'\n", fd_names[i], strerror (errno)); return 1; } } ret = 0; exit: arguments_free (&args); if (scanner) ret |= pid_scanner_free (scanner); if (clean_environment) clean_enviroment(); return ret; }
int main ( int _argc, char *_argv[] ) { struct ctx *ctx_p = xcalloc ( 1, sizeof ( *ctx_p ) ); argv = _argv; argc = _argc; int ret = 0, nret; //SAFE ( posixhacks_init(), errno = ret = _SAFE_rc ); ctx_p->config_group = DEFAULT_CONFIG_GROUP; ctx_p->vms_min = DEFAULT_VMS_MIN; ctx_p->vms_max = DEFAULT_VMS_MAX; ctx_p->vms_spare_min = DEFAULT_VMS_SPARE_MIN; ctx_p->vms_spare_max = DEFAULT_VMS_SPARE_MAX; strncpy ( ctx_p->listen_addr, strdup ( DEFAULT_LISTEN ), 256 ); ctx_p->flags[KILL_ON_DISCONNECT] = DEFAULT_KILL_ON_DISCONNECT; ncpus = sysconf ( _SC_NPROCESSORS_ONLN ); // Get number of available logical CPUs memory_init(); ctx_p->pid = getpid(); int quiet = 0, verbose = 3; error_init ( &ctx_p->flags[OUTPUT_METHOD], &quiet, &verbose, &ctx_p->flags[DEBUG] ); nret = arguments_parse ( argc, argv, ctx_p ); if ( nret ) ret = nret; if ( !ret ) { nret = configs_parse ( ctx_p, PS_CONFIG ); if ( nret ) ret = nret; } if ( !ctx_p->kvm_args[SHARGS_PRIMARY].c ) { char *args_line = strdup ( DEFAULT_KVM_ARGS ); parse_parameter ( ctx_p, KVM_ARGS, args_line, PS_DEFAULTS ); } debug ( 4, "ncpus == %u", ncpus ); debug ( 4, "debugging flags: %u 0 3 %u", ctx_p->flags[OUTPUT_METHOD], ctx_p->flags[DEBUG] ); { int n = 0; while ( n < SHARGS_MAX ) { kvm_args_t *args_p = &ctx_p->kvm_args[n++]; debug ( 9, "Custom arguments %u count: %u", n - 1, args_p->c ); int i = 0; while ( i < args_p->c ) { int macros_count = -1, expanded = -1; args_p->v[i] = parameter_expand ( ctx_p, args_p->v[i], 4, ¯os_count, &expanded, parameter_get, ctx_p ); debug ( 12, "args_p->v[%u] == \"%s\" (t: %u; e: %u)", i, args_p->v[i], macros_count, expanded ); if ( macros_count == expanded ) args_p->isexpanded[i]++; i++; } } } ctx_p->state = STATE_STARTING; nret = main_rehash ( ctx_p ); if ( nret ) ret = nret; { int rc = ctx_check ( ctx_p ); if ( !ret ) ret = rc; } debug ( 3, "Current errno is %i.", ret ); // == RUNNING == if ( ret == 0 ) ret = kvmpool ( ctx_p ); // == /RUNNING == main_cleanup ( ctx_p ); error_deinit(); ctx_cleanup ( ctx_p ); debug ( 1, "finished, exitcode: %i: %s.", ret, strerror ( ret ) ); free ( ctx_p ); #ifndef __FreeBSD__ // Hanging up with 100%CPU eating, https://github.com/xaionaro/clsync/issues/97 //SAFE ( posixhacks_deinit(), errno = ret = _SAFE_rc ); #endif return ret; }
/* * finds (another) bootchart-collector process and * returns it's pid (or -1) if not found, ignores * the --usleep mode we use to simplify our scripts. */ int bootchart_find_running_pid (Arguments *opt_args) { DIR *proc; struct dirent *ent; int pid = -1; char exe_path[1024]; Arguments sargs, *args; if (opt_args) { args = opt_args; } else { args = &sargs; arguments_set_defaults (args); } proc = opendir (PROC_PATH); while ((ent = readdir (proc)) != NULL) { int len; char link_target[1024]; if (!isdigit (ent->d_name[0])) continue; strcpy (exe_path, PROC_PATH); strcat (exe_path, "/"); strcat (exe_path, ent->d_name); strcat (exe_path, "/exe"); if ((len = readlink (exe_path, link_target, 1024-1)) < 0) continue; link_target[len] = '\0'; if (strstr (link_target, PROGRAM_PREFIX "bootchart" PROGRAM_SUFFIX "-collector")) { FILE *argf; int harmless = 0; int p = atoi (ent->d_name); /* log ("found collector '%s' pid %d (my pid %d)\n", link_target, p, getpid()); */ if (p == getpid()) continue; /* I'm not novel */ strcpy (exe_path + strlen (exe_path) - strlen ("/exe"), "/cmdline"); argf = fopen (exe_path, "r"); if (argf) { int i; char abuffer[4096]; len = fread (abuffer, 1, 4095, argf); if (len > 0) { /* step through args */ int argc; char *argv[128]; abuffer[len] = '\0'; argv[0] = abuffer; for (argc = i = 0; i < len && argc < 127; i++) { if (abuffer[i] == '\0') argv[++argc] = abuffer + i + 1; } arguments_set_defaults (args); arguments_parse (args, argc, argv); if (args->usleep_time) harmless = 1; } fclose (argf); } if (!harmless) { pid = p; break; } } } closedir (proc); if (args == &sargs) arguments_free (&sargs); return pid; }
/** * main() * * Initializes the program configuration and state, then enters a main loop that reads log entries * (calling the log_packet_hook function on each) and sends beacon packets. **/ int main (int argc, char **argv) { fd_set all_set; sigset_t blocked_signals; char beacon[BEACON_PACKET_SIZE]; char buffer[PACKET_MAXIMUM_SIZE]; int fd; struct log_host_t *log_host_ptr; sigset_t original_signal_mask; fd_set read_set; int result = 0; struct sockaddr_in sender; socklen_t senderlen = sizeof(sender); /* * Create a signal set that consists of all of the signals except for SIGKILL, * SIGSEGV, and SIGSTOP. Do not include those signals because blocking them * doesn't work (for SIGKILL and SIGSTOP) or they are too important to block * (for SIGSEGV). */ if (sigfillset(&blocked_signals)) { perror("udploggerclientlib.c sigfillset()"); return -1; } if (sigdelset(&blocked_signals, SIGKILL) || sigdelset(&blocked_signals, SIGSEGV) || sigdelset(&blocked_signals, SIGSTOP)) { perror("udploggerclientlib.c sigdelset()"); return -1; } /* * Block all of the signals in blocked_signals from being delivered to this * process. Save the current/original signal mask of this process * to original_signal_mask. */ if (sigemptyset(&original_signal_mask)) { perror("udploggerclientlib.c sigemptyset()"); return -1; } if (sigprocmask(SIG_BLOCK, &blocked_signals, &original_signal_mask)) { perror("udploggerclientlib.c sigprocmask()"); return -1; } /* * Clear out the list of signals that have been received by this process, * so that every signal reads as "not received". */ if (sigemptyset(&signal_flags)) { perror("udploggerclientlib.c sigemptyset()"); return -1; } /* * Install signal handlers that will record the receipt of signals that * we are interested in. */ if (signal(SIGALRM, sig_handler) == SIG_ERR) { perror("udploggerclientlib.c signal(SIGALRM)"); return -1; } if (signal(SIGTERM, sig_handler) == SIG_ERR) { perror("udploggerclientlib.c signal(SIGTERM)"); return -1; } if (signal(SIGHUP, sig_handler) == SIG_ERR) { perror("udploggerclientlib.c signal(SIGHUP)"); return -1; } result = arguments_parse(argc, argv); if (result <= 0) { return result; } #ifdef __DEBUG__ printf("udploggerclientlib.c debug: parameter beacon_interval = '%lu'\n", udploggerclientlib_conf.beacon_interval); log_host_ptr = &udploggerclientlib_conf.log_host; while (log_host_ptr) { printf("udploggerclientlib.c debug: logging host %s:%u\n", inet_ntoa(log_host_ptr->address.sin_addr), ntohs(log_host_ptr->address.sin_port)); log_host_ptr = log_host_ptr->next; } #endif fd = bind_socket(0, 0); if (fd < 0) { fprintf(stderr, "udploggerclientlib.c could not setup socket\n"); return -1; } strncpy(beacon, BEACON_STRING, BEACON_PACKET_SIZE); beacon[BEACON_PACKET_SIZE - 1] = '\0'; /* * Send an ALARM signal to ourselves so that we immediately broadcast a beacon * on startup. Note that this signal is currently blocked but will be delivered * as soon as we unblock signals (i.e. on pselect). */ raise(SIGALRM); FD_ZERO(&all_set); FD_SET(fd, &all_set); while (1) { read_set = all_set; /* * Wait for either a log packet to arrive or the receipt of a * signal. */ result = pselect(fd+1, &read_set, NULL, NULL, NULL, &original_signal_mask); if (result < 0) { /* * If we were not interrupted by a signal, die with a pselect() error. */ if (errno != EINTR) { perror("udploggerclientlib.c pselect()"); return -1; } /* * If we have received a SIGALRM, send out a beacon and then start another * alarm counter so that it will trigger to send out the next one. */ if (sigismember(&signal_flags, SIGALRM)) { #ifdef __DEBUG__ printf("udploggerclientlib.c debug: ALRM received, sending beacon\n"); #endif log_host_ptr = &udploggerclientlib_conf.log_host; do { sendto(fd, beacon, BEACON_PACKET_SIZE, 0, (struct sockaddr *)&log_host_ptr->address, sizeof(log_host_ptr->address)); } while ((log_host_ptr = log_host_ptr->next)); alarm(udploggerclientlib_conf.beacon_interval); } handle_signal_hook(&signal_flags); } else if (FD_ISSET(fd, &read_set)) { if (recvfrom(fd, buffer, PACKET_MAXIMUM_SIZE, 0, (struct sockaddr *)&sender, &senderlen) >= 0) { buffer[PACKET_MAXIMUM_SIZE - 1] = '\0'; log_packet_hook(&sender, buffer); } } if (sigismember(&signal_flags, SIGTERM)) { #ifdef __DEBUG__ printf("udploggerclientlib.c debug: exiting normally\n"); #endif break; } /* * Clear out the list of signals that have been received by this process, * so that every signal reads as "not received". */ if (sigemptyset(&signal_flags)) { perror("udploggerclientlib.c sigemptyset()"); return -1; } } return 0; }
int main(int argc, char* argv[]) { struct arguments arguments; struct board board; char buffer[256]; int format; char input; char* message; int status; // Game status. struct termios term_settings; int valid; // Parse arguments. message = arguments_parse(&arguments, argc, argv); if (message) { usage_print(message); } // Apply arguments. valid = 1; // Hack; overload to determine whether to play or quit. if (arguments.flags & ARGUMENTS_VERSION) { printf("%s\n", VERSION); valid = 0; } if (arguments.flags & ARGUMENTS_LEGAL) { printf("%s\n", legal); valid = 0; } if (arguments.flags & ARGUMENTS_HELP) { usage_print(NULL); } if (!valid) { exit(EXIT_SUCCESS); } if (arguments.flags & ARGUMENTS_MODE) { if (arguments.mode == mode_format) { setup_signal_handlers(); enter_alternate_buffer(); enter_format_mode(&term_settings); format = 1; } else if (arguments.mode == mode_plain) { format = 0; } } else if (isatty(STDOUT_FILENO) && isatty(STDIN_FILENO)) { setup_signal_handlers(); enter_alternate_buffer(); enter_format_mode(&term_settings); format = 1; } if (arguments.flags & ARGUMENTS_SEED) { srand(arguments.seed); } else { srand(time(NULL)); } // Set up the board. board_init(&board); if (!format) { fputs(legal, stdout); } // Play the game. play: valid = 1; while (!(status = board_done(&board))) { // Set up screen for next move. // Sorry about this ugly call. screen_print(&board, valid ? (format ? "\n\n" : "") : "\nInvalid move.\n", format); // Get the player's move. input = yoink(format); // Process player's move. if (input == 'w' || input == 'k') { valid = board_move_up(&board); } else if (input == 's' || input == 'j') { valid = board_move_down(&board); } else if (input == 'a' || input == 'h') { valid = board_move_left(&board); } else if (input == 'd' || input == 'l') { valid = board_move_right(&board); } else if (input == 'n') { // Start a new game (or not) based on user input. printf("Start a new game? [y/N] "); input = yoink(format); if (input == 'y' || input == 'Y') { board_reset(&board); } continue; } else if (input == '?') { help_print(); if (format) { printf("\nPress any key to continue."); input = yoink(format); } continue; } else { valid = 0; } // End player's move. if (valid) { board_plop(&board); } } // Print the final board. snprintf(buffer, sizeof(buffer), "\nGame over, you %s!\n\nPlay again? [y/n]\n", (status < 0) ? "LOSE" : "WIN"); screen_print(&board, buffer, format); // Check for new game. while ((input = yoink(format)) != 'y' && input != 'Y' && input != 'n' && input != 'N') { screen_print(&board, buffer, format); } if (input == 'y' || input == 'Y') { board_reset(&board); goto play; } // Restore the terminal. if (format) { restore_mode(); leave_alternate_buffer(); } // Free the board. board_free(&board); // Return success. return EXIT_SUCCESS; }