void start_cli() { SWSS_LOG_ENTER(); SWSS_LOG_NOTICE("starting cli"); if (run_cli) { stop_cli(); } run_cli = true; cli_thread = std::make_shared<std::thread>(std::thread(cli_thread_function)); }
int main(int argc, char **argv) { int i; int fanotify_fd, ret; struct backend *be = NULL; struct stat stbuf; logfd = stdout; memset(frontend_prefix, 0x0, FILENAME_MAX); while ((i = getopt(argc, argv, "b:c:d:m:n:o:p:su:")) != -1) { switch (i) { case 'b': be = new_backend(optarg); if (!be) { err("Invalid backend '%s'\n", optarg); return EINVAL; } break; case 'd': if (stat(optarg, &stbuf) < 0 || !S_ISDIR(stbuf.st_mode)) { err("Frontend prefix %s is not a directory", optarg); return EINVAL; } strncpy(frontend_prefix, optarg, FILENAME_MAX); break; case 'c': return cli_command(CLI_CHECK, optarg); break; case 'm': ret = cli_command(CLI_CHECK, optarg); if (ret) return ret; ret = cli_command(CLI_MIGRATE, optarg); if (ret) return ret; /* Fallthrough */ case 'n': return cli_command(CLI_MONITOR, optarg); break; case 'o': if (!be) { err("No backend selected"); return EINVAL; } if (parse_backend_options(be, optarg) < 0) { err("Invalid backend option '%s'", optarg); return EINVAL; } break; case 'p': log_priority = strtoul(optarg, NULL, 10); if (log_priority > LOG_DEBUG) { err("Invalid logging priority %d (max %d)", log_priority, LOG_DEBUG); exit(1); } break; case 's': return cli_command(CLI_SHUTDOWN, NULL); break; case 'u': ret = cli_command(CLI_CHECK, optarg); if (ret && ret != ENOENT) return ret; ret = cli_command(CLI_SETUP, optarg); if (ret) return ret; return cli_command(CLI_MONITOR, optarg); break; default: fprintf(stderr, "usage: %s [-d <dir>]\n", argv[0]); return EINVAL; } } if (optind < argc) { fprintf(stderr, "usage: %s [-b file] [-p <dir>]\n", argv[0]); return EINVAL; } signal_set(SIGINT, sigend); signal_set(SIGTERM, sigend); fanotify_fd = fanotify_init(FAN_CLASS_PRE_CONTENT, O_RDWR); if (fanotify_fd < 0) { fprintf(stderr, "cannot start fanotify, error %d\n", errno); return errno; } daemon_thr = pthread_self(); watcher_thr = start_watcher(be, fanotify_fd); if (!watcher_thr) return errno; cli_thr = start_cli(be, fanotify_fd); if (!cli_thr) { stop_watcher(watcher_thr); return ENOMEM; } pthread_cond_wait(&exit_cond, &exit_mutex); stop_cli(cli_thr); stop_watcher(watcher_thr); return 0; }