* libmisc/utmp.c: Avoid implicit conversion of pointers / chars to

booleans.
	* libmisc/utmp.c: Add brackets and parenthesis.
	* libmisc/utmp.c: Exit with the EXIT_FAILURE status code in case
	of failure.
	* libmisc/utmp.c: Avoid assignments in comparisons.
	* lib/prototypes.h, libmisc/utmp.c: Change setutmp()'s prototype
	to return an int because pututline() and pututxline() may fail.
This commit is contained in:
nekral-guest 2008-06-17 21:13:30 +00:00
parent 9320075030
commit 5f9226b14b
3 changed files with 85 additions and 44 deletions

View File

@ -1,3 +1,14 @@
2008-06-17 Nicolas François <nicolas.francois@centraliens.net>
* libmisc/utmp.c: Avoid implicit conversion of pointers / chars to
booleans.
* libmisc/utmp.c: Add brackets and parenthesis.
* libmisc/utmp.c: Exit with the EXIT_FAILURE status code in case
of failure.
* libmisc/utmp.c: Avoid assignments in comparisons.
* lib/prototypes.h, libmisc/utmp.c: Change setutmp()'s prototype
to return an int because pututline() and pututxline() may fail.
2008-06-17 Nicolas François <nicolas.francois@centraliens.net> 2008-06-17 Nicolas François <nicolas.francois@centraliens.net>
* libmisc/audit_help.c: Added one string for translation. * libmisc/audit_help.c: Added one string for translation.

View File

@ -262,7 +262,7 @@ extern int set_filesize_limit (int blocks);
/* utmp.c */ /* utmp.c */
extern void checkutmp (bool picky); extern void checkutmp (bool picky);
extern void setutmp (const char *, const char *, const char *); extern int setutmp (const char *, const char *, const char *);
/* valid.c */ /* valid.c */
extern bool valid (const char *, const struct passwd *); extern bool valid (const char *, const struct passwd *);

View File

@ -81,27 +81,31 @@ void checkutmp (bool picky)
setutent (); setutent ();
/* First, try to find a valid utmp entry for this process. */ /* First, try to find a valid utmp entry for this process. */
while ((ut = getutent ())) while ((ut = getutent ()) != NULL)
if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] && if ( (ut->ut_pid == pid)
(ut->ut_type == LOGIN_PROCESS && ('\0' != ut->ut_line[0])
|| ut->ut_type == USER_PROCESS)) && ('\0' != ut->ut_id[0])
&& ( (LOGIN_PROCESS == ut->ut_type)
|| (USER_PROCESS == ut->ut_type))) {
break; break;
}
/* If there is one, just use it, otherwise create a new one. */ /* If there is one, just use it, otherwise create a new one. */
if (ut) { if (NULL != ut) {
utent = *ut; utent = *ut;
} else { } else {
if (picky) { if (picky) {
puts (NO_UTENT); (void) puts (NO_UTENT);
exit (1); exit (EXIT_FAILURE);
} }
line = ttyname (0); line = ttyname (0);
if (!line) { if (NULL == line) {
puts (NO_TTY); (void) puts (NO_TTY);
exit (1); exit (EXIT_FAILURE);
} }
if (strncmp (line, "/dev/", 5) == 0) if (strncmp (line, "/dev/", 5) == 0) {
line += 5; line += 5;
}
memset ((void *) &utent, 0, sizeof utent); memset ((void *) &utent, 0, sizeof utent);
utent.ut_type = LOGIN_PROCESS; utent.ut_type = LOGIN_PROCESS;
utent.ut_pid = pid; utent.ut_pid = pid;
@ -132,28 +136,34 @@ void checkutmp (bool picky)
if (picky) { if (picky) {
#if HAVE_UTMPX_H #if HAVE_UTMPX_H
while ((utx = getutxent ())) while ((utx = getutxent ()) != NULL) {
if (utx->ut_pid == pid) if (utx->ut_pid == pid) {
break; break;
}
}
if (utx) if (NULL != utx) {
utxent = *utx; utxent = *utx;
}
#endif #endif
while ((ut = getutent ())) while ((ut = getutent ()) != NULL) {
if (ut->ut_pid == pid) if (ut->ut_pid == pid) {
break; break;
}
}
if (ut) if (NULL != ut) {
utent = *ut; utent = *ut;
}
#if HAVE_UTMPX_H #if HAVE_UTMPX_H
endutxent (); endutxent ();
#endif #endif
endutent (); endutent ();
if (!ut) { if (NULL == ut) {
puts (NO_UTENT); (void) puts (NO_UTENT);
exit (1); exit (EXIT_FAILURE);
} }
#ifndef UNIXPC #ifndef UNIXPC
@ -167,28 +177,34 @@ void checkutmp (bool picky)
if (utent.ut_line[0] == '\0') if (utent.ut_line[0] == '\0')
#endif /* !UNIXPC */ #endif /* !UNIXPC */
{ {
if (!(line = ttyname (0))) { line = ttyname (0);
puts (NO_TTY); if (NULL == line) {
exit (1); (void) puts (NO_TTY);
exit (EXIT_FAILURE);
} }
if (strncmp (line, "/dev/", 5) == 0) if (strncmp (line, "/dev/", 5) == 0) {
line += 5; line += 5;
}
strncpy (utent.ut_line, line, sizeof utent.ut_line); strncpy (utent.ut_line, line, sizeof utent.ut_line);
#if HAVE_UTMPX_H #if HAVE_UTMPX_H
strncpy (utxent.ut_line, line, sizeof utxent.ut_line); strncpy (utxent.ut_line, line, sizeof utxent.ut_line);
#endif #endif
} }
} else { } else {
if (!(line = ttyname (0))) { line = ttyname (0);
puts (NO_TTY); if (NULL == line) {
exit (1); (void) puts (NO_TTY);
exit (EXIT_FAILURE);
} }
if (strncmp (line, "/dev/", 5) == 0) if (strncmp (line, "/dev/", 5) == 0) {
line += 5; line += 5;
}
strncpy (utent.ut_line, line, sizeof utent.ut_line); strncpy (utent.ut_line, line, sizeof utent.ut_line);
if ((ut = getutline (&utent))) ut = getutline (&utent);
if (NULL != ut) {
strncpy (utent.ut_id, ut->ut_id, sizeof ut->ut_id); strncpy (utent.ut_id, ut->ut_id, sizeof ut->ut_id);
}
strcpy (utent.ut_user, "LOGIN"); strcpy (utent.ut_user, "LOGIN");
utent.ut_pid = getpid (); utent.ut_pid = getpid ();
@ -196,15 +212,17 @@ void checkutmp (bool picky)
utent.ut_time = time (NULL); utent.ut_time = time (NULL);
#if HAVE_UTMPX_H #if HAVE_UTMPX_H
strncpy (utxent.ut_line, line, sizeof utxent.ut_line); strncpy (utxent.ut_line, line, sizeof utxent.ut_line);
if ((utx = getutxline (&utxent))) utx = getutxline (&utxent);
if (NULL != utx) {
strncpy (utxent.ut_id, utx->ut_id, sizeof utxent.ut_id); strncpy (utxent.ut_id, utx->ut_id, sizeof utxent.ut_id);
}
strcpy (utxent.ut_user, "LOGIN"); strcpy (utxent.ut_user, "LOGIN");
utxent.ut_pid = utent.ut_pid; utxent.ut_pid = utent.ut_pid;
utxent.ut_type = utent.ut_type; utxent.ut_type = utent.ut_type;
if (sizeof (utxent.ut_tv) == sizeof (struct timeval)) if (sizeof (utxent.ut_tv) == sizeof (struct timeval)) {
gettimeofday ((struct timeval *) &utxent.ut_tv, NULL); gettimeofday ((struct timeval *) &utxent.ut_tv, NULL);
else { } else {
struct timeval tv; struct timeval tv;
gettimeofday (&tv, NULL); gettimeofday (&tv, NULL);
@ -262,34 +280,42 @@ static void updwtmpx (const char *filename, const struct utmpx *utx)
#if defined(__linux__) /* XXX */ #if defined(__linux__) /* XXX */
void setutmp (const char *name, const char *line, const char *host) int setutmp (const char *name, const char *line, const char *host)
{ {
int err = 0;
utent.ut_type = USER_PROCESS; utent.ut_type = USER_PROCESS;
strncpy (utent.ut_user, name, sizeof utent.ut_user); strncpy (utent.ut_user, name, sizeof utent.ut_user);
utent.ut_time = time (NULL); utent.ut_time = time (NULL);
/* other fields already filled in by checkutmp above */ /* other fields already filled in by checkutmp above */
setutent (); setutent ();
pututline (&utent); if (pututline (&utent) == NULL) {
err = 1;
}
endutent (); endutent ();
updwtmp (_WTMP_FILE, &utent); updwtmp (_WTMP_FILE, &utent);
return err;
} }
#elif HAVE_UTMPX_H #elif HAVE_UTMPX_H
void setutmp (const char *name, const char *line, const char *host) int setutmp (const char *name, const char *line, const char *host)
{ {
struct utmp *utmp, utline; struct utmp *utmp, utline;
struct utmpx *utmpx, utxline; struct utmpx *utmpx, utxline;
pid_t pid = getpid (); pid_t pid = getpid ();
bool found_utmpx = false, found_utmp = false; bool found_utmpx = false;
bool found_utmp = false;
int err = 0;
/* /*
* The canonical device name doesn't include "/dev/"; skip it * The canonical device name doesn't include "/dev/"; skip it
* if it is already there. * if it is already there.
*/ */
if (strncmp (line, "/dev/", 5) == 0) if (strncmp (line, "/dev/", 5) == 0) {
line += 5; line += 5;
}
/* /*
* Update utmpx. We create an empty entry in case there is * Update utmpx. We create an empty entry in case there is
@ -354,9 +380,9 @@ void setutmp (const char *name, const char *line, const char *host)
utline.ut_type = utxline.ut_type = USER_PROCESS; utline.ut_type = utxline.ut_type = USER_PROCESS;
if (sizeof (utxline.ut_tv) == sizeof (struct timeval)) if (sizeof (utxline.ut_tv) == sizeof (struct timeval)) {
gettimeofday ((struct timeval *) &utxline.ut_tv, NULL); gettimeofday ((struct timeval *) &utxline.ut_tv, NULL);
else { } else {
struct timeval tv; struct timeval tv;
gettimeofday (&tv, NULL); gettimeofday (&tv, NULL);
@ -365,17 +391,21 @@ void setutmp (const char *name, const char *line, const char *host)
} }
utline.ut_time = utxline.ut_tv.tv_sec; utline.ut_time = utxline.ut_tv.tv_sec;
strncpy (utxline.ut_host, host ? host : "", sizeof utxline.ut_host); strncpy (utxline.ut_host, (NULL != host) ? host : "",
sizeof utxline.ut_host);
pututxline (&utxline); if ( (pututxline (&utxline) == NULL)
pututline (&utline); || (pututline (&utline) == NULL)) {
/* TODO: log failures */ err = 1;
}
updwtmpx (_WTMP_FILE "x", &utxline); updwtmpx (_WTMP_FILE "x", &utxline);
updwtmp (_WTMP_FILE, &utline); updwtmp (_WTMP_FILE, &utline);
utxent = utxline; utxent = utxline;
utent = utline; utent = utline;
return err;
} }
#endif #endif