Back to index

glibc  2.9
login.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996, 1997, 1998, 2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <assert.h>
00021 #include <errno.h>
00022 #include <limits.h>
00023 #include <string.h>
00024 #include <unistd.h>
00025 #include <stdlib.h>
00026 #include <utmp.h>
00027 
00028 
00029 /* Return the result of ttyname in the buffer pointed to by TTY, which should
00030    be of length BUF_LEN.  If it is too long to fit in this buffer, a
00031    sufficiently long buffer is allocated using malloc, and returned in TTY.
00032    0 is returned upon success, -1 otherwise.  */
00033 static int
00034 tty_name (int fd, char **tty, size_t buf_len)
00035 {
00036   int rv;                   /* Return value.  0 = success.  */
00037   char *buf = *tty;         /* Buffer for ttyname, initially the user's. */
00038 
00039   for (;;)
00040     {
00041       char *new_buf;
00042 
00043       if (buf_len)
00044        {
00045          rv = ttyname_r (fd, buf, buf_len);
00046 
00047          if (rv != 0 || memchr (buf, '\0', buf_len))
00048            /* We either got an error, or we succeeded and the
00049               returned name fit in the buffer.  */
00050            break;
00051 
00052          /* Try again with a longer buffer.  */
00053          buf_len += buf_len;       /* Double it */
00054        }
00055       else
00056        /* No initial buffer; start out by mallocing one.  */
00057        buf_len = 128;              /* First time guess.  */
00058 
00059       if (buf != *tty)
00060        /* We've already malloced another buffer at least once.  */
00061        new_buf = realloc (buf, buf_len);
00062       else
00063        new_buf = malloc (buf_len);
00064       if (! new_buf)
00065        {
00066          rv = -1;
00067          __set_errno (ENOMEM);
00068          break;
00069        }
00070       buf = new_buf;
00071     }
00072 
00073   if (rv == 0)
00074     *tty = buf;             /* Return buffer to the user.  */
00075   else if (buf != *tty)
00076     free (buf);             /* Free what we malloced when returning an error.  */
00077 
00078   return rv;
00079 }
00080 
00081 void
00082 login (const struct utmp *ut)
00083 {
00084 #ifdef PATH_MAX
00085   char _tty[PATH_MAX + UT_LINESIZE];
00086 #else
00087   char _tty[512 + UT_LINESIZE];
00088 #endif
00089   char *tty = _tty;
00090   int found_tty;
00091   const char *ttyp;
00092   struct utmp copy = *ut;
00093 
00094   /* Fill in those fields we supply.  */
00095 #if _HAVE_UT_TYPE - 0
00096   copy.ut_type = USER_PROCESS;
00097 #endif
00098 #if _HAVE_UT_PID - 0
00099   copy.ut_pid = getpid ();
00100 #endif
00101 
00102   /* Seek tty.  */
00103   found_tty = tty_name (STDIN_FILENO, &tty, sizeof (_tty));
00104   if (found_tty < 0)
00105     found_tty = tty_name (STDOUT_FILENO, &tty, sizeof (_tty));
00106   if (found_tty < 0)
00107     found_tty = tty_name (STDERR_FILENO, &tty, sizeof (_tty));
00108 
00109   if (found_tty >= 0)
00110     {
00111       /* We only want to insert the name of the tty without path.
00112         But take care of name like /dev/pts/3.  */
00113       if (strncmp (tty, "/dev/", 5) == 0)
00114        ttyp = tty + 5;             /* Skip the "/dev/".  */
00115       else
00116        ttyp = basename (tty);
00117 
00118       /* Position to record for this tty.  */
00119       strncpy (copy.ut_line, ttyp, UT_LINESIZE);
00120 
00121       /* Tell that we want to use the UTMP file.  */
00122       if (utmpname (_PATH_UTMP) == 0)
00123        {
00124          /* Open UTMP file.  */
00125          setutent ();
00126 
00127          /* Write the entry.  */
00128          pututline (&copy);
00129 
00130          /* Close UTMP file.  */
00131          endutent ();
00132        }
00133 
00134       if (tty != _tty)
00135        free (tty);          /* Free buffer malloced by tty_name.  */
00136     }
00137   else
00138     /* We provide a default value so that the output does not contain
00139        an random bytes in this field.  */
00140     strncpy (copy.ut_line, "???", UT_LINESIZE);
00141 
00142   /* Update the WTMP file.  Here we have to add a new entry.  */
00143   updwtmp (_PATH_WTMP, &copy);
00144 }