本文整理汇总了C++中setutxent函数的典型用法代码示例。如果您正苦于以下问题:C++ setutxent函数的具体用法?C++ setutxent怎么用?C++ setutxent使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了setutxent函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char **argv)
{
struct utmpx u;
const char file[256] = "/home/flueg/workspace/utmp/ftmpx";
memset(&u, 0, sizeof(struct utmpx));
if (utmpxname(file) == -1) perror("failed to set ftmpx file");
strcpy(u.ut_user, getpwuid(getuid())->pw_name);
printf("ttyname is: %s\n", ttyname(0)+strlen("/dev/"));
strcpy(u.ut_id, ttyname(0) + strlen("/dev/tty"));
strcpy(u.ut_line, ttyname(0) + strlen("/dev/"));
u.ut_pid = getpid();
u.ut_type = LOGIN_PROCESS;
setutxent();
if (pututxline(&u) == NULL) perror("failed to pututxline.\n");
sleep(2);
u.ut_type = DEAD_PROCESS;
time((time_t *) &u.ut_tv.tv_sec);
setutxent();
if (pututxline(&u) == NULL) perror("failed to pututxline.\n");
endutxent();
exit(0);
}
示例2: getutxent
/*
Show utmp information via getutxent(3)
@function utmp
@return information (TABLE)
*/
static int
Futmp(lua_State *L)
{
struct utmpx *utx = {0};
setutxent();
lua_createtable(L, 0, 7);
while ((utx = getutxent())) {
lua_pushstring(L, utx->ut_user);
lua_setfield(L, -2, "user");
lua_pushstring(L, utx->ut_line);
lua_setfield(L, -2, "tty");
lua_pushstring(L, utx->ut_host);
lua_setfield(L, -2, "hostname");
lua_pushnumber(L, (float)utx->ut_tv.tv_sec);
lua_setfield(L, -2, "timestamp");
lua_pushboolean(L, utx->ut_type & USER_PROCESS);
lua_setfield(L, -2, "user_process");
lua_pushboolean(L, utx->ut_type & INIT_PROCESS);
lua_setfield(L, -2, "init_process");
lua_pushboolean(L, utx->ut_type & LOGIN_PROCESS);
lua_setfield(L, -2, "login_process");
}
endutxent();
return 1;
}
示例3: wall_main
int wall_main(int argc UNUSED_PARAM, char **argv)
{
struct utmpx *ut;
char *msg;
int fd;
fd = STDIN_FILENO;
if (argv[1]) {
/* The applet is setuid.
* Access to the file must be under user's uid/gid.
*/
fd = xopen_as_uid_gid(argv[1], O_RDONLY, getuid(), getgid());
}
msg = xmalloc_read(fd, NULL);
if (ENABLE_FEATURE_CLEAN_UP && argv[1])
close(fd);
setutxent();
while ((ut = getutxent()) != NULL) {
char *line;
if (ut->ut_type != USER_PROCESS)
continue;
line = concat_path_file("/dev", ut->ut_line);
xopen_xwrite_close(line, msg);
free(line);
}
if (ENABLE_FEATURE_CLEAN_UP) {
endutxent();
free(msg);
}
return EXIT_SUCCESS;
}
示例4: cleanup_utmp
static void cleanup_utmp(void)
{
struct timeval tv;
if (!pty_stamped_utmp)
return;
utmp_entry.ut_type = DEAD_PROCESS;
memset(utmp_entry.ut_user, 0, lenof(utmp_entry.ut_user));
gettimeofday(&tv, NULL);
utmp_entry.ut_tv.tv_sec = tv.tv_sec;
utmp_entry.ut_tv.tv_usec = tv.tv_usec;
updwtmpx(WTMPX_FILE, &utmp_entry);
memset(utmp_entry.ut_line, 0, lenof(utmp_entry.ut_line));
utmp_entry.ut_tv.tv_sec = 0;
utmp_entry.ut_tv.tv_usec = 0;
setutxent();
pututxline(&utmp_entry);
endutxent();
pty_stamped_utmp = 0; /* ensure we never double-cleanup */
}
示例5: remove_utmp_entry
static void remove_utmp_entry(main_server_st *s, struct proc_st* proc)
{
#ifdef HAVE_LIBUTIL
struct utmpx entry;
struct timespec tv;
if (s->config->use_utmp == 0)
return;
memset(&entry, 0, sizeof(entry));
entry.ut_type = DEAD_PROCESS;
if (proc->tun_lease.name[0] != 0)
snprintf(entry.ut_line, sizeof(entry.ut_line), "%s", proc->tun_lease.name);
entry.ut_pid = proc->pid;
setutxent();
pututxline(&entry);
endutxent();
#if defined(WTMPX_FILE)
gettime(&tv);
entry.ut_tv.tv_sec = tv.tv_sec;
entry.ut_tv.tv_usec = tv.tv_nsec / 1000;
updwtmpx(WTMPX_FILE, &entry);
#endif
return;
#endif
}
示例6: _add_utmp
static void _add_utmp(TE_Pty* pty, int spid) {
memset(&pty->ut_entry, 0, sizeof(pty->ut_entry) );
#if 0
pty->ut_entry.ut_type = USER_PROCESS;
pty->ut_entry.ut_pid = spid;
// strcpy(pty->ut_entry.ut_line, pty->ptyname+5);
// printf("ut name \"%s\" (%d)\n", pty->ut_entry.ut_user, getuid());
#ifdef __APPLE__
// strcpy(pty->ut_entry.ut_id, pty->ptyname+8);
strcpy(pty->ut_entry.ut_user, getpwuid(getuid())->pw_name);
gettimeofday(&pty->ut_entry.ut_tv, NULL);
setutxent();
pututxline(&pty->ut_entry);
endutxent();
#else
strcpy(pty->ut_entry.ut_host, getenv("DISPLAY"));
time_t tt;
time(&tt);
pty->ut_entry.ut_time = tt;
pty->ut_entry.ut_addr = 0;
setutent();
pututline(&pty->ut_entry);
endutent();
#endif
#endif
}
示例7: user_busy_utmp
static int user_busy_utmp (const char *name)
{
#ifdef USE_UTMPX
struct utmpx *utent;
setutxent ();
while ((utent = getutxent ()) != NULL)
#else /* !USE_UTMPX */
struct utmp *utent;
setutent ();
while ((utent = getutent ()) != NULL)
#endif /* !USE_UTMPX */
{
if (utent->ut_type != USER_PROCESS) {
continue;
}
if (strncmp (utent->ut_user, name, sizeof utent->ut_user) != 0) {
continue;
}
if (kill (utent->ut_pid, 0) != 0) {
continue;
}
fprintf (stderr,
_("%s: user %s is currently logged in\n"),
Prog, name);
return 1;
}
return 0;
}
示例8: main
int main(int argc, char *argv[]) {
struct utmpx *ut;
setutxent();
while ((ut = getutxent()) != NULL) {
printf("%-10s ", ut->ut_user);
printf("%-10s ", (ut->ut_type == EMPTY) ? "EMPTY" :
(ut->ut_type == RUN_LVL) ? "RUN_LVL" :
(ut->ut_type == BOOT_TIME) ? "BOOT_TIME":
(ut->ut_type == NEW_TIME) ? "NEW_TIME" :
(ut->ut_type == OLD_TIME) ? "OLD_TIME" :
(ut->ut_type == INIT_PROCESS) ? "INIT_PR" :
(ut->ut_type == LOGIN_PROCESS) ? "LOGIN_PR" :
(ut->ut_type == USER_PROCESS) ? "USER_PR" :
(ut->ut_type == DEAD_PROCESS) ? "DEAD_PR" : "?");
printf("%-7ld %-6s %-5s %-10s ",
(long) ut->ut_pid, ut->ut_line, ut->ut_id, ut->ut_host);
printf("%s", ctime((time_t *) &(ut->ut_tv.tv_sec)));
}
endutxent();
exit(EXIT_SUCCESS);
}
示例9: utmpx_mark_dead
void
utmpx_mark_dead(pid_t pid, int status, boolean_t blocking)
{
struct utmpx *up;
int logged = 0;
for (;;) {
int found = 0;
MUTEX_LOCK(&utmpx_lock);
setutxent();
while (up = getutxent()) {
if (up->ut_pid == pid) {
found = 1;
if (up->ut_type == DEAD_PROCESS) {
/*
* Cleaned up elsewhere.
*/
endutxent();
MUTEX_UNLOCK(&utmpx_lock);
return;
}
up->ut_type = DEAD_PROCESS;
up->ut_exit.e_termination = WTERMSIG(status);
up->ut_exit.e_exit = WEXITSTATUS(status);
(void) time(&up->ut_tv.tv_sec);
if (pututxline(up) != NULL) {
/*
* Now attempt to add to the end of the
* wtmp and wtmpx files. Do not create
* if they don't already exist.
*/
updwtmpx(WTMPX_FILE, up);
endutxent();
MUTEX_UNLOCK(&utmpx_lock);
return;
}
}
}
endutxent();
MUTEX_UNLOCK(&utmpx_lock);
if (!found || !blocking)
return;
if (!logged) {
log_framework(LOG_INFO, "retrying utmpx_dead on PID "
"%ld\n", pid);
logged++;
}
(void) sleep(1);
}
}
示例10: psutil_users
/*
* Return users currently connected on the system.
*/
static PyObject *
psutil_users(PyObject *self, PyObject *args) {
struct utmpx *ut;
PyObject *py_tuple = NULL;
PyObject *py_username = NULL;
PyObject *py_tty = NULL;
PyObject *py_hostname = NULL;
PyObject *py_user_proc = NULL;
PyObject *py_retlist = PyList_New(0);
if (py_retlist == NULL)
return NULL;
setutxent();
while (NULL != (ut = getutxent())) {
if (ut->ut_type == USER_PROCESS)
py_user_proc = Py_True;
else
py_user_proc = Py_False;
py_username = PyUnicode_DecodeFSDefault(ut->ut_user);
if (! py_username)
goto error;
py_tty = PyUnicode_DecodeFSDefault(ut->ut_line);
if (! py_tty)
goto error;
py_hostname = PyUnicode_DecodeFSDefault(ut->ut_host);
if (! py_hostname)
goto error;
py_tuple = Py_BuildValue(
"(OOOfOi)",
py_username, // username
py_tty, // tty
py_hostname, // hostname
(float)ut->ut_tv.tv_sec, // tstamp
py_user_proc, // (bool) user process
ut->ut_pid // process id
);
if (py_tuple == NULL)
goto error;
if (PyList_Append(py_retlist, py_tuple))
goto error;
Py_DECREF(py_username);
Py_DECREF(py_tty);
Py_DECREF(py_hostname);
Py_DECREF(py_tuple);
}
endutxent();
return py_retlist;
error:
Py_XDECREF(py_username);
Py_XDECREF(py_tty);
Py_XDECREF(py_hostname);
Py_XDECREF(py_tuple);
Py_DECREF(py_retlist);
endutxent();
return NULL;
}
示例11: who_main
int who_main(int argc UNUSED_PARAM, char **argv)
{
struct utmpx *ut;
unsigned opt;
int do_users = (ENABLE_USERS && (!ENABLE_WHO || applet_name[0] == 'u'));
const char *fmt = "%s";
opt_complementary = "=0";
opt = getopt32(argv, do_users ? "" : "aH");
if (opt & 2) // -H
puts("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST");
setutxent();
while ((ut = getutxent()) != NULL) {
if (ut->ut_user[0]
&& ((opt & 1) || ut->ut_type == USER_PROCESS)
) {
if (!do_users) {
char str6[6];
char name[sizeof("/dev/") + sizeof(ut->ut_line) + 1];
struct stat st;
time_t seconds;
str6[0] = '?';
str6[1] = '\0';
strcpy(name, "/dev/");
safe_strncpy(ut->ut_line[0] == '/' ? name : name + sizeof("/dev/")-1,
ut->ut_line,
sizeof(ut->ut_line)+1
);
if (stat(name, &st) == 0)
idle_string(str6, st.st_atime);
/* manpages say ut_tv.tv_sec *is* time_t,
* but some systems have it wrong */
seconds = ut->ut_tv.tv_sec;
/* How wide time field can be?
* "Nov 10 19:33:20": 15 chars
* "2010-11-10 19:33": 16 chars
*/
printf("%-15.*s %-15.*s %-7s %-16.16s %.*s\n",
(int)sizeof(ut->ut_user), ut->ut_user,
(int)sizeof(ut->ut_line), ut->ut_line,
str6,
ctime(&seconds) + 4,
(int)sizeof(ut->ut_host), ut->ut_host
);
} else {
printf(fmt, ut->ut_user);
fmt = " %s";
}
}
}
if (do_users)
bb_putchar('\n');
if (ENABLE_FEATURE_CLEAN_UP)
endutxent();
return EXIT_SUCCESS;
}
示例12: users_read
static int users_read (void)
{
#if HAVE_GETUTXENT
unsigned int users = 0;
struct utmpx *entry = NULL;
/* according to the *utent(3) man page none of the functions sets errno
in case of an error, so we cannot do any error-checking here */
setutxent();
while (NULL != (entry = getutxent())) {
if (USER_PROCESS == entry->ut_type) {
++users;
}
}
endutxent();
users_submit (users);
/* #endif HAVE_GETUTXENT */
#elif HAVE_GETUTENT
unsigned int users = 0;
struct utmp *entry = NULL;
/* according to the *utent(3) man page none of the functions sets errno
in case of an error, so we cannot do any error-checking here */
setutent();
while (NULL != (entry = getutent())) {
if (USER_PROCESS == entry->ut_type) {
++users;
}
}
endutent();
users_submit (users);
/* #endif HAVE_GETUTENT */
#elif HAVE_LIBSTATGRAB
sg_user_stats *us;
us = sg_get_user_stats ();
if (us == NULL)
return (-1);
users_submit ((gauge_t) us->num_entries);
/* #endif HAVE_LIBSTATGRAB */
#else
# error "No applicable input method."
#endif
return (0);
} /* int users_read */
示例13: utmpx_write_library
static int
utmpx_write_library(struct logininfo *li, struct utmpx *utx)
{
setutxent();
pututxline(utx);
# ifdef HAVE_ENDUTXENT
endutxent();
# endif
return 1;
}
示例14: login
void
login(struct utmp *ut)
{
struct utmpx ux;
getutmpx(ut, &ux);
/* ut_id will automatically be calculated in the call to pututxline() */
ux.ut_type |= UTMPX_AUTOFILL_MASK;
setutxent();
pututxline(&ux);
endutxent();
}
示例15: main
int
main (int argc, char **argv)
{
int users = -1;
int result = STATE_UNKNOWN;
char *perf;
struct utmpx *putmpx;
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
perf = strdup ("");
/* Parse extra opts if any */
argv = np_extra_opts (&argc, argv, progname);
if (process_arguments (argc, argv) == ERROR)
usage4 (_("Could not parse arguments"));
users = 0;
/* get currently logged users from utmpx */
setutxent ();
while ((putmpx = getutxent ()) != NULL)
if (putmpx->ut_type == USER_PROCESS)
users++;
endutxent ();
/* check the user count against warning and critical thresholds */
if (users > cusers)
result = STATE_CRITICAL;
else if (users > wusers)
result = STATE_WARNING;
else if (users >= 0)
result = STATE_OK;
if (result == STATE_UNKNOWN)
printf ("%s\n", _("Unable to read output"));
else {
asprintf (&perf, "%s", perfdata ("users", users, "",
TRUE, wusers,
TRUE, cusers,
TRUE, 0,
FALSE, 0));
printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result),
users, perf);
}
return result;
}