File: error.c

package info (click to toggle)
libuser 1%3A0.64~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,092 kB
  • sloc: ansic: 16,599; python: 2,555; sh: 877; yacc: 782; makefile: 235; xml: 106
file content (244 lines) | stat: -rw-r--r-- 6,555 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* Copyright (C) 2000-2002 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <config.h>
#include <sys/types.h>
#include <errno.h>
#include <execinfo.h>
#include <libintl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "user.h"
#include "user_private.h"

/**
 * SECTION:error
 * @short_description: Functions for allocating and manipulating #lu_error
 * structures.
 * @include: libuser/error.h
 *
 * <filename>error.h</filename> includes declarations for allocating and
 * manipulating #lu_error structures.  These structures hold error and status
 * information passed between libuser, its modules, and applications.
 *
 * A struct #lu_error contains an error code and a human-readable, possibly
 * translated error string.  The error string uses the encoding specified by
 * the %LC_CTYPE locale category.
 */

/**
 * lu_strerror:
 * @error: An error
 *
 * Converts an #lu_error structure to a string describing the error.  If the
 * @error->string is %NULL, returns a text representation of @error->code.
 *
 * Returns: An error string valid at least until @error is freed.
 */
const char *
lu_strerror(struct lu_error *error)
{
	if (error != NULL) {
		if (error->string != NULL) {
			return error->string;
		}
		switch (error->code) {
			case lu_success:
				return _("success");
			case lu_warning_config_disabled:
				return _("module disabled by configuration");
			case lu_error_generic:
				return _("generic error");
			case lu_error_privilege:
				return _("not enough privileges");
			case lu_error_access_denied:
				return _("access denied");
			case lu_error_name_bad:
				return _("bad user/group name");
			case lu_error_id_bad:
				return _("bad user/group id");
			case lu_error_name_used:
				return _("user/group name in use");
			case lu_error_id_used:
				return _("user/group id in use");
			case lu_error_terminal:
				return _("error manipulating terminal attributes");
			case lu_error_open:
				return _("error opening file");
			case lu_error_lock:
				return _("error locking file");
			case lu_error_stat:
				return _("error statting file");
			case lu_error_read:
				return _("error reading file");
			case lu_error_write:
				return _("error writing to file");
			case lu_error_search:
				return _("data not found in file");
			case lu_error_init:
				return _("internal initialization error");
			case lu_error_module_load:
				return _("error loading module");
			case lu_error_module_sym:
				return _("error resolving symbol in module");
			case lu_error_module_version:
				return _("library/module version mismatch");
			case lu_error_unlock_empty:
				return _("unlocking would make the password "
					 "field empty");
			case lu_error_invalid_attribute_value:
				return _("invalid attribute value");
			case lu_error_invalid_module_combination:
				return _("invalid module combination");
			case lu_error_homedir_not_owned:
				return _("user's home directory not owned by "
					 "them");
			default:
				break;
		}
	}
	return _("unknown error");
}

/**
 * lu_error_is_success:
 * @status: An error code
 *
 * Check if the error code held by an error structure is a success code.
 *
 * Returns: a #gboolean indicating whether or not the error is a success code.
 */
gboolean
lu_error_is_success(enum lu_status status)
{
	switch (status) {
		case lu_success:
			return TRUE;
		default:
			return FALSE;
	}
}

/**
 * lu_error_is_warning:
 * @status: An error code
 *
 * Check if the error code held by an error structure is a warning code.
 *
 * Returns: a #gboolean indicating whether or not the error is a warning code.
 */
gboolean
lu_error_is_warning(enum lu_status status)
{
	switch (status) {
		case lu_warning_config_disabled:
			return TRUE;
		default:
			return FALSE;
	}
}

/**
 * lu_error_is_error:
 * @status: An error code
 *
 * Check if the error code held by an error structure is an error code.
 *
 * Returns: a #gboolean indicating whether or not the error is an error code.
 */
gboolean
lu_error_is_error(enum lu_status status)
{
	switch (status) {
		case lu_error_generic:
		case lu_error_privilege:
		case lu_error_access_denied:
		case lu_error_name_bad:
		case lu_error_id_bad:
		case lu_error_name_used:
		case lu_error_id_used:
		case lu_error_terminal:
		case lu_error_open:
		case lu_error_lock:
		case lu_error_stat:
		case lu_error_read:
		case lu_error_write:
		case lu_error_search:
		case lu_error_init:
		case lu_error_module_load:
		case lu_error_module_sym:
		case lu_error_module_version:
		case lu_error_unlock_empty:
		case lu_error_invalid_attribute_value:
		case lu_error_invalid_module_combination:
		case lu_error_homedir_not_owned:
			return TRUE;
		default:
			return FALSE;
	}
}

/**
 * lu_error_new:
 * @error: A pointer to a struct #lu_error * which will hold the newly-created
 * error structure. It must point to #NULL before calling this function.
 * @code: An error code
 * @fmt: Format string describing the error. If #NULL, a default string is used.
 * @...: Arguments for @fmt, if necessary
 *
 * Creates a new #lu_error structure.
 */
void
lu_error_new(struct lu_error **error, enum lu_status code,
	     const char *fmt, ...)
{
	if (error != NULL) {
		struct lu_error *ret;

		g_assert(*error == NULL);
		ret = g_malloc0(sizeof(struct lu_error));
		ret->code = code;
		if (fmt != NULL) {
			va_list args;

			va_start(args, fmt);
			ret->string = g_strdup_vprintf(fmt, args);
			va_end(args);
		} else
			ret->string = g_strdup(lu_strerror(ret));
		*error = ret;
	}
}

/**
 * lu_error_free:
 * @error: A pointer to a pointer to the structure to be freed.  The pointer is
 * set to %NULL after the error is freed.
 *
 * Frees an #lu_error structure.
 */
void
lu_error_free(struct lu_error **error)
{
	if (error != NULL) {
		g_free((*error)->string);
		memset(*error, 0, sizeof(**error));
		g_free(*error);
		*error = NULL;
	}
}