int
main(int argc, char **argv)
{
	struct pkgdb *db;
	int err;

	if (argc != 2) {
		fprintf(stderr, "usage: %s packagename\n", argv[0]);
		return 1;
	}

	db = NULL;

	err = pkg_init(NULL);
	if (err == EPKG_OK)
		err = getdb(&db);
	else
		fprintf(stderr, "could not parse config file\n");

	if (err == EPKG_OK) {
		struct pkgdb_it *it;

		it = pkgdb_query(db, argv[1], MATCH_EXACT);
		if (it == NULL)
			printf("no local matches!\n");
		else handle_it(it);

		it = pkgdb_rquery(db, argv[1], MATCH_EXACT, NULL);
		if (it == NULL)
			printf("no remote matches!\n");
		else handle_it(it);

		printf("shutting down\n");
		pkgdb_it_free(it);
		pkgdb_close(db);
		pkg_shutdown();
		printf("shutdown\n");
	}
}
示例#2
0
文件: main.c 项目: toddnni/pkgng
int
main(int argc, char **argv)
{
    unsigned int i;
    struct commands *command = NULL;
    unsigned int ambiguous = 0;
    const char *chroot_path = NULL;
    int jid;
    const char *jail_str = NULL;
    size_t len;
    signed char ch;
    int debug = 0;
    int ret = EX_USAGE;

    if (argc < 2)
        usage();

    pkg_event_register(&event_callback, &debug);

    while ((ch = getopt(argc, argv, "dj:c:v")) != -1) {
        switch(ch) {
        case 'd':
            debug++;
            break;
        case 'c':
            chroot_path = optarg;
            break;
        case 'j':
            jail_str = optarg;
            break;
        case 'v':
            printf("%s %s\n", PKGVERSION, GITHASH);
            exit(EXIT_SUCCESS);
            break; /* NOT REACHED */
        default:
            break;
        }
    }
    argc -= optind;
    argv += optind;

    if (argc == 0)
        usage();

    /* reset getopt for the next call */
    optreset = 1;
    optind = 1;

    if (jail_str != NULL && chroot_path != NULL) {
        fprintf(stderr, "-j and -c cannot be used at the same time\n");
        usage();
    }

    if (chroot_path != NULL)
        if (chroot(chroot_path) == -1)
            errx(EX_SOFTWARE, "chroot failed");

    if (jail_str != NULL) {
        jid = jail_getid(jail_str);
        if (jid < 0)
            errx(1, "%s", jail_errmsg);

        if (jail_attach(jid) == -1)
            err(1, "jail_attach(%s)", jail_str);
    }

    if (jail_str != NULL || chroot_path != NULL)
        if (chdir("/") == -1)
            errx(EX_SOFTWARE, "chdir() failed");

    if (pkg_init(NULL) != EPKG_OK)
        errx(EX_SOFTWARE, "can not parse configuration file");

    len = strlen(argv[0]);
    for (i = 0; i < cmd_len; i++) {
        if (strncmp(argv[0], cmd[i].name, len) == 0) {
            /* if we have the exact cmd */
            if (len == strlen(cmd[i].name)) {
                command = &cmd[i];
                ambiguous = 0;
                break;
            }

            /*
             * we already found a partial match so `argv[0]' is
             * an ambiguous shortcut
             */
            ambiguous++;

            command = &cmd[i];
        }
    }

    if (command == NULL) {
        pkg_shutdown();
        usage();
        return (ret); /* Not reached but makes scanbuild happy */
    }

    if (ambiguous <= 1) {
        assert(command->exec != NULL);
        ret = command->exec(argc, argv);
    } else {
        warnx("'%s' is not a valid command.\n", argv[0]);

        fprintf(stderr, "See 'pkg help' for more information on the commands.\n\n");
        fprintf(stderr, "Command '%s' could be one of the following:\n", argv[0]);

        for (i = 0; i < cmd_len; i++)
            if (strncmp(argv[0], cmd[i].name, len) == 0)
                fprintf(stderr, "\t%s\n",cmd[i].name);
    }

    pkg_shutdown();
    return (ret);
}
示例#3
0
/* ---------------------------------------------------------------------
 */
int
netsnmp_swinst_arch_load( netsnmp_container *container, u_int flags)
{
    DIR                  *d;
    struct dirent        *dp;
    struct stat           stat_buf;
#ifdef HAVE_PKGINFO
    char                 *v, *c;
#endif
    char                  buf[ BUFSIZ ];
    unsigned char        *cp;
    time_t                install_time;
    size_t                date_len;
    int                   i = 1;
    netsnmp_swinst_entry *entry;
#ifdef HAVE_LIBPKG
    struct pkgdb *db = NULL;
    struct pkgdb_it *it = NULL;
    struct pkg *pkg = NULL;
    char pkgname[ SNMP_MAXPATH ];
    char pkgdate[ BUFSIZ ];
    int pkgng = 0;

    if (pkg_init(NULL, NULL)) {
        snmp_log( LOG_ERR, "SWInst: error initializing pkgng db\n" );
        return 1;
    }

    if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
	snmp_log( LOG_ERR, "SWInst: error opening pkgng db\n" );
	return 1;
    }

    if (pkg_status(NULL) == PKG_STATUS_ACTIVE) {
	pkgng = 1;
    } else {
	snmp_log( LOG_INFO, "SWInst: not a pkgng system\n" );
    }

    /* if we are using FreeBSD's pkgng */
    if (pkgng) {
        if ((it = pkgdb_query(db, NULL, MATCH_ALL)) == NULL) {
            snmp_log( LOG_ERR, "SWInst: error querying pkgng db\n" );
            return 1;
        }

        while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC) == EPKG_OK) {
            pkg_snprintf(pkgname, sizeof(pkgname), "%n-%v", pkg, pkg);
            pkg_snprintf(pkgdate, sizeof(pkgdate), "%t", pkg);

            entry = netsnmp_swinst_entry_create( i++ );

            if (NULL == entry)
                continue;   /* error already logged by function */

            CONTAINER_INSERT(container, entry);

            entry->swName_len = snprintf( entry->swName, sizeof(entry->swName),
                                          "%s", pkgname );
            if (entry->swName_len >= sizeof(entry->swName))
                entry->swName_len = sizeof(entry->swName)-1;

            install_time = atoi(pkgdate);
            cp = date_n_time( &install_time, &date_len );
            memcpy( entry->swDate, cp, date_len );
            entry->swDate_len = date_len;
        }

        pkgdb_it_free(it);
        pkgdb_close(db);
        pkg_shutdown();
    } else {
#endif /* HAVE_LIBPKG */
        if ( !pkg_directory[0] ) {
            return 1;    /* Can't report installed packages
                         if there isn't a list of them! */
        }

        d = opendir( pkg_directory );
        if (!d)
            return 1;

        while ((dp = readdir(d)) != NULL) {
            if ( '.' == dp->d_name[0] )
                continue;
            snprintf( buf, BUFSIZ, "%s/%s", pkg_directory, dp->d_name );
            if (stat( buf, &stat_buf ) < 0)
                continue;
            entry = netsnmp_swinst_entry_create( i++ );
            if (NULL == entry)
                continue;   /* error already logged by function */
            CONTAINER_INSERT(container, entry);

#ifdef HAVE_PKGINFO
	    v = pkgparam( dp->d_name, "VERSION" );
	    c = pkgparam( dp->d_name, "CATEGORY" );

	    entry->swName_len = snprintf( entry->swName, sizeof(entry->swName),
					  "%s-%s", dp->d_name, v );
	    if (entry->swName_len >= sizeof(entry->swName))
		entry->swName_len = sizeof(entry->swName)-1;
	    entry->swType = (NULL != strstr( c, "system"))
			    ? 2      /* operatingSystem */
			    : 4;     /*  application    */

	    /* Do we need to free 'v' & 'c' ??? */
#else
	    entry->swName_len = snprintf( entry->swName, sizeof(entry->swName),
					  "%s", dp->d_name );
	    if (entry->swName_len >= sizeof(entry->swName))
		entry->swName_len = sizeof(entry->swName)-1;

	    /* no information about O/S vs application packages ??? */
#endif

	    install_time = stat_buf.st_mtime;
	    cp = date_n_time( &install_time, &date_len );
	    memcpy( entry->swDate, cp, date_len );
	    entry->swDate_len = date_len;
	}
	closedir( d );
#ifdef HAVE_LIBPKG
    }
#endif

    DEBUGMSGTL(("swinst:load:arch"," loaded %d entries\n",
                (int)CONTAINER_SIZE(container)));

    return 0;
}