void mnav_close() { //close the serial port sPort2.close_port(); //close files logging_close(); }
int main(int argc, char **argv) { struct drcom_handle *h; int daemon = 1; int i; if(argc > 2) usage(); for (i = 1 ; i < argc; i++) { char *arg = argv[i]; if (strcmp(arg, "--nodaemon") == 0 || strcmp(arg, "-n") == 0) { printf("%s: log to stderr.\n", argv[0]); daemon = 0; } } /* Initialize the handle for the lifetime of the daemon */ h = drcom_create_handle(); if(drcom_init(h)<0){ logerr("conf file err\n"); exit(-1); } load_kernel_module(); if (daemon) daemonize(); logging_init("drcomd", daemon); drcomd_daemon(h); logging_close(); return 0; }
int main(int argc, char * argv[]) { /* State variables. */ struct serverpool * SP; struct dynamodb_request_queue * QW; struct dynamodb_request_queue * QR; struct dispatch_state * D; int s; /* Command-line parameters. */ char * opt_k = NULL; char * opt_l = NULL; char * opt_p = NULL; char * opt_r = NULL; char * opt_s = NULL; char * opt_t = NULL; int opt_1 = 0; /* Working variable. */ char * dynamodb_host; char * key_id; char * key_secret; struct sock_addr ** sas; struct logging_file * logfile; struct capacity_reader * M; const char * ch; WARNP_INIT; /* Parse the command line. */ while ((ch = GETOPT(argc, argv)) != NULL) { GETOPT_SWITCH(ch) { GETOPT_OPTARG("-k"): if (opt_k != NULL) usage(); if ((opt_k = strdup(optarg)) == NULL) OPT_EPARSE(ch, optarg); break; GETOPT_OPTARG("-l"): if (opt_l != NULL) usage(); if ((opt_l = strdup(optarg)) == NULL) OPT_EPARSE(ch, optarg); break; GETOPT_OPTARG("-p"): if (opt_p != NULL) usage(); if ((opt_p = strdup(optarg)) == NULL) OPT_EPARSE(ch, optarg); break; GETOPT_OPTARG("-r"): if (opt_r != NULL) usage(); if ((opt_r = strdup(optarg)) == NULL) OPT_EPARSE(ch, optarg); break; GETOPT_OPTARG("-s"): if (opt_s != NULL) usage(); if ((opt_s = strdup(optarg)) == NULL) OPT_EPARSE(ch, optarg); break; GETOPT_OPTARG("-t"): if (opt_t != NULL) usage(); if ((opt_t = strdup(optarg)) == NULL) OPT_EPARSE(ch, optarg); break; GETOPT_OPT("--version"): fprintf(stderr, "dynamodb-kv @VERSION@\n"); exit(0); GETOPT_OPT("-1"): if (opt_1 != 0) usage(); opt_1 = 1; break; GETOPT_MISSING_ARG: warn0("Missing argument to %s\n", ch); usage(); GETOPT_DEFAULT: warn0("illegal option -- %s\n", ch); usage(); } } argc -= optind; argv += optind; /* We should have processed all the arguments. */ if (argc != 0) usage(); /* Verify that we have mandatory options. */ if (opt_k == NULL) usage(); if (opt_r == NULL) usage(); if (opt_s == NULL) usage(); if (opt_t == NULL) usage(); /* Construct the DynamoDB endpoint host name. */ if (asprintf(&dynamodb_host, "dynamodb.%s.amazonaws.com:80", opt_r) == -1) { warnp("asprintf"); exit(1); } /* Start looking up addresses for DynamoDB endpoints. */ if ((SP = serverpool_create(dynamodb_host, 15, 120)) == NULL) { warnp("Error starting DNS lookups for %s", dynamodb_host); exit(1); } /* Read the key file. */ if (aws_readkeys(opt_k, &key_id, &key_secret)) { warnp("Error reading AWS keys from %s", opt_k); exit(1); } /* Create DynamoDB request queues for writes and reads. */ if ((QW = dynamodb_request_queue_init(key_id, key_secret, opt_r, SP)) == NULL) { warnp("Error creating DynamoDB request queue"); exit(1); } if ((QR = dynamodb_request_queue_init(key_id, key_secret, opt_r, SP)) == NULL) { warnp("Error creating DynamoDB request queue"); exit(1); } /* Start reading table throughput parameters. */ if ((M = capacity_init(key_id, key_secret, opt_t, opt_r, SP, QW, QR)) == NULL) { warnp("Error reading DynamoDB table metadata"); exit(1); } /* Resolve the listening address. */ if ((sas = sock_resolve(opt_s)) == NULL) { warnp("Error resolving socket address: %s", opt_s); exit(1); } if (sas[0] == NULL) { warn0("No addresses found for %s", opt_s); exit(1); } /* Create and bind a socket, and mark it as listening. */ if (sas[1] != NULL) warn0("Listening on first of multiple addresses found for %s", opt_s); if ((s = sock_listener(sas[0])) == -1) exit(1); /* If requested, create a log file. */ if (opt_l != NULL) { if ((logfile = logging_open(opt_l)) == NULL) { warnp("Cannot open log file"); exit(1); } dynamodb_request_queue_log(QW, logfile); dynamodb_request_queue_log(QR, logfile); } else { logfile = NULL; } /* Daemonize and write pid. */ if (opt_p == NULL) { if (asprintf(&opt_p, "%s.pid", opt_s) == -1) { warnp("asprintf"); exit(1); } } if (daemonize(opt_p)) { warnp("Failed to daemonize"); exit(1); } /* Handle connections, one at once. */ do { /* accept a connection. */ if ((D = dispatch_accept(QW, QR, opt_t, s)) == NULL) { warnp("Error accepting new connection"); exit(1); } /* Loop until the connection dies. */ do { if (events_run()) { warnp("Error running event loop"); exit(1); } } while (dispatch_alive(D)); /* Clean up the connection. */ if (dispatch_done(D)) exit(1); } while (opt_1 == 0); /* Close the log file, if we have one. */ if (logfile != NULL) logging_close(logfile); /* Close the listening socket. */ close(s); /* Free the address structures. */ sock_addr_freelist(sas); /* Stop performing DescribeTable requests. */ capacity_free(M); /* Free DynamoDB request queues. */ dynamodb_request_queue_free(QR); dynamodb_request_queue_free(QW); /* Stop DNS lookups. */ serverpool_free(SP); /* Shut down the event subsystem. */ events_shutdown(); /* Free string allocated by asprintf. */ free(dynamodb_host); /* Free key strings. */ free(key_id); insecure_memzero(key_secret, strlen(key_secret)); free(key_secret); /* Free option strings. */ free(opt_k); free(opt_l); free(opt_p); free(opt_r); free(opt_s); free(opt_t); /* Success! */ exit(0); }
int main(int argc, char *argv[]) { struct udev *udev; static const struct option options[] = { { "export", no_argument, NULL, 'x' }, { "debug", no_argument, NULL, 'd' }, { "help", no_argument, NULL, 'h' }, {} }; const char *node = NULL; int export = 0; int fd = -1; int rc = 0; udev = udev_new(); if (udev == NULL) goto exit; logging_init("cdrom_id"); udev_set_log_fn(udev, log_fn); while (1) { int option; option = getopt_long(argc, argv, "dxh", options, NULL); if (option == -1) break; switch (option) { case 'd': debug = 1; if (udev_get_log_priority(udev) < LOG_INFO) udev_set_log_priority(udev, LOG_INFO); break; case 'x': export = 1; break; case 'h': printf("Usage: cdrom_id [options] <device>\n" " --export export key/value pairs\n" " --debug debug to stderr\n" " --help print this help text\n\n"); goto exit; default: rc = 1; goto exit; } } node = argv[optind]; if (!node) { err(udev, "no device\n"); fprintf(stderr, "no device\n"); rc = 1; goto exit; } fd = open(node, O_RDONLY | O_NONBLOCK); if (fd < 0) { info(udev, "unable to open '%s'\n", node); rc = 1; goto exit; } info(udev, "probing: '%s'\n", node); /* same data as original cdrom_id */ if (cd_capability_compat(udev, fd) < 0) { rc = 1; goto exit; } /* check drive */ if (cd_inquiry(udev, fd) < 0) { rc = 2; goto exit; } /* read drive and possibly current profile */ if (cd_profiles(udev, fd) < 0) goto print; /* get session/track info */ if (cd_media_toc(udev, fd) < 0) goto print; /* get writable media state */ if (cd_media_info(udev, fd) < 0) goto print; print: printf("ID_CDROM=1\n"); if (cd_cd_rom) printf("ID_CDROM_CD=1\n"); if (cd_cd_r) printf("ID_CDROM_CD_R=1\n"); if (cd_cd_rw) printf("ID_CDROM_CD_RW=1\n"); if (cd_dvd_rom) printf("ID_CDROM_DVD=1\n"); if (cd_dvd_r) printf("ID_CDROM_DVD_R=1\n"); if (cd_dvd_rw) printf("ID_CDROM_DVD_RW=1\n"); if (cd_dvd_ram) printf("ID_CDROM_DVD_RAM=1\n"); if (cd_dvd_plus_r) printf("ID_CDROM_DVD_PLUS_R=1\n"); if (cd_dvd_plus_rw) printf("ID_CDROM_DVD_PLUS_RW=1\n"); if (cd_dvd_plus_r_dl) printf("ID_CDROM_DVD_PLUS_R_DL=1\n"); if (cd_dvd_plus_rw_dl) printf("ID_CDROM_DVD_PLUS_RW_DL=1\n"); if (cd_bd) printf("ID_CDROM_BD=1\n"); if (cd_bd_r) printf("ID_CDROM_BD_R=1\n"); if (cd_bd_re) printf("ID_CDROM_BD_RE=1\n"); if (cd_hddvd) printf("ID_CDROM_HDDVD=1\n"); if (cd_hddvd_r) printf("ID_CDROM_HDDVD_R=1\n"); if (cd_hddvd_rw) printf("ID_CDROM_HDDVD_RW=1\n"); if (cd_mo) printf("ID_CDROM_MO=1\n"); if (cd_mrw) printf("ID_CDROM_MRW=1\n"); if (cd_mrw_w) printf("ID_CDROM_MRW_W=1\n"); if (cd_media_mo) printf("ID_CDROM_MEDIA_MO=1\n"); if (cd_media_mrw) printf("ID_CDROM_MEDIA_MRW=1\n"); if (cd_media_mrw_w) printf("ID_CDROM_MEDIA_MRW_W=1\n"); if (cd_media_cd_rom) printf("ID_CDROM_MEDIA_CD=1\n"); if (cd_media_cd_r) printf("ID_CDROM_MEDIA_CD_R=1\n"); if (cd_media_cd_rw) printf("ID_CDROM_MEDIA_CD_RW=1\n"); if (cd_media_dvd_rom) printf("ID_CDROM_MEDIA_DVD=1\n"); if (cd_media_dvd_r) printf("ID_CDROM_MEDIA_DVD_R=1\n"); if (cd_media_dvd_ram) printf("ID_CDROM_MEDIA_DVD_RAM=1\n"); if (cd_media_dvd_rw) printf("ID_CDROM_MEDIA_DVD_RW=1\n"); if (cd_media_dvd_plus_r) printf("ID_CDROM_MEDIA_DVD_PLUS_R=1\n"); if (cd_media_dvd_plus_rw) printf("ID_CDROM_MEDIA_DVD_PLUS_RW=1\n"); if (cd_media_dvd_plus_rw_dl) printf("ID_CDROM_MEDIA_DVD_PLUS_RW_DL=1\n"); if (cd_media_dvd_plus_r_dl) printf("ID_CDROM_MEDIA_DVD_PLUS_R_DL=1\n"); if (cd_media_bd) printf("ID_CDROM_MEDIA_BD=1\n"); if (cd_media_bd_r) printf("ID_CDROM_MEDIA_BD_R=1\n"); if (cd_media_bd_re) printf("ID_CDROM_MEDIA_BD_RE=1\n"); if (cd_media_hddvd) printf("ID_CDROM_MEDIA_HDDVD=1\n"); if (cd_media_hddvd_r) printf("ID_CDROM_MEDIA_HDDVD_R=1\n"); if (cd_media_hddvd_rw) printf("ID_CDROM_MEDIA_HDDVD_RW=1\n"); if (cd_media_state != NULL) printf("ID_CDROM_MEDIA_STATE=%s\n", cd_media_state); if (cd_media_session_next > 0) printf("ID_CDROM_MEDIA_SESSION_NEXT=%d\n", cd_media_session_next); if (cd_media_session_count > 0) printf("ID_CDROM_MEDIA_SESSION_COUNT=%d\n", cd_media_session_count); if (cd_media_track_count > 0) printf("ID_CDROM_MEDIA_TRACK_COUNT=%d\n", cd_media_track_count); if (cd_media_track_count_audio > 0) printf("ID_CDROM_MEDIA_TRACK_COUNT_AUDIO=%d\n", cd_media_track_count_audio); if (cd_media_track_count_data > 0) printf("ID_CDROM_MEDIA_TRACK_COUNT_DATA=%d\n", cd_media_track_count_data); if (cd_media_session_last_offset > 0) printf("ID_CDROM_MEDIA_SESSION_LAST_OFFSET=%llu\n", cd_media_session_last_offset); exit: if (fd >= 0) close(fd); udev_unref(udev); logging_close(); return rc; }
int main(int argc, char **argv) { struct timespec mtime = { 0 }; sigset_t mask, sigmask_orig; int c, fd; int ep_timeout = 0; int ignore_timer = 0; int new_events = 0; char *eventdir, *prog, **prog_args; struct option long_options[] = { { "help", no_argument, 0, 'h' }, { "version", no_argument, 0, 'V' }, { "foreground", no_argument, 0, 'f' }, { "loglevel", required_argument, 0, 'L' }, { "logfile", required_argument, 0, 'l' }, { "pidfile", required_argument, 0, 'p' }, { "timeout", required_argument, 0, 't' }, { 0, 0, 0, 0 } }; while ((c = getopt_long(argc, argv, "hVfL:l:p:", long_options, NULL)) != -1) { switch (c) { case 't': timeout = atoi(optarg); if (!timeout) timeout = DEFAULT_TIMEOUT; break; case 'p': pidfile = optarg; break; case 'l': logfile = optarg; break; case 'L': log_priority = logging_level(optarg); break; case 'f': daemonize = 0; break; case 'V': printf("%s %s\n", PROGRAM_NAME, VERSION); return EXIT_SUCCESS; default: case 'h': printf("Usage: %s [options] DIRECTORY PROGRAM [ARGS...]\n" "\nThe utility monitors the DIRECTORY and when\n" "new files appear run the PROGRAM.\n\n" "Options:\n" " -p, --pidfile=FILE pid file location;\n" " -l, --logfile=FILE log file;\n" " -L, --loglevel=LVL logging level;\n" " -t, --timeout=SEC number of seconds that need to wait" " for files before the PROGRAM launch;\n" " -f, --foreground stay in the foreground;\n" " -V, --version print program version and exit;\n" " -h, --help show this text and exit.\n" "\n", PROGRAM_NAME); return EXIT_SUCCESS; } } if (optind >= argc) error(EXIT_FAILURE, 0, "You must specify the directory"); eventdir = argv[optind++]; if (optind >= argc) error(EXIT_FAILURE, 0, "You must specify the program"); prog = canonicalize_file_name(argv[optind]); if (!prog) error(EXIT_FAILURE, errno, "Bad program"); argv[optind] = strrchr(prog, '/'); if (!argv[optind]) argv[optind] = prog; prog_args = argv + optind; if (!log_priority) log_priority = logging_level("info"); if (pidfile && check_pid(pidfile)) error(EXIT_FAILURE, 0, "%s: already running", PROGRAM_NAME); if (chdir("/") < 0) error(EXIT_FAILURE, errno, "%s: chdir(/)", PROGRAM_NAME); close(STDIN_FILENO); if ((fd = open("/dev/null", O_RDONLY)) < 0) error(EXIT_FAILURE, errno, "%s: open(/dev/null)", PROGRAM_NAME); if (fd != STDIN_FILENO) { dup2(fd, STDIN_FILENO); close(fd); } if (daemonize && daemon(0, 1) < 0) error(EXIT_FAILURE, errno, "%s: daemon", PROGRAM_NAME); logging_init(); info("starting version %s", VERSION); if (pidfile && write_pid(pidfile) == 0) return EXIT_FAILURE; sigfillset(&mask); sigprocmask(SIG_SETMASK, &mask, &sigmask_orig); sigdelset(&mask, SIGABRT); sigdelset(&mask, SIGSEGV); if ((fd_ep = epoll_create1(EPOLL_CLOEXEC)) < 0) fatal("epoll_create1: %m"); if ((fd_signal = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC)) < 0) fatal("signalfd: %m"); epollin_add(fd_ep, fd_signal); if ((fd_eventdir = inotify_init1(IN_NONBLOCK | IN_CLOEXEC)) < 0) fatal("inotify_init1: %m"); if (inotify_add_watch(fd_eventdir, eventdir, IN_ONLYDIR | IN_DONT_FOLLOW | IN_MOVED_TO | IN_CLOSE_WRITE) < 0) fatal("inotify_add_watch: %m"); epollin_add(fd_ep, fd_eventdir); ignore_timer = is_dir_not_empty(eventdir); if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) fatal("clock_gettime: %m"); last.tv_sec = now.tv_sec; while (!do_exit) { struct epoll_event ev[42]; int i, fdcount; ssize_t size; if ((fdcount = epoll_wait(fd_ep, ev, ARRAY_SIZE(ev), ep_timeout)) < 0) continue; if (!ep_timeout) ep_timeout = timeout * 1000; for (i = 0; i < fdcount; i++) { if (!(ev[i].events & EPOLLIN)) { continue; } else if (ev[i].data.fd == fd_signal) { struct signalfd_siginfo fdsi; size = TEMP_FAILURE_RETRY(read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo))); if (size != sizeof(struct signalfd_siginfo)) { err("unable to read signal info"); continue; } handle_signal(fdsi.ssi_signo); } else if (ev[i].data.fd == fd_eventdir) { read_inotify_events(fd_eventdir); new_events += 1; } } if (new_events) { struct stat sb; new_events = 0; if (lstat(eventdir, &sb) < 0) fatal("lstat: %s: %m", eventdir); if (mtime.tv_sec != sb.st_mtim.tv_sec || mtime.tv_nsec != sb.st_mtim.tv_nsec) { if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) fatal("clock_gettime: %m"); last.tv_sec = now.tv_sec; } mtime.tv_sec = sb.st_mtim.tv_sec; mtime.tv_nsec = sb.st_mtim.tv_nsec; } if (worker_pid) continue; if (!ignore_timer) { if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) fatal("clock_gettime: %m"); if (now.tv_sec < last.tv_sec || (now.tv_sec - last.tv_sec) < timeout) continue; } ignore_timer = 0; if ((worker_pid = spawn_worker(prog, prog_args)) < 0) fatal("spawn_worker: %m"); dbg("Run worker %d", worker_pid); } epollin_remove(fd_ep, fd_signal); epollin_remove(fd_ep, fd_eventdir); free(prog); if (pidfile) remove_pid(pidfile); logging_close(); return EXIT_SUCCESS; }
int main(int argc, char *argv[], char *envp[]) { char queuename[PATH_SIZE]; char filename[PATH_SIZE]; unsigned long long seq_kernel; unsigned long long seq_udev; char seqnum[32]; int fd; ssize_t len; int timeout = DEFAULT_TIMEOUT; int loop; static const struct option options[] = { { "timeout", 1, NULL, 't' }, { "help", 0, NULL, 'h' }, {} }; int option; int rc = 1; int seconds; logging_init("udevsettle"); udev_config_init(); dbg("version %s", UDEV_VERSION); sysfs_init(); while (1) { option = getopt_long(argc, argv, "t:h", options, NULL); if (option == -1) break; switch (option) { case 't': seconds = atoi(optarg); if (seconds > 0) timeout = seconds; else fprintf(stderr, "invalid timeout value\n"); dbg("timeout=%i", timeout); break; case 'h': printf("Usage: udevsettle [--help] [--timeout=<seconds>]\n\n"); goto exit; } } strlcpy(queuename, udev_root, sizeof(queuename)); strlcat(queuename, "/" EVENT_QUEUE_DIR, sizeof(queuename)); loop = timeout * LOOP_PER_SECOND; while (loop--) { /* wait for events in queue to finish */ while (loop--) { struct stat statbuf; if (stat(queuename, &statbuf) < 0) { info("queue is empty"); break; } usleep(1000 * 1000 / LOOP_PER_SECOND); } if (loop <= 0) { info("timeout waiting for queue"); goto exit; } /* read current udev seqnum */ strlcpy(filename, udev_root, sizeof(filename)); strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename)); fd = open(filename, O_RDONLY); if (fd < 0) goto exit; len = read(fd, seqnum, sizeof(seqnum)-1); close(fd); if (len <= 0) goto exit; seqnum[len] = '\0'; seq_udev = strtoull(seqnum, NULL, 10); info("udev seqnum = %llu", seq_udev); /* read current kernel seqnum */ strlcpy(filename, sysfs_path, sizeof(filename)); strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename)); fd = open(filename, O_RDONLY); if (fd < 0) goto exit; len = read(fd, seqnum, sizeof(seqnum)-1); close(fd); if (len <= 0) goto exit; seqnum[len] = '\0'; seq_kernel = strtoull(seqnum, NULL, 10); info("kernel seqnum = %llu", seq_kernel); /* make sure all kernel events have arrived in the queue */ if (seq_udev >= seq_kernel) { info("queue is empty and no pending events left"); rc = 0; goto exit; } usleep(1000 * 1000 / LOOP_PER_SECOND); info("queue is empty, but events still pending"); } exit: sysfs_cleanup(); logging_close(); return rc; }