int
command_lskern(int argc, char *argv[])
{
    struct preloaded_file	*fp;
    char			lbuf[80];
    int				ch, verbose;

    verbose = 0;
    optind = 1;
    optreset = 1;

    pager_open();
    for (fp = preloaded_files; fp; fp = fp->f_next) {
	snprintf(lbuf, sizeof(lbuf), " %p: %s (%s, 0x%lx)\n", 
		(void *) fp->f_addr, fp->f_name, fp->f_type, (long) fp->f_size);
	pager_output(lbuf);
	if (fp->f_args != NULL) {
	    pager_output("    args: ");
	    pager_output(fp->f_args);
	    pager_output("\n");
	}
    }
    pager_close();
    return(CMD_OK);
}
Esempio n. 2
0
int main(int argc, char *argv[]) {
        sd_bus *bus = NULL;
        int r;

        setlocale(LC_ALL, "");
        log_parse_environment();
        log_open();

        r = parse_argv(argc, argv);
        if (r <= 0)
                goto finish;

        r = bus_connect_transport(arg_transport, arg_host, false, &bus);
        if (r < 0) {
                log_error_errno(r, "Failed to create bus connection: %m");
                goto finish;
        }

        r = timedatectl_main(bus, argc, argv);

finish:
        /* make sure we terminate the bus connection first, and then close the
         * pager, see issue #3543 for the details. */
        sd_bus_flush_close_unref(bus);
        pager_close();

        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
Esempio n. 3
0
static int
command_show(int argc, char *argv[])
{
    struct env_var	*ev;
    char		*cp;

    if (argc < 2) {
	/* 
	 * With no arguments, print everything.
	 */
	pager_open();
	for (ev = environ; ev != NULL; ev = ev->ev_next) {
	    pager_output(ev->ev_name);
	    cp = getenv(ev->ev_name);
	    if (cp != NULL) {
		pager_output("=");
		pager_output(cp);
	    }
	    if (pager_output("\n"))
		break;
	}
	pager_close();
    } else {
	if ((cp = getenv(argv[1])) != NULL) {
	    printf("%s\n", cp);
	} else {
	    sprintf(command_errbuf, "variable '%s' not found", argv[1]);
	    return(CMD_ERROR);
	}
    }
    return(CMD_OK);
}
Esempio n. 4
0
static int
command_more(int argc, char *argv[])
{
    int         i;
    int         res;
    char	line[80];

    res=0;
    pager_open();
    for (i = 1; (i < argc) && (res == 0); i++) {
	sprintf(line, "*** FILE %s BEGIN ***\n", argv[i]);
	if (pager_output(line))
		break;
        res = page_file(argv[i]);
	if (!res) {
	    sprintf(line, "*** FILE %s END ***\n", argv[i]);
	    res = pager_output(line);
	}
    }
    pager_close();

    if (res == 0)
	return CMD_OK;
    else
	return CMD_ERROR;
}
Esempio n. 5
0
static void
efipart_print(int verbose)
{
	char line[80];
	EFI_BLOCK_IO *blkio;
	EFI_HANDLE h;
	EFI_STATUS status;
	u_int unit;

	pager_open();
	for (unit = 0, h = efi_find_handle(&efipart_dev, 0);
	    h != NULL; h = efi_find_handle(&efipart_dev, ++unit)) {
		sprintf(line, "    %s%d:", efipart_dev.dv_name, unit);
		if (pager_output(line))
			break;

		status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
		if (!EFI_ERROR(status)) {
			sprintf(line, "    %llu blocks",
			    (unsigned long long)(blkio->Media->LastBlock + 1));
			if (pager_output(line))
				break;
			if (blkio->Media->RemovableMedia)
				if (pager_output(" (removable)"))
					break;
		}
		if (pager_output("\n"))
			break;
	}
	pager_close();
}
Esempio n. 6
0
int main(int argc, char *argv[]) {
        _cleanup_bus_close_unref_ sd_bus *bus = NULL;
        int r;

        setlocale(LC_ALL, "");
        log_parse_environment();
        log_open();

        r = parse_argv(argc, argv);
        if (r <= 0)
                goto finish;

        r = bus_open_transport(arg_transport, arg_host, false, &bus);
        if (r < 0) {
                log_error("Failed to create bus connection: %s", strerror(-r));
                goto finish;
        }

        r = timedatectl_main(bus, argc, argv);

finish:
        pager_close();

        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
Esempio n. 7
0
int
command_commandlist(int argc, char *argv[])
{
    struct bootblk_command	*cmdp;
    int		res;
    char	name[20];
    int i;

    res = 0;
    pager_open();
    res = pager_output("Available commands:\n");

    for (i = 0, cmdp = commands; (cmdp->c_name != NULL) && (cmdp->c_desc != NULL ); i++, cmdp = commands + i) {
	    if (res)
	    break;
	if ((cmdp->c_name != NULL) && (cmdp->c_desc != NULL)) {
	    sprintf(name, "  %s  ", cmdp->c_name);
	    pager_output(name);
	    pager_output(cmdp->c_desc);
	    res = pager_output("\n");
	}
    }
    pager_close();
    return(CMD_OK);
}
Esempio n. 8
0
static void
efinet_dev_print(int verbose)
{
	char line[80];
	EFI_HANDLE h;
	int unit;

	pager_open();
	for (unit = 0, h = efi_find_handle(&efinet_dev, 0);
	    h != NULL; h = efi_find_handle(&efinet_dev, ++unit)) {
		sprintf(line, "    %s%d:\n", efinet_dev.dv_name, unit);
		if (pager_output(line))
			break;
	}
	pager_close();
}
Esempio n. 9
0
int main(int argc, char *argv[]) {
        int r, retval = EXIT_FAILURE;
        DBusConnection *bus = NULL;
        DBusError error;

        dbus_error_init(&error);

        setlocale(LC_ALL, "");
        log_parse_environment();
        log_open();

        r = parse_argv(argc, argv);
        if (r < 0)
                goto finish;
        else if (r == 0) {
                retval = EXIT_SUCCESS;
                goto finish;
        }

        if (arg_transport == TRANSPORT_NORMAL)
                bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
        else if (arg_transport == TRANSPORT_POLKIT)
                bus_connect_system_polkit(&bus, &error);
        else if (arg_transport == TRANSPORT_SSH)
                bus_connect_system_ssh(NULL, arg_host, &bus, &error);
        else
                assert_not_reached("Uh, invalid transport...");

        r = localectl_main(bus, argc, argv, &error);
        retval = r < 0 ? EXIT_FAILURE : r;

finish:
        if (bus) {
                dbus_connection_flush(bus);
                dbus_connection_close(bus);
                dbus_connection_unref(bus);
        }

        dbus_error_free(&error);
        dbus_shutdown();

        pager_close();

        return retval;
}
Esempio n. 10
0
static int
command_commandlist(int argc, char *argv[])
{
    struct bootblk_command	**cmdp;
    char str[81];
    
    pager_open();
    printf("Available commands:\n");
    SET_FOREACH(cmdp, Xcommand_set) {
	if (((*cmdp)->c_name != NULL) && ((*cmdp)->c_desc != NULL)) {
	    snprintf(str, sizeof(str), "  %-15s  %s\n",
		(*cmdp)->c_name, (*cmdp)->c_desc);
	    pager_output(str);
	}
    }
    pager_close();
    return(CMD_OK);
}
Esempio n. 11
0
static int
command_lsmod(int argc, char *argv[])
{
    struct loaded_module	*am;
    struct module_metadata	*md;
    char			lbuf[80];
    int				ch, verbose;

    verbose = 0;
    optind = 1;
    optreset = 1;
    while ((ch = getopt(argc, argv, "v")) != -1) {
	switch(ch) {
	case 'v':
	    verbose = 1;
	    break;
	case '?':
	default:
	    /* getopt has already reported an error */
	    return(CMD_OK);
	}
    }

    pager_open();
    for (am = loaded_modules; (am != NULL); am = am->m_next) {
	sprintf(lbuf, " %p: %s (%s, 0x%lx)\n", 
		(void *) am->m_addr, am->m_name, am->m_type, (long) am->m_size);
	pager_output(lbuf);
	if (am->m_args != NULL) {
	    pager_output("    args: ");
	    pager_output(am->m_args);
	    pager_output("\n");
	}
	if (verbose)
	    /* XXX could add some formatting smarts here to display some better */
	    for (md = am->m_metadata; md != NULL; md = md->md_next) {
		sprintf(lbuf, "      0x%04x, 0x%lx\n", md->md_type, (long) md->md_size);
		pager_output(lbuf);
	    }
    }
    pager_close();
    return(CMD_OK);
}
Esempio n. 12
0
static void
efinet_dev_print(int verbose)
{
	CHAR16 *text;
	EFI_HANDLE h;
	int unit;

	pager_open();
	for (unit = 0, h = efi_find_handle(&efinet_dev, 0);
	    h != NULL; h = efi_find_handle(&efinet_dev, ++unit)) {
		printf("    %s%d:", efinet_dev.dv_name, unit);
		text = efi_devpath_name(efi_lookup_devpath(h));
		if (text != NULL) {
			printf("    %S", text);
			efi_free_devpath_name(text);
		}
		if (pager_output("\n"))
			break;
	}
	pager_close();
}
Esempio n. 13
0
static int
command_lsdev(int argc, char *argv[])
{
    int		verbose, ch, i;
    char	line[80];
    
    verbose = 0;
    optind = 1;
    optreset = 1;
    while ((ch = getopt(argc, argv, "v")) != -1) {
	switch(ch) {
	case 'v':
	    verbose = 1;
	    break;
	case '?':
	default:
	    /* getopt has already reported an error */
	    return(CMD_OK);
	}
    }
    argv += (optind);
    argc -= (optind);

    pager_open();
    for (i = 0; devsw[i] != NULL; i++) {
	if (devsw[i]->dv_print != NULL){
	    sprintf(line, "%s devices:\n", devsw[i]->dv_name);
	    if (pager_output(line))
		    break;
	    devsw[i]->dv_print(verbose);
	} else {
	    sprintf(line, "%s: (unknown)\n", devsw[i]->dv_name);
	    if (pager_output(line))
		    break;
	}
    }
    pager_close();
    return(CMD_OK);
}
Esempio n. 14
0
/*
 * Please note: although we use the pager for the list of commands,
 * this routine is called from the ? FORTH function which then
 * unconditionally prints some commands. This will lead to anomalous
 * behavior. There's no 'pager_output' binding to FORTH to allow
 * things to work right, so I'm documenting the bug rather than
 * fixing it.
 */
static int
command_commandlist(int argc, char *argv[])
{
    struct bootblk_command	**cmdp;
    int		res;
    char	name[20];

    res = 0;
    pager_open();
    res = pager_output("Available commands:\n");
    SET_FOREACH(cmdp, Xcommand_set) {
	if (res)
	    break;
	if (((*cmdp)->c_name != NULL) && ((*cmdp)->c_desc != NULL)) {
	    sprintf(name, "  %-15s  ", (*cmdp)->c_name);
	    pager_output(name);
	    pager_output((*cmdp)->c_desc);
	    res = pager_output("\n");
	}
    }
    pager_close();
    return(CMD_OK);
}
Esempio n. 15
0
int
command_lsdev(int argc, char *argv[])
{
    int		verbose, ch, i;
    char	line[80];
    
    verbose = 0;
    optind = 1;
    optreset = 1;
    while ((ch = getopt(argc, argv, "v")) != -1) {
	switch(ch) {
	case 'v':
	    verbose = 1;
	    break;
	case '?':
	default:
	    /* getopt has already reported an error */
	    return(CMD_OK);
	}
    }
    argv += (optind);
    argc -= (optind);

    pager_open();

    sprintf(line, "Device Enumeration:\n");
    pager_output(line);

    for (i = 0; i < ndevs; i++) {
	 sprintf(line, "%s\n", devsw[i].dv_name);
	    if (pager_output(line))
		    break;
    }

    pager_close();
    return(CMD_OK);
}
Esempio n. 16
0
/*
 * Display a region in traditional hexdump format.
 */
void
hexdump(caddr_t region, size_t len)
{
    caddr_t	line;
    int		x, c;
    char	lbuf[80];
#define emit(fmt, args...)	{sprintf(lbuf, fmt , ## args); pager_output(lbuf);}

    pager_open();
    for (line = region; line < (region + len); line += 16) {
	emit("%08lx  ", (long) line);
	
	for (x = 0; x < 16; x++) {
	    if ((line + x) < (region + len)) {
		emit("%02x ", *(u_int8_t *)(line + x));
	    } else {
		emit("-- ");
	    }
	    if (x == 7)
		emit(" ");
	}
	emit(" |");
	for (x = 0; x < 16; x++) {
	    if ((line + x) < (region + len)) {
		c = *(u_int8_t *)(line + x);
		if ((c < ' ') || (c > '~'))	/* !isprint(c) */
		    c = '.';
		emit("%c", c);
	    } else {
		emit(" ");
	    }
	}
	emit("|\n");
    }
    pager_close();
}
Esempio n. 17
0
int main(int argc, char *argv[]) {
        int r = 0, retval = EXIT_FAILURE;
        int output_flags;
        _cleanup_free_ char *root = NULL;
        _cleanup_bus_close_unref_ sd_bus *bus = NULL;

        log_parse_environment();
        log_open();

        r = parse_argv(argc, argv);
        if (r < 0)
                goto finish;
        else if (r == 0) {
                retval = EXIT_SUCCESS;
                goto finish;
        }

        if (!arg_no_pager) {
                r = pager_open(false);
                if (r > 0) {
                        if (arg_full == -1)
                                arg_full = true;
                }
        }

        output_flags =
                arg_all * OUTPUT_SHOW_ALL |
                (arg_full > 0) * OUTPUT_FULL_WIDTH;

        r = bus_open_transport(BUS_TRANSPORT_LOCAL, NULL, false, &bus);
        if (r < 0) {
                log_error_errno(r, "Failed to create bus connection: %m");
                goto finish;
        }

        if (optind < argc) {
                int i;

                for (i = optind; i < argc; i++) {
                        int q;

                        fprintf(stdout, "%s:\n", argv[i]);
                        fflush(stdout);

                        if (arg_machine)
                                root = strjoin("machine/", arg_machine, "/", argv[i], NULL);
                        else
                                root = strdup(argv[i]);
                        if (!root)
                                return log_oom();

                        q = show_cgroup_by_path(root, NULL, 0,
                                                arg_kernel_threads, output_flags);
                        if (q < 0)
                                r = q;
                }

        } else {
                _cleanup_free_ char *p;

                p = get_current_dir_name();
                if (!p) {
                        log_error_errno(errno, "Cannot determine current working directory: %m");
                        goto finish;
                }

                if (path_startswith(p, "/sys/fs/cgroup") && !arg_machine) {
                        printf("Working Directory %s:\n", p);
                        r = show_cgroup_by_path(p, NULL, 0,
                                                arg_kernel_threads, output_flags);
                } else {
                        if (arg_machine) {
                                char *m;
                                const char *cgroup;
                                _cleanup_free_ char *scope = NULL;
                                _cleanup_free_ char *path = NULL;
                                _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
                                _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;

                                m = strjoina("/run/systemd/machines/", arg_machine);
                                r = parse_env_file(m, NEWLINE, "SCOPE", &scope, NULL);
                                if (r < 0) {
                                        log_error_errno(r, "Failed to get machine path: %m");
                                        goto finish;
                                }

                                path = unit_dbus_path_from_name(scope);
                                if (!path) {
                                        log_oom();
                                        goto finish;
                                }

                                r = sd_bus_get_property(
                                                bus,
                                                "org.freedesktop.systemd1",
                                                path,
                                                "org.freedesktop.systemd1.Scope",
                                                "ControlGroup",
                                                &error,
                                                &reply,
                                                "s");

                                if (r < 0) {
                                        log_error("Failed to query ControlGroup: %s", bus_error_message(&error, -r));
                                        goto finish;
                                }

                                r = sd_bus_message_read(reply, "s", &cgroup);
                                if (r < 0) {
                                        bus_log_parse_error(r);
                                        goto finish;
                                }

                                root = strdup(cgroup);
                                if (!root) {
                                        log_oom();
                                        goto finish;
                                }

                        } else
                                r = cg_get_root_path(&root);
                        if (r < 0) {
                                log_error_errno(r, "Failed to get %s path: %m",
                                                arg_machine ? "machine" : "root");
                                goto finish;
                        }

                        r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, root, NULL, 0,
                                        arg_kernel_threads, output_flags);
                }
        }

        if (r < 0) {
                log_error_errno(r, "Failed to list cgroup tree %s: %m", root);
                retval = EXIT_FAILURE;
        } else
                retval = EXIT_SUCCESS;

finish:
        pager_close();

        return retval;
}
Esempio n. 18
0
int main(int argc, char *argv[]) {
        int r;
        sd_journal *j = NULL;
        unsigned line = 0;
        bool need_seek = false;
        sd_id128_t previous_boot_id;
        bool previous_boot_id_valid = false;
        bool have_pager;

        log_parse_environment();
        log_open();

        r = parse_argv(argc, argv);
        if (r <= 0)
                goto finish;

        if (arg_new_id128) {
                r = generate_new_id128();
                goto finish;
        }

#ifdef HAVE_ACL
        if (!arg_quiet && geteuid() != 0 && in_group("adm") <= 0)
                log_warning("Showing user generated messages only. Users in the group 'adm' can see all messages. Pass -q to turn this message off.");
#endif

        if (arg_directory)
                r = sd_journal_open_directory(&j, arg_directory, 0);
        else
                r = sd_journal_open(&j, arg_local ? SD_JOURNAL_LOCAL_ONLY : 0);

        if (r < 0) {
                log_error("Failed to open journal: %s", strerror(-r));
                goto finish;
        }

        if (arg_print_header) {
                journal_print_header(j);
                r = 0;
                goto finish;
        }

        r = add_this_boot(j);
        if (r < 0)
                goto finish;

        r = add_matches(j, argv + optind);
        if (r < 0)
                goto finish;

        if (!arg_quiet) {
                usec_t start, end;
                char start_buf[FORMAT_TIMESTAMP_MAX], end_buf[FORMAT_TIMESTAMP_MAX];

                r = sd_journal_get_cutoff_realtime_usec(j, &start, &end);
                if (r < 0) {
                        log_error("Failed to get cutoff: %s", strerror(-r));
                        goto finish;
                }

                if (r > 0) {
                        if (arg_follow)
                                printf("Logs begin at %s.\n", format_timestamp(start_buf, sizeof(start_buf), start));
                        else
                                printf("Logs begin at %s, end at %s.\n",
                                       format_timestamp(start_buf, sizeof(start_buf), start),
                                       format_timestamp(end_buf, sizeof(end_buf), end));
                }
        }

        if (arg_lines >= 0) {
                r = sd_journal_seek_tail(j);
                if (r < 0) {
                        log_error("Failed to seek to tail: %s", strerror(-r));
                        goto finish;
                }

                r = sd_journal_previous_skip(j, arg_lines);
        } else {
                r = sd_journal_seek_head(j);
                if (r < 0) {
                        log_error("Failed to seek to head: %s", strerror(-r));
                        goto finish;
                }

                r = sd_journal_next(j);
        }

        if (r < 0) {
                log_error("Failed to iterate through journal: %s", strerror(-r));
                goto finish;
        }

        have_pager = !arg_no_pager && !arg_follow;
        if (have_pager) {
                columns();
                pager_open();
        }

        if (arg_output == OUTPUT_JSON) {
                fputc('[', stdout);
                fflush(stdout);
        }

        for (;;) {
                for (;;) {
                        sd_id128_t boot_id;
                        int flags = (arg_show_all*OUTPUT_SHOW_ALL |
                                     have_pager*OUTPUT_FULL_WIDTH);

                        if (need_seek) {
                                r = sd_journal_next(j);
                                if (r < 0) {
                                        log_error("Failed to iterate through journal: %s", strerror(-r));
                                        goto finish;
                                }
                        }

                        if (r == 0)
                                break;

                        r = sd_journal_get_monotonic_usec(j, NULL, &boot_id);
                        if (r >= 0) {
                                if (previous_boot_id_valid &&
                                    !sd_id128_equal(boot_id, previous_boot_id))
                                        printf(ANSI_HIGHLIGHT_ON "----- Reboot -----" ANSI_HIGHLIGHT_OFF "\n");

                                previous_boot_id = boot_id;
                                previous_boot_id_valid = true;
                        }

                        line ++;

                        r = output_journal(j, arg_output, line, 0, flags);
                        if (r < 0)
                                goto finish;

                        need_seek = true;
                }

                if (!arg_follow)
                        break;

                r = sd_journal_wait(j, (uint64_t) -1);
                if (r < 0) {
                        log_error("Couldn't wait for log event: %s", strerror(-r));
                        goto finish;
                }
        }

        if (arg_output == OUTPUT_JSON)
                fputs("\n]\n", stdout);

finish:
        if (j)
                sd_journal_close(j);

        pager_close();

        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
Esempio n. 19
0
static int
command_memmap(int argc, char *argv[])
{
	UINTN sz;
	EFI_MEMORY_DESCRIPTOR *map, *p;
	UINTN key, dsz;
	UINT32 dver;
	EFI_STATUS status;
	int i, ndesc;
	char line[80];
	static char *types[] = {
	    "Reserved",
	    "LoaderCode",
	    "LoaderData",
	    "BootServicesCode",
	    "BootServicesData",
	    "RuntimeServicesCode",
	    "RuntimeServicesData",
	    "ConventionalMemory",
	    "UnusableMemory",
	    "ACPIReclaimMemory",
	    "ACPIMemoryNVS",
	    "MemoryMappedIO",
	    "MemoryMappedIOPortSpace",
	    "PalCode"
	};

	sz = 0;
	status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
	if (status != EFI_BUFFER_TOO_SMALL) {
		printf("Can't determine memory map size\n");
		return (CMD_ERROR);
	}
	map = malloc(sz);
	status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
	if (EFI_ERROR(status)) {
		printf("Can't read memory map\n");
		return (CMD_ERROR);
	}

	ndesc = sz / dsz;
	snprintf(line, sizeof(line), "%23s %12s %12s %8s %4s\n",
	    "Type", "Physical", "Virtual", "#Pages", "Attr");
	pager_open();
	if (pager_output(line)) {
		pager_close();
		return (CMD_OK);
	}

	for (i = 0, p = map; i < ndesc;
	     i++, p = NextMemoryDescriptor(p, dsz)) {
		printf("%23s %012jx %012jx %08jx ", types[p->Type],
		    (uintmax_t)p->PhysicalStart, (uintmax_t)p->VirtualStart,
		    (uintmax_t)p->NumberOfPages);
		if (p->Attribute & EFI_MEMORY_UC)
			printf("UC ");
		if (p->Attribute & EFI_MEMORY_WC)
			printf("WC ");
		if (p->Attribute & EFI_MEMORY_WT)
			printf("WT ");
		if (p->Attribute & EFI_MEMORY_WB)
			printf("WB ");
		if (p->Attribute & EFI_MEMORY_UCE)
			printf("UCE ");
		if (p->Attribute & EFI_MEMORY_WP)
			printf("WP ");
		if (p->Attribute & EFI_MEMORY_RP)
			printf("RP ");
		if (p->Attribute & EFI_MEMORY_XP)
			printf("XP ");
		if (pager_output("\n"))
			break;
	}

	pager_close();
	return (CMD_OK);
}
Esempio n. 20
0
int main(int argc, char *argv[]) {
        int r = 0, retval = EXIT_FAILURE;
        int output_flags;
        char _cleanup_free_ *root = NULL;

        log_parse_environment();
        log_open();

        r = parse_argv(argc, argv);
        if (r < 0)
                goto finish;
        else if (r == 0) {
                retval = EXIT_SUCCESS;
                goto finish;
        }

        if (!arg_no_pager) {
                r = pager_open(false);
                if (r > 0) {
                        if (arg_full == -1)
                                arg_full = true;
                }
        }

        output_flags =
                arg_all * OUTPUT_SHOW_ALL |
                (arg_full > 0) * OUTPUT_FULL_WIDTH;

        if (optind < argc) {
                int i;

                for (i = optind; i < argc; i++) {
                        int q;

                        fprintf(stdout, "%s:\n", argv[i]);
                        fflush(stdout);

                        if (arg_machine)
                                root = strjoin("machine/", arg_machine, "/", argv[i], NULL);
                        else
                                root = strdup(argv[i]);
                        if (!root)
                                return log_oom();

                        q = show_cgroup_by_path(root, NULL, 0,
                                                arg_kernel_threads, output_flags);
                        if (q < 0)
                                r = q;
                }

        } else {
                _cleanup_free_ char *p;

                p = get_current_dir_name();
                if (!p) {
                        log_error("Cannot determine current working directory: %m");
                        goto finish;
                }

                if (path_startswith(p, "/sys/fs/cgroup") && !arg_machine) {
                        printf("Working Directory %s:\n", p);
                        r = show_cgroup_by_path(p, NULL, 0,
                                                arg_kernel_threads, output_flags);
                } else {
                        if (arg_machine) {
                                char *m;
                                m = strappenda("/run/systemd/machines/", arg_machine);
                                r = parse_env_file(m, NEWLINE, "CGROUP", &root, NULL);
                        } else
                                r = cg_get_root_path(&root);
                        if (r < 0) {
                                log_error("Failed to get %s path: %s",
                                          arg_machine ? "machine" : "root", strerror(-r));
                                goto finish;
                        }

                        r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, root, NULL, 0,
                                        arg_kernel_threads, output_flags);
                }
        }

        if (r < 0) {
                log_error("Failed to list cgroup tree %s: %s", root, strerror(-r));
                retval = EXIT_FAILURE;
        } else
                retval = EXIT_SUCCESS;

finish:
        pager_close();

        return retval;
}
Esempio n. 21
0
static int
command_efi_show(int argc, char *argv[])
{
	/*
	 * efi-show [-a]
	 *	print all the env
	 * efi-show -u UUID
	 *	print all the env vars tagged with UUID
	 * efi-show -v var
	 *	search all the env vars and print the ones matching var
	 * eif-show -u UUID -v var
	 * eif-show UUID var
	 *	print all the env vars that match UUID and var
	 */
	/* NB: We assume EFI_GUID is the same as uuid_t */
	int		aflag = 0, gflag = 0, lflag = 0, vflag = 0;
	int		ch, rv;
	unsigned	i;
	EFI_STATUS	status;
	EFI_GUID	varguid = { 0,0,0,{0,0,0,0,0,0,0,0} };
	EFI_GUID	matchguid = { 0,0,0,{0,0,0,0,0,0,0,0} };
	uint32_t	uuid_status;
	CHAR16		*varname;
	CHAR16		*newnm;
	CHAR16		varnamearg[128];
	UINTN		varalloc;
	UINTN		varsz;

	while ((ch = getopt(argc, argv, "ag:lv:")) != -1) {
		switch (ch) {
		case 'a':
			aflag = 1;
			break;
		case 'g':
			gflag = 1;
			uuid_from_string(optarg, (uuid_t *)&matchguid,
			    &uuid_status);
			if (uuid_status != uuid_s_ok) {
				printf("uid %s could not be parsed\n", optarg);
				return (CMD_ERROR);
			}
			break;
		case 'l':
			lflag = 1;
			break;
		case 'v':
			vflag = 1;
			if (strlen(optarg) >= nitems(varnamearg)) {
				printf("Variable %s is longer than %zd characters\n",
				    optarg, nitems(varnamearg));
				return (CMD_ERROR);
			}
			for (i = 0; i < strlen(optarg); i++)
				varnamearg[i] = optarg[i];
			varnamearg[i] = 0;
			break;
		default:
			printf("Invalid argument %c\n", ch);
			return (CMD_ERROR);
		}
	}

	if (aflag && (gflag || vflag)) {
		printf("-a isn't compatible with -v or -u\n");
		return (CMD_ERROR);
	}

	if (aflag && optind < argc) {
		printf("-a doesn't take any args");
		return (CMD_ERROR);
	}

	if (optind == argc)
		aflag = 1;

	argc -= optind;
	argv += optind;

	pager_open();
	if (vflag && gflag) {
		rv = efi_print_var(varnamearg, &matchguid, lflag);
		pager_close();
		return (rv);
	}

	if (argc == 2) {
		optarg = argv[0];
		if (strlen(optarg) >= nitems(varnamearg)) {
			printf("Variable %s is longer than %zd characters\n",
			    optarg, nitems(varnamearg));
			pager_close();
			return (CMD_ERROR);
		}
		for (i = 0; i < strlen(optarg); i++)
			varnamearg[i] = optarg[i];
		varnamearg[i] = 0;
		optarg = argv[1];
		uuid_from_string(optarg, (uuid_t *)&matchguid,
		    &uuid_status);
		if (uuid_status != uuid_s_ok) {
			printf("uid %s could not be parsed\n", optarg);
			pager_close();
			return (CMD_ERROR);
		}
		rv = efi_print_var(varnamearg, &matchguid, lflag);
		pager_close();
		return (rv);
	}

	if (argc > 0) {
		printf("Too many args %d\n", argc);
		pager_close();
		return (CMD_ERROR);
	}

	/*
	 * Initiate the search -- note the standard takes pain
	 * to specify the initial call must be a poiner to a NULL
	 * character.
	 */
	varalloc = 1024;
	varname = malloc(varalloc);
	if (varname == NULL) {
		printf("Can't allocate memory to get variables\n");
		pager_close();
		return (CMD_ERROR);
	}
	varname[0] = 0;
	while (1) {
		varsz = varalloc;
		status = RS->GetNextVariableName(&varsz, varname, &varguid);
		if (status == EFI_BUFFER_TOO_SMALL) {
			varalloc = varsz;
			newnm = malloc(varalloc);
			if (newnm == NULL) {
				printf("Can't allocate memory to get variables\n");
				free(varname);
				pager_close();
				return (CMD_ERROR);
			}
			memcpy(newnm, varname, varsz);
			free(varname);
			varname = newnm;
			continue; /* Try again with bigger buffer */
		}
		if (status != EFI_SUCCESS)
			break;
		if (aflag) {
			if (efi_print_var(varname, &varguid, lflag) != CMD_OK)
				break;
			continue;
		}
		if (vflag) {
			if (wcscmp(varnamearg, varname) == 0) {
				if (efi_print_var(varname, &varguid, lflag) != CMD_OK)
					break;
				continue;
			}
		}
		if (gflag) {
			if (memcmp(&varguid, &matchguid, sizeof(varguid)) == 0) {
				if (efi_print_var(varname, &varguid, lflag) != CMD_OK)
					break;
				continue;
			}
		}
	}
	free(varname);
	pager_close();

	return (CMD_OK);
}
Esempio n. 22
0
static int
command_help(int argc, char *argv[]) 
{
    char	buf[81];	/* XXX buffer size? */
    int		hfd, matched, doindex;
    char	*topic, *subtopic, *t, *s, *d;

    /* page the help text from our load path */
    /* sprintf(buf, "%s/boot/loader.help", getenv("loaddev")); */
    /* page the help text from our base path */
    snprintf(buf, sizeof(buf), "%sloader.help", getenv("base"));
    if ((hfd = open(buf, O_RDONLY)) < 0) {
	if ((hfd = rel_open("loader.help", NULL, O_RDONLY)) < 0) {
	    printf("Verbose help not available, use '?' to list commands\n");
	    return(CMD_OK);
	}
    }

    /* pick up request from arguments */
    topic = subtopic = NULL;
    switch(argc) {
    case 3:
	subtopic = strdup(argv[2]);
    case 2:
	topic = strdup(argv[1]);
	break;
    case 1:
	topic = strdup("help");
	break;
    default:
	command_errmsg = "usage is 'help <topic> [<subtopic>]";
	return(CMD_ERROR);
    }

    /* magic "index" keyword */
    doindex = !strcmp(topic, "index");
    matched = doindex;
    
    /* Scan the helpfile looking for help matching the request */
    pager_open();
    while(help_getnext(hfd, &t, &s, &d)) {

	if (doindex) {		/* dink around formatting */
	    help_emitsummary(t, s, d);

	} else if (strcmp(topic, t)) {
	    /* topic mismatch */
	    if(matched)		/* nothing more on this topic, stop scanning */
		break;

	} else {
	    /* topic matched */
	    matched = 1;
	    if (((subtopic == NULL) && (s == NULL)) ||
		((subtopic != NULL) && (s != NULL) && !strcmp(subtopic, s))) {
		/* exact match, print text */
		while((fgetstr(buf, 80, hfd) >= 0) && (buf[0] != '#')) {
		    if (pager_output(buf))
			break;
		    if (pager_output("\n"))
			break;
		}
	    } else if ((subtopic == NULL) && (s != NULL)) {
		/* topic match, list subtopics */
		help_emitsummary(t, s, d);
	    }
	}
	free(t);
	free(s);
	free(d);
    }
    pager_close();
    close(hfd);
    if (!matched) {
	sprintf(command_errbuf, "no help available for '%s'", topic);
	free(topic);
	if (subtopic)
	    free(subtopic);
	return(CMD_ERROR);
    }
    free(topic);
    if (subtopic)
	free(subtopic);
    return(CMD_OK);
}
Esempio n. 23
0
int myrepo_compare(char *catalogpath1, char *catalogpath2)
{
    unsigned int latest1;
    unsigned int latest2;
    HashTreeNode *tree1;
    HashTreeNode *tree2;
    const char **differences1;
    const char **differences2;
    const char **tmp1;
    const char **tmp2;
    FILE *out;

    assert(catalogpath1 != NULL);
    assert(catalogpath2 != NULL);

    /* Remove any possible end slash */
    catalogpath1[strlen(catalogpath1) - 2] = '\0';
    catalogpath2[strlen(catalogpath2) - 2] = '\0';

    /* Remove last component (.index) */
    catalogpath1 = dirname(catalogpath1);
    catalogpath2 = dirname(catalogpath2);

    latest1 = commit_latest(catalogpath1, 0);

    /* The directory is likely not a valid repo */
    if (latest1 == 0)
        return 1;

    latest2 = commit_latest(catalogpath2, 0);

    /* The directory is likely not a valid repo */
    if (latest2 == 0)
        return 1;

    /* Load the hash trees */
    tree1 = commit_loadtree(catalogpath1, latest1);
    tree2 = commit_loadtree(catalogpath2, latest2);

    if (tree1 == NULL || tree2 == NULL)
        return 1;

    /* Are the trees the same? */
    if (!strcmp(tree1->hash, tree2->hash)) {
        printf("The trees are identical.\n");
        return 0;
    }

    /* Trees are different */

    /* Open a pager to print the diff to */
    out = pager_init();

    /* Compare one way */
    differences1 = hashtree_compare(tree1, tree2);
    tmp1 = differences1;

    /* Process the differences */
    while (*tmp1 != NULL) {
        compare_diff(catalogpath1, latest1, catalogpath2, latest2, *tmp1, out);
        tmp1++;
    }

    /* Now compare the reverse way, to catch files only on the old repo.
     * Make sure we don't process files twice. */
    differences2 = hashtree_compare(tree2, tree1);
    tmp2 = differences2;

    while (*tmp2 != NULL) {
        tmp1 = differences1;
        while (*tmp1 != NULL && strcmp(*tmp2, *tmp1))
            tmp1++;

        if (*tmp1 == NULL)
            compare_diff(catalogpath1, latest1, catalogpath2, latest2, *tmp2,
                         out);

        tmp2++;
    }

    pager_close(out);
    free(differences1);
    free(differences2);

    return 0;
}
Esempio n. 24
0
int main(int argc, char *argv[]) {
        int r = 0, retval = EXIT_FAILURE;
        int output_flags;

        log_parse_environment();
        log_open();

        r = parse_argv(argc, argv);
        if (r < 0)
                goto finish;
        else if (r == 0) {
                retval = EXIT_SUCCESS;
                goto finish;
        }

        if (!arg_no_pager) {
                r = pager_open(false);
                if (r > 0) {
                        if (arg_full == -1)
                                arg_full = true;
                }
        }

        output_flags =
                arg_all * OUTPUT_SHOW_ALL |
                (arg_full > 0) * OUTPUT_FULL_WIDTH;

        if (optind < argc) {
                unsigned i;

                for (i = (unsigned) optind; i < (unsigned) argc; i++) {
                        int q;
                        printf("%s:\n", argv[i]);

                        q = show_cgroup_by_path(argv[i], NULL, 0,
                                                arg_kernel_threads, output_flags);
                        if (q < 0)
                                r = q;
                }

        } else {
                char _cleanup_free_ *p;

                p = get_current_dir_name();
                if (!p) {
                        log_error("Cannot determine current working directory: %m");
                        goto finish;
                }

                if (path_startswith(p, "/sys/fs/cgroup")) {
                        printf("Working Directory %s:\n", p);
                        r = show_cgroup_by_path(p, NULL, 0,
                                                arg_kernel_threads, output_flags);
                } else {
                        char _cleanup_free_ *root = NULL;
                        const char *t = NULL;

                        r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &root);
                        if (r < 0)
                                t = "/";
                        else {
                                if (endswith(root, "/system"))
                                        root[strlen(root)-7] = 0;

                                t = root[0] ? root : "/";
                        }

                        r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, t, NULL, 0,
                                        arg_kernel_threads, output_flags);
                }
        }

        if (r < 0)
                log_error("Failed to list cgroup tree: %s", strerror(-r));

        retval = EXIT_SUCCESS;

finish:
        pager_close();

        return retval;
}
Esempio n. 25
0
static int
command_ls(int argc, char *argv[])
{
    int		fd;
    struct stat	sb;
    struct 	dirent *d;
    char	*buf, *path;
    char	lbuf[128];		/* one line */
    int		result, ch;
    int		verbose;
	
    result = CMD_OK;
    fd = -1;
    verbose = 0;
    optind = 1;
    optreset = 1;
    while ((ch = getopt(argc, argv, "l")) != -1) {
	switch(ch) {
	case 'l':
	    verbose = 1;
	    break;
	case '?':
	default:
	    /* getopt has already reported an error */
	    return(CMD_OK);
	}
    }
    argv += (optind - 1);
    argc -= (optind - 1);

    if (argc < 2) {
	path = "";
    } else {
	path = argv[1];
    }

    fd = ls_getdir(&path);
    if (fd == -1) {
	result = CMD_ERROR;
	goto out;
    }
    pager_open();
    pager_output(path);
    pager_output("\n");

    while ((d = readdirfd(fd)) != NULL) {
	if (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) {
	    if (verbose) {
		/* stat the file, if possible */
		sb.st_size = 0;
		buf = malloc(strlen(path) + strlen(d->d_name) + 2);
		sprintf(buf, "%s/%s", path, d->d_name);
		/* ignore return, could be symlink, etc. */
		if (stat(buf, &sb))
		    sb.st_size = 0;
		free(buf);
		sprintf(lbuf, " %c %8d %s\n", typestr[d->d_type],
		    (int)sb.st_size, d->d_name);
	    } else {
		sprintf(lbuf, " %c  %s\n", typestr[d->d_type], d->d_name);
	    }
	    if (pager_output(lbuf))
		goto out;
	}
    }
 out:
    pager_close();
    if (fd != -1)
	close(fd);
    if (path != NULL)
	free(path);
    return(result);
}
Esempio n. 26
0
void
ofw_memmap(int acells)
{
	struct		ofw_mapping *mapptr;
	struct		ofw_mapping2 *mapptr2;
        phandle_t	mmup;
        int		nmapping, i;
        u_char		mappings[256 * sizeof(struct ofw_mapping2)];
        char		lbuf[80];

	mmup = OF_instance_to_package(mmu);

	bzero(mappings, sizeof(mappings));

	nmapping = OF_getprop(mmup, "translations", mappings, sizeof(mappings));
	if (nmapping == -1) {
		printf("Could not get memory map (%d)\n",
		    nmapping);
		return;
	}

	pager_open();
	if (acells == 1) {
		nmapping /= sizeof(struct ofw_mapping);
		mapptr = (struct ofw_mapping *) mappings;

		printf("%17s\t%17s\t%8s\t%6s\n", "Virtual Range",
		    "Physical Range", "#Pages", "Mode");

		for (i = 0; i < nmapping; i++) {
			sprintf(lbuf, "%08x-%08x\t%08x-%08x\t%8d\t%6x\n",
				mapptr[i].va,
				mapptr[i].va + mapptr[i].len,
				mapptr[i].pa,
				mapptr[i].pa + mapptr[i].len,
				mapptr[i].len / 0x1000,
				mapptr[i].mode);
			if (pager_output(lbuf))
				break;
		}
	} else {
		nmapping /= sizeof(struct ofw_mapping2);
		mapptr2 = (struct ofw_mapping2 *) mappings;

		printf("%17s\t%17s\t%8s\t%6s\n", "Virtual Range",
		       "Physical Range", "#Pages", "Mode");

		for (i = 0; i < nmapping; i++) {
			sprintf(lbuf, "%08x-%08x\t%08x-%08x\t%8d\t%6x\n",
				mapptr2[i].va,
				mapptr2[i].va + mapptr2[i].len,
				mapptr2[i].pa_lo,
				mapptr2[i].pa_lo + mapptr2[i].len,
				mapptr2[i].len / 0x1000,
				mapptr2[i].mode);
			if (pager_output(lbuf))
				break;
		}
	}
	pager_close();
}