| 1 | /* Copyright (C) 1996-2017 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Ulrich Drepper <drepper@cygnus.com> |
| 4 | and Paul Janzen <pcj@primenet.com>, 1996. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <libc-lock.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <utmp.h> |
| 23 | |
| 24 | #include "utmp-private.h" |
| 25 | |
| 26 | |
| 27 | /* Functions defined here. */ |
| 28 | static int setutent_unknown (void); |
| 29 | static int getutent_r_unknown (struct utmp *buffer, struct utmp **result); |
| 30 | static int getutid_r_unknown (const struct utmp *line, struct utmp *buffer, |
| 31 | struct utmp **result); |
| 32 | static int getutline_r_unknown (const struct utmp *id, struct utmp *buffer, |
| 33 | struct utmp **result); |
| 34 | static struct utmp *pututline_unknown (const struct utmp *data); |
| 35 | static void endutent_unknown (void); |
| 36 | |
| 37 | /* Initial Jump table. */ |
| 38 | const struct utfuncs __libc_utmp_unknown_functions = |
| 39 | { |
| 40 | setutent_unknown, |
| 41 | getutent_r_unknown, |
| 42 | getutid_r_unknown, |
| 43 | getutline_r_unknown, |
| 44 | pututline_unknown, |
| 45 | endutent_unknown, |
| 46 | NULL |
| 47 | }; |
| 48 | |
| 49 | /* Currently selected backend. */ |
| 50 | const struct utfuncs *__libc_utmp_jump_table = &__libc_utmp_unknown_functions; |
| 51 | |
| 52 | /* We need to protect the opening of the file. */ |
| 53 | __libc_lock_define_initialized (, __libc_utmp_lock attribute_hidden) |
| 54 | |
| 55 | |
| 56 | static int |
| 57 | setutent_unknown (void) |
| 58 | { |
| 59 | int result; |
| 60 | |
| 61 | result = (*__libc_utmp_file_functions.setutent) (); |
| 62 | if (result) |
| 63 | __libc_utmp_jump_table = &__libc_utmp_file_functions; |
| 64 | |
| 65 | return result; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | static int |
| 70 | getutent_r_unknown (struct utmp *buffer, struct utmp **result) |
| 71 | { |
| 72 | /* The backend was not yet initialized. */ |
| 73 | if (setutent_unknown ()) |
| 74 | return (*__libc_utmp_jump_table->getutent_r) (buffer, result); |
| 75 | |
| 76 | /* Not available. */ |
| 77 | *result = NULL; |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | static int |
| 83 | getutid_r_unknown (const struct utmp *id, struct utmp *buffer, |
| 84 | struct utmp **result) |
| 85 | { |
| 86 | /* The backend was not yet initialized. */ |
| 87 | if (setutent_unknown ()) |
| 88 | return (*__libc_utmp_jump_table->getutid_r) (id, buffer, result); |
| 89 | |
| 90 | /* Not available. */ |
| 91 | *result = NULL; |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | static int |
| 97 | getutline_r_unknown (const struct utmp *line, struct utmp *buffer, |
| 98 | struct utmp **result) |
| 99 | { |
| 100 | /* The backend was not yet initialized. */ |
| 101 | if (setutent_unknown ()) |
| 102 | return (*__libc_utmp_jump_table->getutline_r) (line, buffer, result); |
| 103 | |
| 104 | /* Not available. */ |
| 105 | *result = NULL; |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | static struct utmp * |
| 111 | pututline_unknown (const struct utmp *data) |
| 112 | { |
| 113 | /* The backend was not yet initialized. */ |
| 114 | if (setutent_unknown ()) |
| 115 | return (*__libc_utmp_jump_table->pututline) (data); |
| 116 | |
| 117 | /* Not available. */ |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | static void |
| 123 | endutent_unknown (void) |
| 124 | { |
| 125 | /* Nothing to do. */ |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void |
| 130 | __setutent (void) |
| 131 | { |
| 132 | __libc_lock_lock (__libc_utmp_lock); |
| 133 | |
| 134 | (*__libc_utmp_jump_table->setutent) (); |
| 135 | |
| 136 | __libc_lock_unlock (__libc_utmp_lock); |
| 137 | } |
| 138 | weak_alias (__setutent, setutent) |
| 139 | |
| 140 | |
| 141 | int |
| 142 | __getutent_r (struct utmp *buffer, struct utmp **result) |
| 143 | { |
| 144 | int retval; |
| 145 | |
| 146 | __libc_lock_lock (__libc_utmp_lock); |
| 147 | |
| 148 | retval = (*__libc_utmp_jump_table->getutent_r) (buffer, result); |
| 149 | |
| 150 | __libc_lock_unlock (__libc_utmp_lock); |
| 151 | |
| 152 | return retval; |
| 153 | } |
| 154 | weak_alias (__getutent_r, getutent_r) |
| 155 | |
| 156 | |
| 157 | struct utmp * |
| 158 | __pututline (const struct utmp *data) |
| 159 | { |
| 160 | struct utmp *buffer; |
| 161 | |
| 162 | __libc_lock_lock (__libc_utmp_lock); |
| 163 | |
| 164 | buffer = (*__libc_utmp_jump_table->pututline) (data); |
| 165 | |
| 166 | __libc_lock_unlock (__libc_utmp_lock); |
| 167 | |
| 168 | return buffer; |
| 169 | } |
| 170 | weak_alias (__pututline, pututline) |
| 171 | |
| 172 | |
| 173 | void |
| 174 | __endutent (void) |
| 175 | { |
| 176 | __libc_lock_lock (__libc_utmp_lock); |
| 177 | |
| 178 | (*__libc_utmp_jump_table->endutent) (); |
| 179 | __libc_utmp_jump_table = &__libc_utmp_unknown_functions; |
| 180 | |
| 181 | __libc_lock_unlock (__libc_utmp_lock); |
| 182 | } |
| 183 | weak_alias (__endutent, endutent) |
| 184 | |