1/* Copyright (C) 1996-2018 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. */
28static int setutent_unknown (void);
29static int getutent_r_unknown (struct utmp *buffer, struct utmp **result);
30static int getutid_r_unknown (const struct utmp *line, struct utmp *buffer,
31 struct utmp **result);
32static int getutline_r_unknown (const struct utmp *id, struct utmp *buffer,
33 struct utmp **result);
34static struct utmp *pututline_unknown (const struct utmp *data);
35static void endutent_unknown (void);
36
37/* Initial Jump table. */
38const 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. */
50const 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
56static int
57setutent_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
69static int
70getutent_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
82static int
83getutid_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
96static int
97getutline_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
110static struct utmp *
111pututline_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
122static void
123endutent_unknown (void)
124{
125 /* Nothing to do. */
126}
127
128
129void
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}
138weak_alias (__setutent, setutent)
139
140
141int
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}
154libc_hidden_def (__getutent_r)
155weak_alias (__getutent_r, getutent_r)
156
157
158struct utmp *
159__pututline (const struct utmp *data)
160{
161 struct utmp *buffer;
162
163 __libc_lock_lock (__libc_utmp_lock);
164
165 buffer = (*__libc_utmp_jump_table->pututline) (data);
166
167 __libc_lock_unlock (__libc_utmp_lock);
168
169 return buffer;
170}
171libc_hidden_def (__pututline)
172weak_alias (__pututline, pututline)
173
174
175void
176__endutent (void)
177{
178 __libc_lock_lock (__libc_utmp_lock);
179
180 (*__libc_utmp_jump_table->endutent) ();
181 __libc_utmp_jump_table = &__libc_utmp_unknown_functions;
182
183 __libc_lock_unlock (__libc_utmp_lock);
184}
185weak_alias (__endutent, endutent)
186