int main(int argc, char *argv[]) { int status; struct test_params p; status = handle_cmdline(argc, argv, &p); if (status == 0) { status = test_run(&p); } else if (status > 0) { print_usage(argv[0]); status = 0; } if (p.in_file) { fclose(p.in_file); } if (p.out_file) { fclose(p.out_file); } return status; }
int main(int argc, char **argv) { IDEAL* pp; IDEAL** Ms; IDEAL** Rads; char M_name[MAX_LABEL_LENGTH]; char N_name[MAX_LABEL_LENGTH]; /* learn who we really are */ progname = (const char *)strrchr(argv[0], '/'); progname = progname ? (progname + 1) : argv[0]; /* handle cmdline */ handle_cmdline(&argc, &argv); /* initializing needed things */ init_constants(); if ( !(i < numofMs) || !(j < nilindex) ) { fprintf(stderr, "i must be less than numofMs = m*(pi - 1) + 1, " "j must be less than nilindex = l*(p - 1) + 1.\n"); exit(EXIT_FAILURE); } /* we need at most 2 IDEALs per array */ Ms = (IDEAL**) malloc(2*sizeof(IDEAL*)); Rads = (IDEAL**) malloc(2*sizeof(IDEAL*)); if (Ms == NULL || Rads == NULL) { fprintf(stderr, "Unable to allocate memory for ideals' arrays.\n"); exit(EXIT_FAILURE); } /* do the job */ if ((output_control & WAS_SPECIFIED_M) && (output_control & WAS_SPECIFIED_RAD)) { /* both M_pi(m,i) and Rad^j were specified, process only them*/ pp = ideal_create(q); ideal_init(pp, pi, m, i); Ms[0] = pp; pp = ideal_create(q); ideal_init(pp, p, l, l*(p - 1) - j); Rads[0] = pp; sprintf(M_name, "M_%llu(%u,%llu)", pi, m, i); sprintf(N_name, "Rad^%llu", j); if (ideal_issubset(Rads[0], Ms[0])) { print_diff(Ms[0], Ms[0], Rads[0], M_name, N_name, use_colors); if (debug >= 2) { fprintf(stdout, "%s\t\t=\n", N_name); if (use_colors) { color_ideal_print(SPECIAL_COLOR, Rads[0], isspecial_integer); } else { ideal_print(Rads[0]); } fprintf(stdout, "\n"); } } else if (ideal_issubset(Ms[0], Rads[0])) { print_diff(Rads[0], Rads[0], Ms[0], N_name, M_name, use_colors); if (debug >= 2) { fprintf(stdout, "%s\t\t=\n", M_name); if (use_colors) { color_ideal_print(SPECIAL_COLOR, Ms[0], isspecial_integer); } else { ideal_print(Ms[0]); } fprintf(stdout, "\n"); } } else { /* we will store intersection here */ pp = ideal_create(q); Ms[1] = pp; if (debug >= 2) { fprintf(stdout, "%s\t\t=\n", M_name); if (use_colors) { color_ideal_print(SPECIAL_COLOR, Ms[0], isspecial_integer); } else { ideal_print(Ms[0]); } fprintf(stdout, "\n"); } if (debug >= 2) { fprintf(stdout, "%s\t\t=\n", N_name); if (use_colors) { color_ideal_print(SPECIAL_COLOR, Rads[0], isspecial_integer); } else { ideal_print(Rads[0]); } fprintf(stdout, "\n"); } ideal_intersect(Ms[1], Ms[0], Rads[0]); sprintf(N_name, "(M_%llu(%u,%llu) /\\ Rad^%llu)", pi, m, i, j); sprintf(M_name, "M_%llu(%u,%llu)", pi, m, i); print_diff(Ms[0], Ms[0], Ms[1], M_name, N_name, use_colors); sprintf(M_name, "Rad^%llu", j); print_diff(Rads[0], Rads[0], Ms[1], M_name, N_name, use_colors); } } else if (output_control & WAS_SPECIFIED_RAD) { /* only Rad^j was specified, find sup and inf M_pi(m,k) and process all three of them */ unsigned long long Mpi_to_Rad, Rad_to_Mpi; Rad_to_Mpi = maximum_Pi_for_P(l*(p - 1) - j, p, m); Mpi_to_Rad = minimum_Pi_for_P(l*(p - 1) - j, p, m, lambda); if (Mpi_to_Rad == Rad_to_Mpi) { fprintf(stdout, "Rad^%llu == M_%llu(%u,%llu)\n\n", j, pi, m, Rad_to_Mpi); goto end; } pp = ideal_create(q); ideal_init(pp, pi, m, Rad_to_Mpi); Ms[0] = pp; pp = ideal_create(q); ideal_init(pp, pi, m, Mpi_to_Rad); Ms[1] = pp; pp = ideal_create(q); ideal_init(pp, p, l, l*(p - 1) - j); Rads[0] = pp; fprintf(stdout, "M_%llu(%u,%llu) -> Rad^%llu -> M_%llu(%u,%llu)\n\n", pi, m, Mpi_to_Rad, j, pi, m, Rad_to_Mpi); sprintf(N_name, "Rad^%llu", j); sprintf(M_name, "M_%llu(%u,%llu)", pi, m, Mpi_to_Rad); print_diff(Ms[1], Ms[1], Rads[0], M_name, N_name, use_colors); sprintf(M_name, "M_%llu(%u,%llu)", pi, m, Rad_to_Mpi); print_diff(Ms[0], Rads[0], Ms[0], N_name, M_name, use_colors); if (debug >= 2) { fprintf(stdout, "%s\t\t=\n", N_name); if (use_colors) { color_ideal_print(SPECIAL_COLOR, Rads[0], isspecial_integer); } else { ideal_print(Rads[0]); } fprintf(stdout, "\n"); } } else if (output_control & WAS_SPECIFIED_M) { /* only M_pi(m,i) was specified, find sup and inf Rad^j and process all three of them */ unsigned long long Mpi_to_Rad, Rad_to_Mpi; Rad_to_Mpi = minimum_P_for_Pi(i, p, m); Mpi_to_Rad = maximum_P_for_Pi(i, p, m, l, lambda, numofMs - 1); if (Mpi_to_Rad == Rad_to_Mpi) { fprintf(stdout, "M_%llu(%u,%llu) == Rad^%llu\n\n", pi, m, i, l*(p - 1) - Rad_to_Mpi); goto end; } pp = ideal_create(q); ideal_init(pp, p, l, Mpi_to_Rad); Rads[0] = pp; pp = ideal_create(q); ideal_init(pp, p, l, Rad_to_Mpi); Rads[1] = pp; pp = ideal_create(q); ideal_init(pp, pi, m, i); Ms[0] = pp; fprintf(stdout, "Rad^%llu -> M_%llu(%u,%llu) -> Rad^%llu\n\n", l*(p - 1) - Rad_to_Mpi, pi, m, i, l*(p - 1) - Mpi_to_Rad); sprintf(M_name, "M_%llu(%u,%llu)", pi, m, i); sprintf(N_name, "Rad^%llu", l*(p - 1) - Rad_to_Mpi); print_diff(Rads[1], Rads[1], Ms[0], N_name, M_name, use_colors); sprintf(N_name, "Rad^%llu", l*(p - 1) - Mpi_to_Rad); print_diff(Rads[0], Ms[0], Rads[0], M_name, N_name, use_colors); if (debug >= 2) { fprintf(stdout, "%s\t\t=\n", M_name); if (use_colors) { color_ideal_print(SPECIAL_COLOR, Ms[0], isspecial_integer); } else { ideal_print(Ms[0]); } fprintf(stdout, "\n"); } } else { fprintf(stderr, "Incorrect usage. See --help.\n"); } end: /* do cleanup */ dbg_msg_l(5, "Freeing Ms...\n"); for (i = 0; i < 2; ++i) { ideal_free(Ms[i]); } free(Ms); dbg_msg_l(5, "Freeing Rads...\n"); for (i = 0; i < 2; ++i) { ideal_free(Rads[i]); } free(Rads); return 0; }
static int awk_main_2 (MyAwk& awk, int argc, qse_char_t* argv[]) { MyAwk::Run* run; cmdline_t cmdline; int n; awk.setTrait (awk.getTrait() | QSE_AWK_FLEXMAP | QSE_AWK_RWPIPE | QSE_AWK_NEXTOFILE); // ARGV[0] if (awk.addArgument (QSE_T("awk25")) <= -1) { print_error (awk); return -1; } if ((n = handle_cmdline (awk, argc, argv, &cmdline)) <= 0) return n; MyAwk::Source* in, * out; MyAwk::SourceString in_str (cmdline.ins); MyAwk::SourceFile in_file (cmdline.inf); MyAwk::SourceFile out_file (cmdline.outf); in = (cmdline.ins)? (MyAwk::Source*)&in_str: (MyAwk::Source*)&in_file; out = (cmdline.outf)? (MyAwk::Source*)&out_file: &MyAwk::Source::NONE; run = awk.parse (*in, *out); if (run == QSE_NULL) { print_error (awk); return -1; } if (cmdline.fs) { MyAwk::Value fs (run); if (fs.setStr (cmdline.fs) <= -1) { print_error (awk); return -1; } if (awk.setGlobal (QSE_AWK_GBL_FS, fs) <= -1) { print_error (awk); return -1; } } if (cmdline.outc) { if (awk.addConsoleOutput (cmdline.outc) <= -1) { print_error (awk); return -1; } } MyAwk::Value ret; if (awk.loop (&ret) <= -1) { print_error (awk); return -1; } return 0; }
int main(int argc, char **argv) { int event_fd; int sock_fd = -1; /* init to avoid a compiler warning */ /* learn who we really are */ progname = (const char *)strrchr(argv[0], '/'); progname = progname ? (progname + 1) : argv[0]; /* handle the commandline */ handle_cmdline(&argc, &argv); /* close any extra file descriptors */ close_fds(); /* actually open the event file */ event_fd = open(eventfile, O_RDONLY); if (event_fd < 0) { fprintf(stderr, "%s: can't open %s: %s\n", progname, eventfile, strerror(errno)); exit(EXIT_FAILURE); } fcntl(event_fd, F_SETFD, FD_CLOEXEC); /* * if there is data, and the kernel is NOT broken, this eats 1 byte. We * can't have that. This is ifdef'ed out on the assumption that old kernels * are out of popular use, by now. */ #ifdef TEST_FOR_BAD_KERNELS /* * Older kernels did not support read() properly or poll() at all * Check that the kernel supports the proper semantics, or die. * * Good kernels will respect O_NONBLOCK and return -1. Bad kernels * will ignore O_NONBLOCK and return 0. Really bad kernels will block * and overflow the buffer. Can't deal with the really bad ones. */ { int fl; char buf; fl = fcntl(event_fd, F_GETFL); fcntl(event_fd, F_SETFL, fl | O_NONBLOCK); if (read(event_fd, &buf, 1) == 0) { fprintf(stderr, "%s: this kernel does not support proper " "event file handling.\n" "Please get the patch from " "http://acpid.sourceforge.net\n", progname); exit(EXIT_FAILURE); } fcntl(event_fd, F_SETFL, fl); } #endif /* open our socket */ if (!nosocket) { sock_fd = ud_create_socket(socketfile); if (sock_fd < 0) { fprintf(stderr, "%s: can't open socket %s: %s\n", progname, socketfile, strerror(errno)); exit(EXIT_FAILURE); } fcntl(sock_fd, F_SETFD, FD_CLOEXEC); chmod(socketfile, socketmode); if (socketgroup) { struct group *gr; struct stat buf; gr = getgrnam(socketgroup); if (!gr) { fprintf(stderr, "%s: group %s does not exist\n", progname, socketgroup); exit(EXIT_FAILURE); } if (stat(socketfile, &buf) < 0) { fprintf(stderr, "%s: can't stat %s\n", progname, socketfile); exit(EXIT_FAILURE); } if (chown(socketfile, buf.st_uid, gr->gr_gid) < 0) { fprintf(stderr, "%s: chown(): %s\n", progname, strerror(errno)); exit(EXIT_FAILURE); } } } /* if we're running in foreground, we don't daemonize */ if (!foreground) { if (daemonize() < 0) exit(EXIT_FAILURE); } /* open the log */ if (open_log() < 0) { exit(EXIT_FAILURE); } acpid_log(LOG_INFO, "starting up\n"); /* trap key signals */ signal(SIGHUP, reload_conf); signal(SIGINT, clean_exit); signal(SIGQUIT, clean_exit); signal(SIGTERM, clean_exit); signal(SIGPIPE, SIG_IGN); /* read in our configuration */ if (acpid_read_conf(confdir)) { exit(EXIT_FAILURE); } /* create our pidfile */ if (create_pidfile() < 0) { exit(EXIT_FAILURE); } /* main loop */ acpid_log(LOG_INFO, "waiting for events: event logging is %s\n", logevents ? "on" : "off"); while (1) { struct pollfd ar[2]; int r; int fds = 0; /* poll for the socket and the event file */ ar[0].fd = event_fd; ar[0].events = POLLIN; fds++; if (!nosocket) { ar[1].fd = sock_fd; ar[1].events = POLLIN; fds++; } r = poll(ar, fds, -1); if (r < 0 && errno == EINTR) { continue; } else if (r < 0) { acpid_log(LOG_ERR, "poll(): %s\n", strerror(errno)); continue; } /* house keeping */ acpid_close_dead_clients(); /* was it an event? */ if (ar[0].revents) { char *event; struct stat trash; int fexists; /* check for existence of a lockfile */ fexists = (stat(lockfile, &trash) == 0); /* this shouldn't happen */ if (!ar[0].revents & POLLIN) { acpid_log(LOG_DEBUG, "odd, poll set flags 0x%x\n", ar[0].revents); continue; } /* read an event */ event = read_line(event_fd); /* if we're locked, don't process the event */ if (fexists) { if (logevents) { acpid_log(LOG_INFO, "lockfile present, not processing " "event \"%s\"\n", event); } continue; } /* handle the event */ if (event) { if (logevents) { acpid_log(LOG_INFO, "received event \"%s\"\n", event); } acpid_handle_event(event); if (logevents) { acpid_log(LOG_INFO, "completed event \"%s\"\n", event); } } else if (errno == EPIPE) { acpid_log(LOG_WARNING, "events file connection closed\n"); break; } else { static int nerrs; if (++nerrs >= ACPID_MAX_ERRS) { acpid_log(LOG_ERR, "too many errors reading " "events file - aborting\n"); break; } } } /* was it a new connection? */ if (!nosocket && ar[1].revents) { int cli_fd; struct ucred creds; char buf[32]; static int accept_errors; /* this shouldn't happen */ if (!ar[1].revents & POLLIN) { acpid_log(LOG_DEBUG, "odd, poll set flags 0x%x\n", ar[1].revents); continue; } /* accept and add to our lists */ cli_fd = ud_accept(sock_fd, &creds); if (cli_fd < 0) { acpid_log(LOG_ERR, "can't accept client: %s\n", strerror(errno)); accept_errors++; if (accept_errors >= 5) { acpid_log(LOG_ERR, "giving up\n"); clean_exit_with_status(EXIT_FAILURE); } continue; } accept_errors = 0; if (creds.uid != 0 && non_root_clients >= clientmax) { close(cli_fd); acpid_log(LOG_ERR, "too many non-root clients\n"); continue; } if (creds.uid != 0) { non_root_clients++; } fcntl(cli_fd, F_SETFD, FD_CLOEXEC); snprintf(buf, sizeof(buf)-1, "%d[%d:%d]", creds.pid, creds.uid, creds.gid); acpid_add_client(cli_fd, buf); } } clean_exit_with_status(EXIT_SUCCESS); return 0; }
int main(int argc, char **argv) { sigset_t sigmask; struct pollfd pollfds[NPFD_COUNT]; int npollfds; int k; int64_t wait_ms; struct paging_data pd0; struct paging_data pd1; bool recheck_time; int init_pass = 0; /* * if running interactively, output initially to the terminal, * otherwise to syslog right away */ if (isatty(fileno(stderr))) { log_fp = stderr; log_syslog = false; } else { log_fp = NULL; log_syslog = true; } if (log_syslog) openlog(progname, LOG_CONS | LOG_PID, LOG_DAEMON); /* parse arguments */ handle_cmdline(argc, argv); if (run_as_daemon) daemonize(); debug_msg(1, "debug level set to %d", debug_level); // page_size = sysconf(_SC_PAGESIZE); // if (page_size <= 0) // fatal_msg("unable to get system page size"); /* * Block signals so that they aren't handled according to their * default dispositions */ sigemptyset(&sigmask); sigaddset(&sigmask, SIGHUP); sigaddset(&sigmask, SIGTERM); if (sigprocmask(SIG_BLOCK, &sigmask, NULL) < 0) fatal_perror("sigprocmask"); /* * Receive signals on file descriptors */ sigemptyset(&sigmask); sigaddset(&sigmask, SIGHUP); fd_sighup = signalfd(-1, &sigmask, 0); if (fd_sighup < 0) fatal_perror("signalfd(SIGHUP)"); sigemptyset(&sigmask); sigaddset(&sigmask, SIGTERM); fd_sigterm = signalfd(-1, &sigmask, 0); if (fd_sigterm < 0) fatal_perror("signalfd(SIGTERM)"); /* select clock to use */ select_clk_id(); /* verify we are running under a hypervisor */ verify_hypervisor(); pollfds[NPFD_SIGTERM].fd = fd_sigterm; pollfds[NPFD_SIGTERM].events = POLLIN|POLLPRI; pollfds[NPFD_SIGHUP].fd = fd_sighup; pollfds[NPFD_SIGHUP].events = POLLIN|POLLPRI; /* keep xs poll fd last in the array */ pollfds[NPFD_XS].fd = -1; pollfds[NPFD_XS].events = POLLIN|POLLPRI; /* initialize xenstore structure */ initialize_xs(pollfds); pollfds[NPFD_XS].fd = xs_fileno(xs); get_paging_data(&pd0); for (;;) { try_subscribe_membalance_settings(); /* calculate sleep time till next sample point */ if (initialized_xs) { wait_ms = timespec_diff_ms(pd0.ts, getnow()) + interval * MSEC_PER_SEC; } else { /* if xs not initialzied, keep retrying */ wait_ms = interval * MSEC_PER_SEC; if (++init_pass > 30) wait_ms *= 2; } if (wait_ms >= tolerance_ms) { for (k = 0; k < countof(pollfds); k++) pollfds[k].revents = 0; /* if settings need to be updated, retry in 1 sec or sooner */ if (need_update_membalance_settings) wait_ms = min(wait_ms, MSEC_PER_SEC); /* include xs in the poll only if has initialized xs */ npollfds = countof(pollfds); if (!initialized_xs) npollfds--; if (poll(pollfds, npollfds, wait_ms) < 0) { if (errno == EINTR) continue; fatal_perror("poll"); } recheck_time = false; handle_signals(pollfds, &recheck_time); if (!initialized_xs) { /* try to initialzie xs */ initialize_xs(NULL); /* if successful, start monitoring page map-in rate */ if (initialized_xs) get_paging_data(&pd0); continue; } if (pollfds[NPFD_XS].revents & (POLLIN|POLLPRI)) { /* a watched value in xenstore has been changed */ handle_xs_watch(); recheck_time = true; } if (need_update_membalance_settings) update_membalance_settings(); if (recheck_time) { /* go sleep again if wait interval has not expired yet */ if (timespec_diff_ms(getnow(), pd0.ts) < interval * MSEC_PER_SEC - tolerance_ms) continue; } } if (!initialized_xs) { /* try to initialzie xs */ initialize_xs(NULL); /* if successful, start monitoring page map-in rate */ if (initialized_xs) get_paging_data(&pd0); continue; } /* process sample and set it as the new "previous" one */ get_paging_data(&pd1); process_sample(&pd1, &pd0); pd0 = pd1; } /* close connection to xenstore */ shutdown_xs(); return EXIT_SUCCESS; }