コード例 #1
0
ファイル: aa-ctty.c プロジェクト: linsam/anopa
int
main (int argc, char * const argv[], char const * const *envp)
{
    PROG = "aa-ctty";
    int fd = 0;
    int steal = 0;

    for (;;)
    {
        struct option longopts[] = {
            { "double-output",      no_argument,        NULL,   'D' },
            { "fd",                 required_argument,  NULL,   'f' },
            { "help",               no_argument,        NULL,   'h' },
            { "steal",              no_argument,        NULL,   's' },
            { "version",            no_argument,        NULL,   'V' },
            { NULL, 0, 0, 0 }
        };
        int c;

        c = getopt_long (argc, argv, "+Df:hsV", longopts, NULL);
        if (c == -1)
            break;
        switch (c)
        {
            case 'D':
                aa_set_double_output (1);
                break;

            case 'f':
                if (!uint0_scan (optarg, &fd))
                    aa_strerr_diefu1sys (1, "set fd");
                break;

            case 'h':
                dieusage (0);

            case 's':
                steal = 1;
                break;

            case 'V':
                aa_die_version ();

            default:
                dieusage (1);
        }
    }
    argc -= optind;
    argv += optind;

    if (argc == 0)
        dieusage (1);

    if (ioctl (fd, TIOCSCTTY, steal) < 0)
        aa_strerr_warnu1sys ("set controlling terminal");

    pathexec_run (argv[0], (char const * const *) argv, envp);
    aa_strerr_dieexec (111, argv[0]);
}
コード例 #2
0
ファイル: aa-sync.c プロジェクト: jjk-jacky/anopa
int
main (int argc, char * const argv[])
{
    PROG = "aa-sync";

    if (argc == 1)
    {
        sync ();
        return 0;
    }

    if (argc == 2 && (str_equal (argv[1], "-V") || str_equal (argv[1], "--version")))
        aa_die_version ();
    dieusage ((argc == 2 && (str_equal (argv[1], "-h") || str_equal (argv[1], "--help"))) ? 0 : 1);
}
コード例 #3
0
ファイル: aa-terminate.c プロジェクト: jjk-jacky/anopa
int
main (int argc, char * const argv[])
{
    PROG = "aa-terminate";
    stralloc sa = STRALLOC_ZERO;
    stralloc sa_swaps = STRALLOC_ZERO;
    stralloc sa_mounts = STRALLOC_ZERO;
    stralloc sa_loops = STRALLOC_ZERO;
    stralloc sa_dms = STRALLOC_ZERO;
    int apis = 0;
    int lazy = 0;
    int did_smthg;

    for (;;)
    {
        struct option longopts[] = {
            { "apis",               no_argument,        NULL,   'a' },
            { "double-output",      no_argument,        NULL,   'D' },
            { "help",               no_argument,        NULL,   'h' },
            { "lazy-umounts",       no_argument,        NULL,   'l' },
            { "quiet",              no_argument,        NULL,   'q' },
            { "version",            no_argument,        NULL,   'V' },
            { "verbose",            no_argument,        NULL,   'v' },
            { NULL, 0, 0, 0 }
        };
        int c;

        c = getopt_long (argc, argv, "aDhlqVv", longopts, NULL);
        if (c == -1)
            break;
        switch (c)
        {
            case 'a':
                apis = 1;
                break;

            case 'D':
                aa_set_double_output (1);
                break;

            case 'h':
                dieusage (0);

            case 'l':
                lazy = 1;
                break;

            case 'q':
                level = 0;
                break;

            case 'V':
                aa_die_version ();

            case 'v':
                level = 2;
                break;

            default:
                dieusage (1);
        }
    }
    argc -= optind;
    argv += optind;

    if (argc > 0)
        dieusage (1);

again:
    for (;;)
    {
        /* re-init */
        sa_swaps.len = 0;
        sa_mounts.len = 0;
        sa_loops.len = 0;
        sa_dms.len = 0;

        /* read swaps */
        if (!openslurpclose (&sa, "/proc/swaps"))
            aa_strerr_diefu1sys (2, "read /proc/swaps");

        if (sa.len > 0)
        {
            size_t l;

            l = byte_chr (sa.s, sa.len, '\n') + 1;
            for ( ; l < sa.len; )
            {
                size_t e;

                /* FIXME: how are spaces-in-filename treated? */
                e = byte_chr (sa.s + l, sa.len - l, ' ');
                if (e < sa.len - l)
                {
                    if (!stralloc_catb (&sa_swaps, sa.s + l, e)
                            || !stralloc_0 (&sa_swaps))
                        aa_strerr_diefu1sys (2, "stralloc_catb");
                }
                l += byte_chr (sa.s + l, sa.len - l, '\n') + 1;
            }
            sa.len = 0;
        }


        /* read mounts */
        {
            FILE *mounts;
            struct mntent *mnt;

            mounts = setmntent ("/proc/mounts", "r");
            if (!mounts)
                aa_strerr_diefu1sys (2, "read /proc/mounts");

            while ((mnt = getmntent (mounts)))
            {
                if (str_equal (mnt->mnt_dir, "/")
                        || str_equal (mnt->mnt_dir, "/dev")
                        || str_equal (mnt->mnt_dir, "/proc")
                        || str_equal (mnt->mnt_dir, "/sys")
                        || str_equal (mnt->mnt_dir, "/run"))
                    continue;

                if (!stralloc_catb (&sa_mounts, mnt->mnt_dir, strlen (mnt->mnt_dir) + 1))
                    aa_strerr_diefu1sys (2, "stralloc_catb");
            }
            endmntent (mounts);
        }


        /* read loops + dms */
        {
            stralloc *sas[2] = { &sa_loops, &sa_dms };
            int r;

            stralloc_catb (&sa, "/dev", 5);
            r = aa_scan_dir (&sa, 2, it_loops_dms, &sas);
            if (r < 0)
                aa_strerr_diefu1sys (2, "scan /dev");
            sa.len = 0;
        }

        did_smthg = 0;

        if (do_work (&sa_swaps, do_swapoff))
            did_smthg = 1;
        if (do_work (&sa_mounts, do_umount))
            did_smthg = 1;
        if (do_work (&sa_loops, do_loop_close))
            did_smthg = 1;
        if (do_work (&sa_dms, do_dm_close))
            did_smthg = 1;

        if (!did_smthg)
            break;
    }

    if (lazy && umnt_flags == 0 && sa_mounts.len > 0)
    {
        verbose_do ("Switching to lazy umount mode", "");
        umnt_flags = MNT_DETACH;
        goto again;
    }

    if (apis)
    {
        umount_api ("/run");
        umount_api ("/sys");
        umount_api ("/proc");
        umount_api ("/dev");
    }

    if (level > 0)
    {
        show_left ("Remaining swap", &sa_swaps);
        show_left ("Remaining mountpoint", &sa_mounts);
        show_left ("Remaining loop device", &sa_loops);
        show_left ("Remaining block device", &sa_dms);
    }

    return (sa_swaps.len + sa_mounts.len + sa_loops.len + sa_dms.len == 0) ? 0 : 2;
}
コード例 #4
0
ファイル: aa-reset.c プロジェクト: linsam/anopa
int
main (int argc, char * const argv[])
{
    PROG = "aa-reset";
    const char *path_repo = "/run/services";
    intptr_t mode = MODE_NONE;
    int i;
    int r;

    for (;;)
    {
        struct option longopts[] = {
            { "auto",               no_argument,        NULL,   'A' },
            { "started",            no_argument,        NULL,   'a' },
            { "double-output",      no_argument,        NULL,   'D' },
            { "help",               no_argument,        NULL,   'h' },
            { "stopped",            no_argument,        NULL,   'o' },
            { "repodir",            required_argument,  NULL,   'r' },
            { "version",            no_argument,        NULL,   'V' },
            { NULL, 0, 0, 0 }
        };
        int c;

        c = getopt_long (argc, argv, "AaDhor:V", longopts, NULL);
        if (c == -1)
            break;
        switch (c)
        {
            case 'A':
                mode = MODE_AUTO;
                break;

            case 'a':
                mode = MODE_STARTED;
                break;

            case 'D':
                aa_set_double_output (1);
                break;

            case 'h':
                dieusage (0);

            case 'o':
                mode = MODE_STOPPED;
                break;

            case 'r':
                unslash (optarg);
                path_repo = optarg;
                break;

            case 'V':
                aa_die_version ();

            default:
                dieusage (1);
        }
    }
    argc -= optind;
    argv += optind;

    if (argc < 1 || mode == MODE_NONE)
        dieusage (1);

    r = aa_init_repo (path_repo, AA_REPO_READ);
    if (r < 0)
        aa_strerr_diefu2sys (2, "init repository ", path_repo);

    for (i = 0; i < argc; ++i)
        if (str_equal (argv[i], "-"))
        {
            if (process_names_from_stdin ((names_cb) reset_service, (void *) mode) < 0)
                aa_strerr_diefu1sys (ERR_IO, "process names from stdin");
        }
        else
            reset_service (argv[i], mode);

    return 0;
}
コード例 #5
0
ファイル: aa-kill.c プロジェクト: linsam/anopa
int
main (int argc, char * const argv[])
{
    PROG = "aa-kill";

    for (;;)
    {
        struct option longopts[] = {
            { "double-output",      no_argument,        NULL,   'D' },
            { "help",               no_argument,        NULL,   'h' },
            { "kill",               no_argument,        NULL,   'k' },
            { "skip-at",            no_argument,        NULL,   's' },
            { "term",               no_argument,        NULL,   't' },
            { "hup",                no_argument,        NULL,   'u' },
            { "version",            no_argument,        NULL,   'V' },
            { NULL, 0, 0, 0 }
        };
        int c;

        c = getopt_long (argc, argv, "DhkstuV", longopts, NULL);
        if (c == -1)
            break;
        switch (c)
        {
            case 'D':
                aa_set_double_output (1);
                break;

            case 'h':
                dieusage (0);

            case 'k':
                send.kill = 1;
                break;

            case 's':
                send.skip_at = 1;
                break;

            case 't':
                send.term = 1;
                break;

            case 'u':
                send.hup = 1;
                break;

            case 'V':
                aa_die_version ();

            default:
                dieusage (1);
        }
    }
    argc -= optind;
    argv += optind;

    if (argc > 0 || (!send.hup && !send.term && !send.kill))
        dieusage (1);

    if (send.skip_at)
    {
        stralloc sa = STRALLOC_ZERO;
        unsigned int u;

        u = (unsigned int) getpid ();
        ownpid[uint_fmt (ownpid, u)] = '\0';

        if (!stralloc_catb (&sa, "/proc", sizeof ("/proc")))
            aa_strerr_diefu1sys (1, "stralloc_catb");
        if (aa_scan_dir (&sa, 0, it_kill, &sa) < 0)
            aa_strerr_diefu1sys (1, "scan /proc");
        stralloc_free (&sa);
    }
    else
    {
        if (send.hup)
        {
            sig_ignore (SIGHUP);
            _kill (-1, SIGHUP);
        }

        if (send.term)
        {
            sig_ignore (SIGTERM);
            _kill (-1, SIGTERM);
            _kill (-1, SIGCONT);
        }

        if (send.kill)
            _kill (-1, SIGKILL);
    }

    return 0;
}
コード例 #6
0
ファイル: aa-start.c プロジェクト: jjk-jacky/anopa
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;
}