Exemplo n.º 1
0
static PyObject* pyutmpx(PyObject* self, PyObject* args)
{

    PyObject *ret = PyList_New(0);
    PyObject *l_utmp = PyList_New(0);
    PyObject *l_wtmp = PyList_New(0);
    struct utmpx *ut;
    
    setutxdb(UTXDB_ACTIVE, UTX_ACTIVE);
    while ((ut = getutxent()) != NULL) PyList_Append(l_utmp, Py_BuildValue("sssi", ut->ut_line, ut->ut_user, ut->ut_host, ut->ut_tv));
    endutxent();

    setutxdb(UTXDB_LOG, UTX_LOG);
    while ((ut = getutxent()) != NULL) PyList_Append(l_wtmp, Py_BuildValue("sssi", ut->ut_line, ut->ut_user, ut->ut_host, ut->ut_tv));
    endutxent();

    PyList_Append(ret, Py_BuildValue("N", l_utmp));
    PyList_Append(ret, Py_BuildValue("N", l_wtmp));
    return Py_BuildValue("N", ret);
}
Exemplo n.º 2
0
void
setutxent()
{
	(void)memset(&ut, 0, sizeof(ut));
	if (fp == NULL)
		return;

#if 0
	if (dbtype != UTX_DB_UTMPX)
		setutxdb(UTX_DB_UTMPX, utfile);
#endif
	(void)fseeko(fp, (off_t)sizeof(ut), SEEK_SET);
}
Exemplo n.º 3
0
int
utmpx_get_entry(struct logininfo *li)
{
	struct utmpx *utx;

	if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0)
		return (0);
	utx = getutxuser(li->username);
	if (utx == NULL) {
		endutxent();
		return (0);
	}

	line_fullname(li->line, utx->ut_line,
	    MIN_SIZEOF(li->line, utx->ut_line));
	strlcpy(li->hostname, utx->ut_host,
	    MIN_SIZEOF(li->hostname, utx->ut_host));
	li->tv_sec = utx->ut_tv.tv_sec;
	li->tv_usec = utx->ut_tv.tv_usec;
	endutxent();
	return (1);
}
Exemplo n.º 4
0
static int
utmpx(int argc, char *argv[])
{
	const struct utmpx *ut;
	const char *file = NULL;
	int rv = RV_OK, db = 0;

	assert(argc > 1);
	assert(argv != NULL);

	if (argc == 3 || argc == 4) {
		if (strcmp(argv[2], "active") == 0)
			db = UTXDB_ACTIVE;
		else if (strcmp(argv[2], "lastlogin") == 0)
			db = UTXDB_LASTLOGIN;
		else if (strcmp(argv[2], "log") == 0)
			db = UTXDB_LOG;
		else
			rv = RV_USAGE;
		if (argc == 4)
			file = argv[3];
	} else {
		rv = RV_USAGE;
	}

	if (rv == RV_USAGE) {
		fprintf(stderr,
		    "Usage: %s utmpx active | lastlogin | log [filename]\n",
		    getprogname());
	} else if (rv == RV_OK) {
		if (setutxdb(db, file) != 0)
			return (RV_NOTFOUND);
		while ((ut = getutxent()) != NULL)
			utmpxprint(ut);
		endutxent();
	}
	return (rv);
}
Exemplo n.º 5
0
void
enter_lastlog(PERSON *pn)
{
	WHERE *w;
	struct utmpx *ut = NULL;
	char doit = 0;

	if (setutxdb(UTXDB_LASTLOGIN, NULL) == 0)
		ut = getutxuser(pn->name);
	if ((w = pn->whead) == NULL)
		doit = 1;
	else if (ut != NULL && ut->ut_type == USER_PROCESS) {
		/* if last login is earlier than some current login */
		for (; !doit && w != NULL; w = w->next)
			if (w->info == LOGGEDIN &&
			    w->loginat < ut->ut_tv.tv_sec)
				doit = 1;
		/*
		 * and if it's not any of the current logins
		 * can't use time comparison because there may be a small
		 * discrepancy since login calls time() twice
		 */
		for (w = pn->whead; doit && w != NULL; w = w->next)
			if (w->info == LOGGEDIN &&
			    strcmp(w->tty, ut->ut_line) == 0)
				doit = 0;
	}
	if (ut != NULL && doit) {
		w = walloc(pn);
		w->info = LASTLOG;
		strcpy(w->tty, ut->ut_line);
		strcpy(w->host, ut->ut_host);
		w->loginat = ut->ut_tv.tv_sec;
	}
	endutxent();
}
Exemplo n.º 6
0
void
setutxent(void)
{

	setutxdb(UTXDB_ACTIVE, NULL);
}
Exemplo n.º 7
0
int
main(int argc, char *argv[])
{
	int ch;

	setlocale(LC_TIME, "");

	while ((ch = getopt(argc, argv, "HTabmqsu")) != -1) {
		switch (ch) {
		case 'H':		/* Write column headings */
			Hflag = 1;
			break;
		case 'T':		/* Show terminal state */
			Tflag = 1;
			break;
		case 'a':		/* Same as -bdlprtTu */
			aflag = bflag = Tflag = uflag = 1;
			break;
		case 'b':		/* Show date of the last reboot */
			bflag = 1;
			break;
		case 'm':		/* Show info about current terminal */
			mflag = 1;
			break;
		case 'q':		/* "Quick" mode */
			qflag = 1;
			break;
		case 's':		/* Show name, line, time */
			sflag = 1;
			break;
		case 'u':		/* Show idle time */
			uflag = 1;
			break;
		default:
			usage();
			/*NOTREACHED*/
		}
	}
	argc -= optind;
	argv += optind;

	if (argc >= 2 && strcmp(argv[0], "am") == 0 &&
	    (strcmp(argv[1], "i") == 0 || strcmp(argv[1], "I") == 0)) {
		/* "who am i" or "who am I", equivalent to -m */
		mflag = 1;
		argc -= 2;
		argv += 2;
	}
	if (argc > 1)
		usage();

	if (*argv != NULL) {
		if (setutxdb(UTXDB_ACTIVE, *argv) != 0)
			err(1, "%s", *argv);
	}

	if (qflag)
		quick();
	else {
		if (sflag)
			Tflag = uflag = 0;
		if (Hflag)
			heading();
		if (mflag)
			whoami();
		else
			process_utmp();
	}

	endutxent();

	exit(0);
}
Exemplo n.º 8
0
int
main(int argc, char *argv[])
{
	int	ch, i, ulistsize;
	struct utmpx *u, *ulist;

	while ((ch = getopt(argc, argv, "f:rt")) != -1) {
		switch (ch) {
		case 'f':
			file = optarg;
			break;
		case 'r':
			order = -1;
			break;
		case 't':
			utcmp = utcmp_time;
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (argc > 0) {
		/* Process usernames given on the command line. */
		for (i = 0; i < argc; i++) {
			if (setutxdb(UTXDB_LASTLOGIN, file) != 0)
				err(1, "failed to open lastlog database");
			if ((u = getutxuser(argv[i])) == NULL) {
				warnx("user '%s' not found", argv[i]);
				continue;
			}
			output(u);
			endutxent();
		}
	} else {
		/* Read all lastlog entries, looking for active ones. */
		if (setutxdb(UTXDB_LASTLOGIN, file) != 0)
			err(1, "failed to open lastlog database");
		ulist = NULL;
		ulistsize = 0;
		while ((u = getutxent()) != NULL) {
			if (u->ut_type != USER_PROCESS)
				continue;
			if ((ulistsize % 16) == 0) {
				ulist = realloc(ulist,
				    (ulistsize + 16) * sizeof(struct utmpx));
				if (ulist == NULL)
					err(1, "malloc");
			}
			ulist[ulistsize++] = *u;
		}
		endutxent();

		qsort(ulist, ulistsize, sizeof(struct utmpx), utcmp);
		for (i = 0; i < ulistsize; i++)
			output(&ulist[i]);
	}

	exit(0);
}