static void warn_cb (const char *name, int err) { aa_put_warn (cur_name, name, 0); aa_bs_noflush (AA_ERR, ": "); aa_bs_noflush (AA_ERR, error_str (err)); aa_end_warn (); }
static void verbose_fail (int e) { if (level < 2) return; aa_bs_noflush (AA_OUT, "Failed: "); aa_bs_noflush (AA_OUT, strerror (e)); aa_bs_flush (AA_OUT, "\n"); }
static void verbose_do (const char *s1, const char *s2) { if (level < 2) return; aa_bs_noflush (AA_OUT, s1); aa_bs_noflush (AA_OUT, s2); aa_bs_flush (AA_OUT, "...\n"); }
static void _kill (pid_t pid, int sig) { char buf[UINT_FMT]; unsigned int u; u = pid; buf[uint_fmt (buf, u)] = 0; aa_bs_noflush (AA_OUT, "kill("); if (u == (unsigned int) -1) aa_bs_noflush (AA_OUT, "-1"); else aa_bs_noflush (AA_OUT, buf); aa_bs_noflush (AA_OUT, ","); aa_bs_noflush (AA_OUT, sig_name (sig)); aa_bs_flush (AA_OUT, ")\n"); }
static void autoload_cb (int si, aa_al al, const char *name, int err) { if (verbose) { int aa = (mode & AA_MODE_IS_DRY) ? AA_ERR : AA_OUT; aa_bs_noflush (aa, "auto-add: "); aa_bs_noflush (aa, aa_service_name (aa_service (si))); aa_bs_noflush (aa, (al == AA_AUTOLOAD_NEEDS) ? " needs " : " wants "); aa_bs_noflush (aa, name); aa_bs_flush (aa, "\n"); } if (al == AA_AUTOLOAD_WANTS && err > 0) { put_warn (aa_service_name (aa_service (si)), "Skipping wanted service ", 0); add_warn (name); add_warn (": "); add_warn (errmsg[err]); end_warn (); add_name_to_ga (name, &ga_skipped); } }
static void reset_service (const char *name, intptr_t mode) { aa_service *s; int si; int r; int old_event; int event; r = aa_get_service (name, &si, 1); if (r < 0) { aa_put_err (name, errmsg[-r], 1); return; } r = aa_preload_service (si); if (r < 0) { aa_put_err (name, errmsg[-r], 1); return; } s = aa_service (si); if (aa_service_status_read (&s->st, aa_service_name (s)) < 0 && errno != ENOENT) { int e = errno; aa_put_err (name, "Failed to read service status file: ", 0); aa_bs_noflush (AA_ERR, error_str (e)); aa_end_err (); return; } if (s->st.type == AA_TYPE_LONGRUN) { aa_put_err (name, "Can only reset ont-shot services", 1); return; } /* Starting/Stopping cannot be reset */ if (s->st.event == AA_EVT_STARTING || s->st.event == AA_EVT_STOPPING) return; if (mode == MODE_AUTO) { if (s->st.event == AA_EVT_STARTING_FAILED || s->st.event == AA_EVT_START_FAILED) event = AA_EVT_STARTED; else if (s->st.event == AA_EVT_STOPPING_FAILED || s->st.event == AA_EVT_STOP_FAILED) event = AA_EVT_STOPPED; else return; } else event = (mode == MODE_STARTED) ? AA_EVT_STARTED : AA_EVT_STOPPED; if (s->st.event == event) return; tain_now_g (); old_event = s->st.event; s->st.event = event; s->st.stamp = STAMP; aa_service_status_set_msg (&s->st, ""); if (aa_service_status_write (&s->st, aa_service_name (s)) < 0) { int e = errno; aa_put_err (name, "Failed to write service status file: ", 0); aa_bs_noflush (AA_ERR, error_str (e)); aa_end_err (); } else { aa_put_title (1, name, "", 0); aa_is_noflush (AA_OUT, ANSI_HIGHLIGHT_OFF); aa_bs_noflush (AA_OUT, eventmsg[old_event]); aa_is_noflush (AA_OUT, ANSI_HIGHLIGHT_ON); aa_bs_noflush (AA_OUT, " -> "); aa_bs_noflush (AA_OUT, eventmsg[event]); aa_end_title (); } }
int main (int argc, char * const argv[]) { PROG = "aa-start"; const char *path_repo = "/run/services"; const char *path_list = NULL; int i; aa_secs_timeout = DEFAULT_TIMEOUT_SECS; for (;;) { struct option longopts[] = { { "double-output", no_argument, NULL, 'D' }, { "help", no_argument, NULL, 'h' }, { "listdir", required_argument, NULL, 'l' }, { "dry-list", no_argument, NULL, 'n' }, { "repodir", required_argument, NULL, 'r' }, { "timeout", required_argument, NULL, 't' }, { "version", no_argument, NULL, 'V' }, { "verbose", no_argument, NULL, 'v' }, { "no-wants", no_argument, NULL, 'W' }, { NULL, 0, 0, 0 } }; int c; c = getopt_long (argc, argv, "Dhl:nr:t:VvW", longopts, NULL); if (c == -1) break; switch (c) { case 'D': aa_set_double_output (1); break; case 'h': dieusage (0); case 'l': unslash (optarg); path_list = optarg; break; case 'n': if (mode & AA_MODE_IS_DRY) mode |= AA_MODE_IS_DRY_FULL; else mode |= AA_MODE_IS_DRY; break; case 'r': unslash (optarg); path_repo = optarg; break; case 't': if (!uint0_scan (optarg, &aa_secs_timeout)) aa_strerr_diefu2sys (ERR_IO, "set default timeout to ", optarg); break; case 'V': aa_die_version (); case 'v': verbose = 1; break; case 'W': no_wants = 1; break; default: dieusage (1); } } argc -= optind; argv += optind; cols = get_cols (1); is_utf8 = is_locale_utf8 (); if (!path_list && argc < 1) dieusage (1); if (aa_init_repo (path_repo, (mode & AA_MODE_IS_DRY) ? AA_REPO_READ : AA_REPO_WRITE) < 0) aa_strerr_diefu2sys (ERR_IO, "init repository ", path_repo); if (path_list) { stralloc sa = STRALLOC_ZERO; int r; if (*path_list != '/' && *path_list != '.') stralloc_cats (&sa, LISTDIR_PREFIX); stralloc_catb (&sa, path_list, strlen (path_list) + 1); r = aa_scan_dir (&sa, 1, it_start, NULL); stralloc_free (&sa); if (r < 0) aa_strerr_diefu3sys (-r, "read list directory ", (*path_list != '/' && *path_list != '.') ? LISTDIR_PREFIX : path_list, (*path_list != '/' && *path_list != '.') ? path_list : ""); } tain_now_g (); for (i = 0; i < argc; ++i) if (str_equal (argv[i], "-")) { if (process_names_from_stdin ((names_cb) add_service, NULL) < 0) aa_strerr_diefu1sys (ERR_IO, "process names from stdin"); } else add_service (argv[i], NULL); mainloop (mode, scan_cb); if (!(mode & AA_MODE_IS_DRY)) { aa_bs_noflush (AA_OUT, "\n"); put_title (1, PROG, "Completed.", 1); aa_show_stat_nb (nb_already, "Already up", ANSI_HIGHLIGHT_GREEN_ON); aa_show_stat_nb (nb_done, "Started", ANSI_HIGHLIGHT_GREEN_ON); show_stat_service_names (&ga_timedout, "Timed out", ANSI_HIGHLIGHT_RED_ON); show_stat_service_names (&ga_failed, "Failed", ANSI_HIGHLIGHT_RED_ON); show_stat_service_names (&ga_depend, "Dependency failed", ANSI_HIGHLIGHT_RED_ON); aa_show_stat_names (aa_names.s, &ga_io, "I/O error", ANSI_HIGHLIGHT_RED_ON); aa_show_stat_names (aa_names.s, &ga_unknown, "Unknown", ANSI_HIGHLIGHT_RED_ON); aa_show_stat_names (aa_names.s, &ga_skipped, "Skipped", ANSI_HIGHLIGHT_YELLOW_ON); } genalloc_free (int, &ga_timedout); genalloc_free (int, &ga_failed); genalloc_free (int, &ga_depend); genalloc_free (size_t, &ga_io); genalloc_free (size_t, &ga_unknown); genalloc_free (size_t, &ga_skipped); genalloc_free (pid_t, &ga_pid); genalloc_free (int, &aa_tmp_list); genalloc_free (int, &aa_main_list); stralloc_free (&aa_names); genalloc_deepfree (struct progress, &ga_progress, free_progress); aa_free_services (close_fd); genalloc_free (iopause_fd, &ga_iop); return rc; }