Back to index

glibc  2.9
utmp32.c
Go to the documentation of this file.
00001 /* Copyright (C) 2008 Free Software Foundation, Inc.
00002    Contributed by Andreas Krebbel <Andreas.Krebbel@de.ibm.com>.
00003    This file is part of the GNU C Library.
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 <sys/types.h>
00021 #include <utmp.h>
00022 #include <errno.h>
00023 #include <libc-symbols.h>
00024 
00025 #include "utmp32.h"
00026 #include "utmp-convert.h"
00027 
00028 /* Allocate a static buffer to be returned to the caller.  As well as
00029    with the existing version of these functions the caller has to be
00030    aware that the contents of this buffer will change with subsequent
00031    calls.  */
00032 #define ALLOCATE_UTMP32_OUT(OUT)                 \
00033   static struct utmp32 *OUT = NULL;                     \
00034                                                  \
00035   if (OUT == NULL)                               \
00036     {                                            \
00037       OUT = malloc (sizeof (struct utmp32));            \
00038       if (OUT == NULL)                                  \
00039        return NULL;                              \
00040     }
00041 
00042 /* Perform a lookup for a utmp entry matching FIELD using function
00043    FUNC.  FIELD is converted to a 64 bit utmp and the result is
00044    converted back to 32 bit utmp.  */
00045 #define ACCESS_UTMP_ENTRY(FUNC, FIELD)                  \
00046   struct utmp in64;                              \
00047   struct utmp *out64;                                   \
00048   ALLOCATE_UTMP32_OUT (out32);                          \
00049                                                  \
00050   utmp_convert32to64 (FIELD, &in64);                    \
00051   out64 = FUNC (&in64);                                 \
00052                                                  \
00053   if (out64 == NULL)                             \
00054     return NULL;                                 \
00055                                                  \
00056   utmp_convert64to32 (out64, out32);                    \
00057                                                  \
00058   return out32;
00059 
00060 /* Search forward from the current point in the utmp file until the
00061    next entry with a ut_type matching ID->ut_type.  */
00062 struct utmp32 *
00063 getutid32 (const struct utmp32 *id)
00064 {
00065   ACCESS_UTMP_ENTRY (getutid, id)
00066 }
00067 symbol_version (getutid32, getutid, GLIBC_2.0);
00068 
00069 /* Search forward from the current point in the utmp file until the
00070    next entry with a ut_line matching LINE->ut_line.  */
00071 struct utmp32 *
00072 getutline32 (const struct utmp32 *line)
00073 {
00074   ACCESS_UTMP_ENTRY (getutline, line)
00075 }
00076 symbol_version (getutline32, getutline, GLIBC_2.0);
00077 
00078 /* Write out entry pointed to by UTMP_PTR into the utmp file.  */
00079 struct utmp32 *
00080 pututline32 (const struct utmp32 *utmp_ptr)
00081 {
00082   ACCESS_UTMP_ENTRY (pututline, utmp_ptr)
00083 }
00084 symbol_version (pututline32, pututline, GLIBC_2.0);
00085 
00086 /* Read next entry from a utmp-like file.  */
00087 struct utmp32 *
00088 getutent32 (void)
00089 {
00090   struct utmp *out64;
00091   ALLOCATE_UTMP32_OUT (out32);
00092 
00093   out64 = getutent ();
00094   if (!out64)
00095     return NULL;
00096 
00097   utmp_convert64to32 (out64, out32);
00098   return out32;
00099 }
00100 symbol_version (getutent32, getutent, GLIBC_2.0);
00101 
00102 /* Reentrant versions of the file for handling utmp files.  */
00103 
00104 int
00105 getutent32_r (struct utmp32 *buffer, struct utmp32 **result)
00106 {
00107   struct utmp out64;
00108   struct utmp *out64p;
00109   int ret;
00110 
00111   ret = getutent_r (&out64, &out64p);
00112   if (ret == -1)
00113     {
00114       *result = NULL;
00115       return -1;
00116     }
00117 
00118   utmp_convert64to32 (out64p, buffer);
00119   *result = buffer;
00120 
00121   return 0;
00122 }
00123 symbol_version (getutent32_r, getutent_r, GLIBC_2.0);
00124 
00125 int
00126 getutid32_r (const struct utmp32 *id, struct utmp32 *buffer,
00127               struct utmp32 **result)
00128 {
00129   struct utmp in64;
00130   struct utmp out64;
00131   struct utmp *out64p;
00132   int ret;
00133 
00134   utmp_convert32to64 (id, &in64);
00135 
00136   ret = getutid_r (&in64, &out64, &out64p);
00137   if (ret == -1)
00138     {
00139       *result = NULL;
00140       return -1;
00141     }
00142 
00143   utmp_convert64to32 (out64p, buffer);
00144   *result = buffer;
00145 
00146   return 0;
00147 }
00148 symbol_version (getutid32_r, getutid_r, GLIBC_2.0);
00149 
00150 int
00151 getutline32_r (const struct utmp32 *line,
00152                struct utmp32 *buffer, struct utmp32 **result)
00153 {
00154   struct utmp in64;
00155   struct utmp out64;
00156   struct utmp *out64p;
00157   int ret;
00158 
00159   utmp_convert32to64 (line, &in64);
00160 
00161   ret = getutline_r (&in64, &out64, &out64p);
00162   if (ret == -1)
00163     {
00164       *result = NULL;
00165       return -1;
00166     }
00167 
00168   utmp_convert64to32 (out64p, buffer);
00169   *result = buffer;
00170 
00171   return 0;
00172 
00173 }
00174 symbol_version (getutline32_r, getutline_r, GLIBC_2.0);
00175 
00176 /* Append entry UTMP to the wtmp-like file WTMP_FILE.  */
00177 void
00178 updwtmp32 (const char *wtmp_file, const struct utmp32 *utmp)
00179 {
00180   struct utmp in32;
00181 
00182   utmp_convert32to64 (utmp, &in32);
00183   updwtmp (wtmp_file, &in32);
00184 }
00185 symbol_version (updwtmp32, updwtmp, GLIBC_2.0);