Example #1
0
int main(int argc, char *argv[])
{
    auto desc = create_descriptions();
    auto vm = parse_options(argc, argv, desc);
    if(vm.count("help"))
    {
        std::cout << desc << std::endl;
        return 1;
    }

    QApplication a{argc, argv};

    fg::main_window_context c;
        
    c.home = vm["home"].as<std::string>();
    c.host = vm["host"].as<std::string>();
    c.port = vm["port"].as<std::string>();

    if(!user_setup(c.home)) return 0;

    fg::main_window w{c};

    w.show();

    return a.exec();
}
Example #2
0
void user_init(void)
{
    // Setup HW
    user_setup();

    // Just some infomations
    printf("\n");
    printf("SDK version : %s\n", sdk_system_get_sdk_version());
    printf("GIT version : %s\n", GITSHORTREV);

    // Use our user inform implementation
    bmp180_informUser = bmp180_i2c_informUser;

    // Init BMP180 Interface
    bmp180_init(SCL_PIN, SDA_PIN);

    // Create Main Communication Queue
    mainqueue = xQueueCreate(10, sizeof(my_event_t));

    // Create user interface task
    xTaskCreate(bmp180_task, (const char * const)"bmp180_task", 256, &mainqueue, 2, NULL);

    // Create Timer (Trigger a measurement every second)
    timerHandle = xTimerCreate((const char * const)"BMP180 Trigger", 1000/portTICK_RATE_MS, pdTRUE, NULL, bmp180_i2c_timer_cb);

    if (timerHandle != NULL)
    {
        if (xTimerStart(timerHandle, 0) != pdPASS)
        {
            printf("%s: Unable to start Timer ...\n", __FUNCTION__);
        }
    }
    else
    {
        printf("%s: Unable to create Timer ...\n", __FUNCTION__);
    }
}
Example #3
0
int main(int argc, char **argv) {
  char *options, *path;
  int fd, inotify, option, pwd, waitargs;
  pid_t pid;
  time_t started;
  struct sigaction action;

  progname = argv[0];

  /* Redirect stdin from /dev/null. */
  if ((fd = open("/dev/null", O_RDWR)) < 0)
    error(EXIT_FAILURE, errno, "open /dev/null");
  if (fd != STDIN_FILENO)
    if ((dup2(fd, STDIN_FILENO)) < 0)
      error(EXIT_FAILURE, errno, "dup2");

  /* Redirect stdout and/or stderr to /dev/null if closed. */
  while (fd <= STDERR_FILENO)
    if ((fd = dup(fd)) < 0)
      error(EXIT_FAILURE, errno, "dup");
  close(fd);

  /* Close all file descriptors apart from stdin, stdout and stderr. */
  fd = getdtablesize() - 1;
  while (fd > STDERR_FILENO)
    close(fd--);

  options = "+:cfl:p:ru:w:", waitargs = 0;
  while ((option = getopt(argc, argv, options)) > 0)
    switch (option) {
      case 'c':
        command.chdir = 1;
        break;
      case 'f':
        /* On by default; ignored for compatibility with BSD daemon(1). */
        break;
      case 'l':
        logger_setup(optarg);
        break;
      case 'p':
        pidfile_open(optarg);
        break;
      case 'r':
        command.restart = 1;
        break;
      case 'u':
        user_setup(optarg);
        break;
      case 'w':
        waitargs++;
        break;
      default:
        usage(argv[0]);
    }

  if (argc <= optind)
    usage(argv[0]);

  switch (fork()) {
    case -1:
      error(EXIT_FAILURE, errno, "fork");
    case 0:
      setsid(); /* This should work after forking; ignore errors anyway. */
      break;
    default:
      _exit(EXIT_SUCCESS); /* Don't delete pidfile in atexit() handler. */
  }

  logger_start();
  pidfile_write();

  /* We can handle all -w command line arguments now we're daemonized. */
  if (waitargs > 0) {
    if ((inotify = inotify_init1(IN_CLOEXEC)) < 0)
      error(EXIT_FAILURE, errno, "inotify_init1");

    /* Open the working directory so we can restore it after each await(). */
    if ((pwd = open(".", O_RDONLY | O_DIRECTORY)) < 0)
      error(EXIT_FAILURE, errno, "open pwd");

    optind = 0; /* Need to reset optind to reprocess arguments. */
    while ((option = getopt(argc, argv, options)) > 0)
      if (option == 'w') {
        if (!(path = strdup(optarg)))
          error(EXIT_FAILURE, errno, "strdup");
        await(path, inotify, 0);
        free(path);
        fchdir(pwd);
      }

    close(inotify);
    close(pwd);
  }

  if (command.chdir && chdir("/") < 0)
    error(EXIT_FAILURE, errno, "chdir");

  command.argv = argv + optind;
  if (!command.restart && !pidfile.path) {
    /* We don't need to supervise in this case, so just exec. */
    if (command.gid > 0 && setgid(command.gid) < 0)
      error(EXIT_FAILURE, errno, "setgid");
    if (command.uid > 0 && setuid(command.uid) < 0)
      error(EXIT_FAILURE, errno, "setuid");
    execvp(command.argv[0], command.argv);
    error(EXIT_FAILURE, errno, "exec");
  }

  /* Handle and pass on HUP, INT, TERM, USR1, USR2 signals. */
  sigfillset(&action.sa_mask);
  action.sa_flags = SA_RESTART;
  action.sa_handler = handler;
  sigaction(SIGHUP, &action, NULL);
  sigaction(SIGINT, &action, NULL);
  sigaction(SIGTERM, &action, NULL);
  sigaction(SIGUSR1, &action, NULL);
  sigaction(SIGUSR2, &action, NULL);

  do {
    command.killed = 0; /* Have we signalled the child? */
    switch (command.pid = fork()) {
      case -1:
        error(EXIT_FAILURE, errno, "fork");
      case 0:
        if (command.gid > 0 && setgid(command.gid) < 0)
          error(EXIT_FAILURE, errno, "setgid");
        if (command.uid > 0 && setuid(command.uid) < 0)
          error(EXIT_FAILURE, errno, "setuid");
        setsid(); /* This should work after forking; ignore errors anyway. */
        execvp(command.argv[0], command.argv);
        error(EXIT_FAILURE, errno, "exec");
    }

    started = time(NULL);
    while (pid = wait(NULL), pid != (pid_t) command.pid)
      if (pid < 0 && errno != EINTR)
        error(EXIT_FAILURE, errno, "wait");

    /* Try to avoid restarting a crashing command in a tight loop. */
    if (command.restart && !command.killed && time(NULL) < started + 5)
      error(EXIT_FAILURE, 0, "Child died within 5 seconds: not restarting");
  } while (command.restart);

  return EXIT_SUCCESS;
}