void get_event(XEvent *ev) { fd_set rfds; int nfds=wglobal.conn; while(1){ check_signals(); if(QLength(wglobal.dpy)>0){ XNextEvent(wglobal.dpy, ev); return; } XFlush(wglobal.dpy); FD_ZERO(&rfds); FD_SET(wglobal.conn, &rfds); set_input_fds(&rfds, &nfds); if(select(nfds+1, &rfds, NULL, NULL, NULL)>0){ check_input_fds(&rfds); if(FD_ISSET(wglobal.conn, &rfds)){ XNextEvent(wglobal.dpy, ev); return; } } } }
/* loop forever. doState() has a switch for the actions and events to be checked for 'port_state'. the actions and events may or may not change 'port_state' by calling toState(), but once they are done we loop around again and perform the actions required for the new 'port_state'. */ void protocol(RunTimeOpts *rtOpts, PtpClock *ptpClock) { DBG("event POWERUP\n"); toState(PTP_INITIALIZING, rtOpts, ptpClock); DBG("Debug Initializing...\n"); for (;;) { /* 20110701: this main loop was rewritten to be more clear */ if (ptpClock->portState == PTP_INITIALIZING) { if (!doInit(rtOpts, ptpClock)) { return; } } else { doState(rtOpts, ptpClock); } if (ptpClock->message_activity) DBGV("activity\n"); /* Perform the heavy signal processing synchronously */ check_signals(rtOpts, ptpClock); } }
static int read_data(char *buf) { int count; int sig = 0; while(1){ count = read(DATA_PATH , buf, MAX_DATA); if(count < 0){ #ifdef DEBUG write_log("LM(P) read error\n"); #endif /* read error !? and check */ sig = check_signals(); if(signal_last(sig)) continue; /* retry */ if(signal_start(sig)){ start_signal = 0; continue; } return -1; /* SIGTERM or EAGAIN or other read error */ } break; /* read SUCCESS */ } return count; }
void work_hard() { check_signals(); if (is_empty()) { receive_work(); } assert(!is_empty()); //printf("%d %d %d\n",count,count_elements(),q_size()); tCell *t = dequeue(); assert(t->term != NULL); assert(t->digest != NULL); // printf("process: %s ...",t->term); if (!contains_processed(t->digest)) { hit++; int i; int l = atoi(t->term); for (i=0;i<N;i++) { if (produce_work(l,i)) { char *r = malloc(10); sprintf(r,"%d",i); char *d = malloc(20); sha1(r,d); if (!contains(d)) { // put_local(d); enqueue(r,d); send_digest_queued(send_hashes,d); } else { free(r); free(d); } } } // printf("%s done\n",node); send_digest_processed(send_hashes, t->digest); } else { cache++; // printf("cache hit\n"); } free(t->term); free(t->digest); free(t); // printf("%d\n",q_size()); // print_queue(); }
static void sp_updating(MafwSource *self, gint progress, gint processed_items, gint remaining_items, gint remaining_time) { fail_if(progress != 25, "Wrong updating progress"); fail_if(processed_items != 4, "Wrong updating processed items"); fail_if(remaining_items != 6, "Wrong updating remaining items"); fail_if(remaining_time != 12, "Wrong updating remaining time"); updt_chd = TRUE; check_signals(); }
int wait_restart(void) { int sig; /* wait restart(BSCC success) */ while(1){ sig = check_signals(); if(signal_end(sig)) return DETECT_END; /* SIGTERM */ if(signal_start(sig)){ start_signal = 0; return DETECT_START; } pause(); /* wait signal */ } }
void get_event_mask(XEvent *ev, long mask) { fd_set rfds; bool found=FALSE; while(1){ check_signals(); while(XCheckMaskEvent(wglobal.dpy, mask, ev)){ if(ev->type!=MotionNotify) return; found=TRUE; } if(found) return; FD_ZERO(&rfds); FD_SET(wglobal.conn, &rfds); select(wglobal.conn+1, &rfds, NULL, NULL, NULL); } }
int main(int argc, char *argv[]) { int foreground = 0, do_syslog = 1; fd_set fds; int nfds, fd, n = -1, i, ch; struct sigaction sa; struct option long_options[] = { { "config", 1, 0, 'f' }, { "no-fallback", 0, 0, 500 }, { "disable-vifs", 0, 0, 'N' }, { "foreground", 0, 0, 'n' }, { "help", 0, 0, 'h' }, { "ident", 1, 0, 'I' }, { "loglevel", 1, 0, 'l' }, { "pidfile", 1, 0, 'P' }, { "syslog", 0, 0, 's' }, { "table-id", 1, 0, 't' }, { "version", 0, 0, 'v' }, { NULL, 0, 0, 0 } }; snprintf(versionstring, sizeof (versionstring), "pimd version %s", PACKAGE_VERSION); prognm = ident = progname(argv[0]); while ((ch = getopt_long(argc, argv, "d:f:hI:l:nNP:rst:v", long_options, NULL)) != EOF) { const char *errstr; switch (ch) { case 'd': { char *p,*q; size_t i, len; struct debugname *d; debug = 0; p = optarg; q = NULL; while (p) { q = strchr(p, ','); if (q) *q++ = '\0'; len = strlen(p); for (i = 0, d = debugnames; i < ARRAY_LEN(debugnames); i++, d++) { if (len >= d->nchars && strncmp(d->name, p, len) == 0) break; } if (i == ARRAY_LEN(debugnames)) return usage(1); debug |= d->level; p = q; } } break; case 'f': config_file = optarg; break; case 500: no_fallback = 1; break; case 'h': return usage(0); case 'I': /* --ident=NAME */ ident = optarg; break; case 'l': loglevel = loglvl(optarg); if (-1 == loglevel) return usage(1); break; case 'n': do_syslog--; foreground = 1; break; case 'N': do_vifs = 0; break; case 'P': /* --pidfile=NAME */ pid_file = strdup(optarg); break; case 'r': retry_forever++; break; case 's': do_syslog++; break; case 't': mrt_table_id = strtonum(optarg, 0, 999999999, &errstr); if (errstr) { fprintf(stderr, "Table ID %s!\n", errstr); return usage(1); } break; case 'v': printf("%s\n", versionstring); return 0; default: return usage(1); } } argc -= optind; if (argc > 0) return usage(1); if (geteuid() != 0) errx(1, "Need root privileges to start."); compose_paths(); setlinebuf(stderr); if (debug != 0) { struct debugname *d; char c; int tmpd = debug; fprintf(stderr, "debug level 0x%lx ", debug); c = '('; for (d = debugnames; d < debugnames + ARRAY_LEN(debugnames); d++) { if ((tmpd & d->level) == d->level) { tmpd &= ~d->level; fprintf(stderr, "%c%s", c, d->name); c = ','; } } fprintf(stderr, ")\n"); } if (!debug && !foreground) { /* Detach from the terminal */ haveterminal = 0; if (fork()) exit(0); close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); fd = open("/dev/null", O_RDWR, 0); if (fd >= 0) { dup2(fd, STDIN_FILENO); dup2(fd, STDOUT_FILENO); dup2(fd, STDERR_FILENO); close(fd); } #ifdef SYSV setpgrp(); #else #ifdef TIOCNOTTY fd = open("/dev/tty", 2); if (fd >= 0) { (void)ioctl(fd, TIOCNOTTY, (char *)0); close(fd); } #else if (setsid() < 0) perror("setsid"); #endif /* TIOCNOTTY */ #endif /* SYSV */ } /* End of child process code */ /* * Create directory for runtime files */ if (-1 == mkdir(_PATH_PIMD_RUNDIR, 0755) && errno != EEXIST) err(1, "Failed creating %s directory for runtime files", _PATH_PIMD_RUNDIR); /* * Setup logging */ log_init(haveterminal && do_syslog > 0); logit(LOG_NOTICE, 0, "%s starting ...", versionstring); do_randomize(); callout_init(); init_igmp(); init_pim(); init_routesock(); /* Both for Linux netlink and BSD routing socket */ init_pim_mrt(); init_timers(); ipc_init(); /* Start up the log rate-limiter */ resetlogging(NULL); /* TODO: check the kernel DVMRP/MROUTED/PIM support version */ init_vifs(); init_rp_and_bsr(); /* Must be after init_vifs() */ add_static_rp(); /* Must be after init_vifs() */ #ifdef RSRR rsrr_init(); #endif /* RSRR */ sa.sa_handler = handle_signals; sa.sa_flags = 0; /* Interrupt system calls */ sigemptyset(&sa.sa_mask); sigaction(SIGALRM, &sa, NULL); sigaction(SIGHUP, &sa, NULL); sigaction(SIGTERM, &sa, NULL); sigaction(SIGINT, &sa, NULL); sigaction(SIGUSR1, &sa, NULL); sigaction(SIGUSR2, &sa, NULL); IF_DEBUG(DEBUG_IF) dump_vifs(stderr); IF_DEBUG(DEBUG_PIM_MRT) dump_pim_mrt(stderr); /* schedule first timer interrupt */ timer_setTimer(TIMER_INTERVAL, timer, NULL); if (pidfile(pid_file)) warn("Cannot create pidfile"); /* * Main receive loop. */ while (1) { if (check_signals()) break; FD_ZERO(&fds); for (i = 0, nfds = 0; i < nhandlers; i++) { FD_SET(ihandlers[i].fd, &fds); if (ihandlers[i].fd >= nfds) nfds = ihandlers[i].fd + 1; } n = select(nfds, &fds, NULL, NULL, timeout(n)); if (n < 0) { if (errno != EINTR) /* SIGALRM is expected */ logit(LOG_WARNING, errno, "select failed"); continue; } for (i = 0; n > 0 && i < nhandlers; i++) { if (FD_ISSET(ihandlers[i].fd, &fds)) ihandlers[i].func(ihandlers[i].fd); } } logit(LOG_NOTICE, 0, "%s exiting.", versionstring); cleanup(); exit(0); }
static unsigned int data_output_main(int sm_sock_fd, int fix_size) { int sig = 0; int r_size; int w_size; int dev_path = PRNT_PATH; unsigned int total = 0; /* total data size (wrote size) */ char *buf; char *ptr; /* read buffer get */ buf = (char *)malloc(MAX_DATA); if(!buf) exit(0); /* no ram */ /* print data read(from stdin) and write(to stdout) */ while((r_size = read_data(buf)) > 0){ ptr = buf; /* data print(output) loop */ for(;r_size > 0;r_size -= w_size){ int __attribute__ ((unused)) err; signal_block(sigmask); /* signal block */ get_printer_sem(sem_id); /* printer lock */ w_size = p_dvacs->ptdev_write(dev_path, ptr, r_size); /* data write -> printer */ err = errno; #ifdef DATA_LOG if(log_hand >=0 && w_size > 0) write(log_hand, ptr, w_size); #endif /* write() error check (power off??) */ if(w_size <= 0){ w_size = 0; if(check_power(sm_sock_fd, dev_path) == LM_PRN_POWOFF){ #ifdef DEBUG write_log("LM(P) into wait_restart()\n"); #endif release_printer_sem(sem_id); signal_unblock(sigmask); /* wait BSCC SUCCESS */ if(wait_restart() == DETECT_END) goto dataout_exit; else continue; } #ifdef DEBUG fprintf(log_path, "LM(P) write error. normal=%x\n",err); fflush(log_path); #endif sleep(1); } else total += w_size; /* total write size save */ /* write() is normal end */ release_printer_sem(sem_id); signal_unblock(sigmask); if((sig = check_signals()) != 0){ /* signal detect */ if(signal_end(sig)){ goto dataout_exit; /* SIGTERM */ } /* other signal ignore */ start_signal = 0; last_signal = 0; } ptr += w_size; } /* written data size == request data size ?*/ if(fix_size && (total >= fix_size) ) break; } dataout_exit: free(buf); return total; }
int main (int argc, char *argv[]) { DBusConnection *conn[NUM_CONN]; DBusConnection *monitor; DBusError error; int i; int test_data_len; test_data_len = sizeof (test_data) / sizeof (CommandAndResult); dbus_error_init (&error); conn[0] = dbus_bus_get_private (DBUS_BUS_SESSION, &error); if (dbus_error_is_set (&error)) { fprintf (stderr, "*** Failed to open connection 0 to session bus: %s\n", error.message); dbus_error_free (&error); return 1; } if (!match_acquired_or_lost_signal (conn[0], "NameAcquired", dbus_bus_get_unique_name (conn[0]))) return 1; conn[1] = dbus_bus_get_private (DBUS_BUS_SESSION, &error); if (dbus_error_is_set (&error)) { fprintf (stderr, "*** Failed to open connection 1 to session bus: %s\n", error.message); dbus_error_free (&error); return 1; } if (!match_acquired_or_lost_signal (conn[1], "NameAcquired", dbus_bus_get_unique_name (conn[1]))) return 1; conn[2] = dbus_bus_get_private (DBUS_BUS_SESSION, &error); if (dbus_error_is_set (&error)) { fprintf (stderr, "*** Failed to open connection 2 to session bus: %s\n", error.message); dbus_error_free (&error); return 1; } if (!match_acquired_or_lost_signal (conn[2], "NameAcquired", dbus_bus_get_unique_name (conn[2]))) return 1; conn[3] = dbus_bus_get_private (DBUS_BUS_SESSION, &error); if (dbus_error_is_set (&error)) { fprintf (stderr, "*** Failed to open connection 3 to session bus: %s\n", error.message); dbus_error_free (&error); return 1; } if (!match_acquired_or_lost_signal (conn[3], "NameAcquired", dbus_bus_get_unique_name (conn[3]))) return 1; monitor = dbus_bus_get (DBUS_BUS_SESSION, &error); if (dbus_error_is_set (&error)) { fprintf (stderr, "*** Failed to open monitoring connection to session bus: %s\n", error.message); dbus_error_free (&error); return 1; } if (!match_acquired_or_lost_signal (monitor, "NameAcquired", dbus_bus_get_unique_name (monitor))) return 1; dbus_bus_add_match (monitor, "", &error); if (dbus_error_is_set (&error)) { fprintf (stderr, "*** Failed to set filter on monitoring connection: %s\n", error.message); dbus_error_free (&error); return 1; } for (i = 0; i < NUM_CONN; i++) dbus_connection_set_exit_on_disconnect (conn[i], FALSE); for (i = 0; i < test_data_len; i++) { dbus_uint32_t result; result = 0; if (test_data[i].command == ADD_CONNECTION) { result = dbus_bus_request_name (conn[test_data[i].connection_number], TEST_NAME, test_data[i].flags, &error); if (dbus_error_is_set (&error)) { fprintf (stderr, "Error on addition in iteration %i: %s\n", i, error.message); dbus_error_free (&error); return 1; } } else if (test_data[i].command == REMOVE_CONNECTION) { result = dbus_bus_release_name (conn[test_data[i].connection_number], TEST_NAME, &error); if (dbus_error_is_set (&error)) { fprintf (stderr, "*** Failed to remove connection %i in iteration %i: %s\n", test_data[i].connection_number, i, error.message); dbus_error_free (&error); return 1; } } else { fprintf (stderr, "Command #%i not a valid command!\n", test_data[i].command); return 1; } if (result != test_data[i].expected_result) { fprintf (stderr, "Results recived (%i) are not the expected results (%i) in iteration %i\n", result, test_data[i].expected_result, i); return 1; } if (!check_connection (monitor, i, conn)) { fprintf (stderr, "Failed at iteration %i\n", i); return 1; } if (!check_signals (monitor, i, conn)) { fprintf (stderr, "Failed at iteration %i\n", i); return 1; } } return 0; }
static void sp_container_changed(MafwSource *self, const gchar *object_id) { fail_if(strcmp(object_id, "str_oid") != 0, "Wrong object_id"); cont_chd = TRUE; check_signals(); }
static void sp_metadata_changed(MafwSource *self, const gchar *object_id) { fail_if(strcmp(object_id, "str") != 0, "Wrong object_id"); mdata_chd = TRUE; check_signals(); }
int main(int argc, char *argv[]) { mrp_mainloop_t *ml; mrp_clear(&cfg); parse_cmdline(&cfg, argc, argv); mrp_log_set_mask(cfg.log_mask); mrp_log_set_target(cfg.log_target); ml = mainloop_create(&cfg); if (ml == NULL) fatal("failed to create main loop."); dbus_test.ml = ml; setup_dbus_tests(ml); ml = dbus_test.ml; setup_timers(ml); setup_io(ml); setup_signals(ml); MRP_UNUSED(setup_deferred); /* XXX TODO: add deferred tests... */ #ifdef GLIB_ENABLED if (cfg.mainloop_type != MAINLOOP_GLIB && cfg.mainloop_type != MAINLOOP_QT) { if (cfg.ngio > 0 || cfg.ngtimer > 0) glib_pump_setup(ml); } setup_glib_io(); setup_glib_timers(); #endif if (mrp_add_timer(ml, 1000, check_quit, NULL) == NULL) fatal("failed to create quit-check timer"); setup_wakeup(ml); mainloop_run(&cfg); check_io(); check_timers(); check_signals(); #ifdef GLIB_ENABLED check_glib_io(); check_glib_timers(); #endif if (dbus_test.client != 0) close(dbus_test.pipe[1]); /* let the client continue */ check_dbus(); #ifdef GLIB_ENABLED if (cfg.mainloop_type != MAINLOOP_GLIB) { if (cfg.ngio > 0 || cfg.ngtimer > 0) glib_pump_cleanup(); } #endif cleanup_wakeup(); mainloop_cleanup(&cfg); }