Skip to content

Commit 2b22a69

Browse files
committed
libsubid: don't print error messages on stderr by default
Closes #325 Add a new subid_init() function which can be used to specify the stream on which error messages should be printed. (If you want to get fancy you can redirect that to memory :) If subid_init() is not called, use stderr. If NULL is passed, then /dev/null will be used. This patch also fixes up the 'Prog', which previously had to be defined by any program linking against libsubid. Now, by default in libsubid it will show (subid). Once subid_init() is called, it will use the first variable passed to subid_init(). Signed-off-by: Serge Hallyn <[email protected]>
1 parent 3ac8d97 commit 2b22a69

75 files changed

Lines changed: 311 additions & 191 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎lib/commonio.c‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static int do_lock_file (const char *file, const char *lock, bool log)
144144
fd = open (file, O_CREAT | O_TRUNC | O_WRONLY, 0600);
145145
if (-1 == fd) {
146146
if (log) {
147-
(void) fprintf (stderr,
147+
(void) fprintf (shadow_logfd,
148148
"%s: %s: %s\n",
149149
Prog, file, strerror (errno));
150150
}
@@ -156,7 +156,7 @@ static int do_lock_file (const char *file, const char *lock, bool log)
156156
len = (ssize_t) strlen (buf) + 1;
157157
if (write (fd, buf, (size_t) len) != len) {
158158
if (log) {
159-
(void) fprintf (stderr,
159+
(void) fprintf (shadow_logfd,
160160
"%s: %s file write error: %s\n",
161161
Prog, file, strerror (errno));
162162
}
@@ -166,7 +166,7 @@ static int do_lock_file (const char *file, const char *lock, bool log)
166166
}
167167
if (fdatasync (fd) == -1) {
168168
if (log) {
169-
(void) fprintf (stderr,
169+
(void) fprintf (shadow_logfd,
170170
"%s: %s file sync error: %s\n",
171171
Prog, file, strerror (errno));
172172
}
@@ -179,7 +179,7 @@ static int do_lock_file (const char *file, const char *lock, bool log)
179179
if (link (file, lock) == 0) {
180180
retval = check_link_count (file);
181181
if ((0==retval) && log) {
182-
(void) fprintf (stderr,
182+
(void) fprintf (shadow_logfd,
183183
"%s: %s: lock file already used\n",
184184
Prog, file);
185185
}
@@ -190,7 +190,7 @@ static int do_lock_file (const char *file, const char *lock, bool log)
190190
fd = open (lock, O_RDWR);
191191
if (-1 == fd) {
192192
if (log) {
193-
(void) fprintf (stderr,
193+
(void) fprintf (shadow_logfd,
194194
"%s: %s: %s\n",
195195
Prog, lock, strerror (errno));
196196
}
@@ -202,7 +202,7 @@ static int do_lock_file (const char *file, const char *lock, bool log)
202202
close (fd);
203203
if (len <= 0) {
204204
if (log) {
205-
(void) fprintf (stderr,
205+
(void) fprintf (shadow_logfd,
206206
"%s: existing lock file %s without a PID\n",
207207
Prog, lock);
208208
}
@@ -213,7 +213,7 @@ static int do_lock_file (const char *file, const char *lock, bool log)
213213
buf[len] = '\0';
214214
if (get_pid (buf, &pid) == 0) {
215215
if (log) {
216-
(void) fprintf (stderr,
216+
(void) fprintf (shadow_logfd,
217217
"%s: existing lock file %s with an invalid PID '%s'\n",
218218
Prog, lock, buf);
219219
}
@@ -223,7 +223,7 @@ static int do_lock_file (const char *file, const char *lock, bool log)
223223
}
224224
if (kill (pid, 0) == 0) {
225225
if (log) {
226-
(void) fprintf (stderr,
226+
(void) fprintf (shadow_logfd,
227227
"%s: lock %s already used by PID %lu\n",
228228
Prog, lock, (unsigned long) pid);
229229
}
@@ -233,7 +233,7 @@ static int do_lock_file (const char *file, const char *lock, bool log)
233233
}
234234
if (unlink (lock) != 0) {
235235
if (log) {
236-
(void) fprintf (stderr,
236+
(void) fprintf (shadow_logfd,
237237
"%s: cannot get lock %s: %s\n",
238238
Prog, lock, strerror (errno));
239239
}
@@ -245,13 +245,13 @@ static int do_lock_file (const char *file, const char *lock, bool log)
245245
if (link (file, lock) == 0) {
246246
retval = check_link_count (file);
247247
if ((0==retval) && log) {
248-
(void) fprintf (stderr,
248+
(void) fprintf (shadow_logfd,
249249
"%s: %s: lock file already used\n",
250250
Prog, file);
251251
}
252252
} else {
253253
if (log) {
254-
(void) fprintf (stderr,
254+
(void) fprintf (shadow_logfd,
255255
"%s: cannot get lock %s: %s\n",
256256
Prog, lock, strerror (errno));
257257
}
@@ -442,7 +442,7 @@ int commonio_lock (struct commonio_db *db)
442442
if (0 == lock_count) {
443443
if (lckpwdf () == -1) {
444444
if (geteuid () != 0) {
445-
(void) fprintf (stderr,
445+
(void) fprintf (shadow_logfd,
446446
"%s: Permission denied.\n",
447447
Prog);
448448
}
@@ -478,7 +478,7 @@ int commonio_lock (struct commonio_db *db)
478478
}
479479
/* no unnecessary retries on "permission denied" errors */
480480
if (geteuid () != 0) {
481-
(void) fprintf (stderr, "%s: Permission denied.\n",
481+
(void) fprintf (shadow_logfd, "%s: Permission denied.\n",
482482
Prog);
483483
return 0;
484484
}
@@ -1109,7 +1109,7 @@ int commonio_update (struct commonio_db *db, const void *eptr)
11091109
p = find_entry_by_name (db, db->ops->getname (eptr));
11101110
if (NULL != p) {
11111111
if (next_entry_by_name (db, p->next, db->ops->getname (eptr)) != NULL) {
1112-
fprintf (stderr, _("Multiple entries named '%s' in %s. Please fix this with pwck or grpck.\n"), db->ops->getname (eptr), db->filename);
1112+
fprintf (shadow_logfd, _("Multiple entries named '%s' in %s. Please fix this with pwck or grpck.\n"), db->ops->getname (eptr), db->filename);
11131113
db->ops->free (nentry);
11141114
return 0;
11151115
}
@@ -1214,7 +1214,7 @@ int commonio_remove (struct commonio_db *db, const char *name)
12141214
return 0;
12151215
}
12161216
if (next_entry_by_name (db, p->next, name) != NULL) {
1217-
fprintf (stderr, _("Multiple entries named '%s' in %s. Please fix this with pwck or grpck.\n"), name, db->filename);
1217+
fprintf (shadow_logfd, _("Multiple entries named '%s' in %s. Please fix this with pwck or grpck.\n"), name, db->filename);
12181218
return 0;
12191219
}
12201220

‎lib/encrypt.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
method = &nummethod[0];
8585
}
8686
}
87-
(void) fprintf (stderr,
87+
(void) fprintf (shadow_logfd,
8888
_("crypt method not supported by libcrypt? (%s)\n"),
8989
method);
9090
exit (EXIT_FAILURE);

‎lib/getdef.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ int getdef_num (const char *item, int dflt)
266266
if ( (getlong (d->value, &val) == 0)
267267
|| (val > INT_MAX)
268268
|| (val < INT_MIN)) {
269-
fprintf (stderr,
269+
fprintf (shadow_logfd,
270270
_("configuration error - cannot parse %s value: '%s'"),
271271
item, d->value);
272272
return dflt;
@@ -301,7 +301,7 @@ unsigned int getdef_unum (const char *item, unsigned int dflt)
301301
if ( (getlong (d->value, &val) == 0)
302302
|| (val < 0)
303303
|| (val > INT_MAX)) {
304-
fprintf (stderr,
304+
fprintf (shadow_logfd,
305305
_("configuration error - cannot parse %s value: '%s'"),
306306
item, d->value);
307307
return dflt;
@@ -334,7 +334,7 @@ long getdef_long (const char *item, long dflt)
334334
}
335335

336336
if (getlong (d->value, &val) == 0) {
337-
fprintf (stderr,
337+
fprintf (shadow_logfd,
338338
_("configuration error - cannot parse %s value: '%s'"),
339339
item, d->value);
340340
return dflt;
@@ -367,7 +367,7 @@ unsigned long getdef_ulong (const char *item, unsigned long dflt)
367367

368368
if (getulong (d->value, &val) == 0) {
369369
/* FIXME: we should have a getulong */
370-
fprintf (stderr,
370+
fprintf (shadow_logfd,
371371
_("configuration error - cannot parse %s value: '%s'"),
372372
item, d->value);
373373
return dflt;
@@ -405,7 +405,7 @@ int putdef_str (const char *name, const char *value)
405405
cp = strdup (value);
406406
if (NULL == cp) {
407407
(void) fputs (_("Could not allocate space for config info.\n"),
408-
stderr);
408+
shadow_logfd);
409409
SYSLOG ((LOG_ERR, "could not allocate space for config info"));
410410
return -1;
411411
}
@@ -449,7 +449,7 @@ static /*@observer@*/ /*@null@*/struct itemdef *def_find (const char *name)
449449
goto out;
450450
}
451451
}
452-
fprintf (stderr,
452+
fprintf (shadow_logfd,
453453
_("configuration error - unknown item '%s' (notify administrator)\n"),
454454
name);
455455
SYSLOG ((LOG_CRIT, "unknown configuration item `%s'", name));

‎lib/nscd.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ int nscd_flush_cache (const char *service)
2525

2626
if (run_command (cmd, spawnedArgs, spawnedEnv, &status) != 0) {
2727
/* run_command writes its own more detailed message. */
28-
(void) fprintf (stderr, _(MSG_NSCD_FLUSH_CACHE_FAILED), Prog);
28+
(void) fprintf (shadow_logfd, _(MSG_NSCD_FLUSH_CACHE_FAILED), Prog);
2929
return -1;
3030
}
3131

3232
code = WEXITSTATUS (status);
3333
if (!WIFEXITED (status)) {
34-
(void) fprintf (stderr,
34+
(void) fprintf (shadow_logfd,
3535
_("%s: nscd did not terminate normally (signal %d)\n"),
3636
Prog, WTERMSIG (status));
3737
return -1;
@@ -43,9 +43,9 @@ int nscd_flush_cache (const char *service)
4343
/* nscd is installed, but it isn't active. */
4444
return 0;
4545
} else if (code != 0) {
46-
(void) fprintf (stderr, _("%s: nscd exited with status %d\n"),
46+
(void) fprintf (shadow_logfd, _("%s: nscd exited with status %d\n"),
4747
Prog, code);
48-
(void) fprintf (stderr, _(MSG_NSCD_FLUSH_CACHE_FAILED), Prog);
48+
(void) fprintf (shadow_logfd, _(MSG_NSCD_FLUSH_CACHE_FAILED), Prog);
4949
return -1;
5050
}
5151

‎lib/nss.c‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void nss_init(char *nsswitch_path) {
5656
// subid: files
5757
nssfp = fopen(nsswitch_path, "r");
5858
if (!nssfp) {
59-
fprintf(stderr, "Failed opening %s: %m", nsswitch_path);
59+
fprintf(shadow_logfd, "Failed opening %s: %m", nsswitch_path);
6060
atomic_store(&nss_init_completed, true);
6161
return;
6262
}
@@ -82,16 +82,16 @@ void nss_init(char *nsswitch_path) {
8282
goto done;
8383
}
8484
if (strlen(token) > 50) {
85-
fprintf(stderr, "Subid NSS module name too long (longer than 50 characters): %s\n", token);
86-
fprintf(stderr, "Using files\n");
85+
fprintf(shadow_logfd, "Subid NSS module name too long (longer than 50 characters): %s\n", token);
86+
fprintf(shadow_logfd, "Using files\n");
8787
subid_nss = NULL;
8888
goto done;
8989
}
9090
snprintf(libname, 64, "libsubid_%s.so", token);
9191
h = dlopen(libname, RTLD_LAZY);
9292
if (!h) {
93-
fprintf(stderr, "Error opening %s: %s\n", libname, dlerror());
94-
fprintf(stderr, "Using files\n");
93+
fprintf(shadow_logfd, "Error opening %s: %s\n", libname, dlerror());
94+
fprintf(shadow_logfd, "Using files\n");
9595
subid_nss = NULL;
9696
goto done;
9797
}
@@ -102,31 +102,31 @@ void nss_init(char *nsswitch_path) {
102102
}
103103
subid_nss->has_range = dlsym(h, "shadow_subid_has_range");
104104
if (!subid_nss->has_range) {
105-
fprintf(stderr, "%s did not provide @has_range@\n", libname);
105+
fprintf(shadow_logfd, "%s did not provide @has_range@\n", libname);
106106
dlclose(h);
107107
free(subid_nss);
108108
subid_nss = NULL;
109109
goto done;
110110
}
111111
subid_nss->list_owner_ranges = dlsym(h, "shadow_subid_list_owner_ranges");
112112
if (!subid_nss->list_owner_ranges) {
113-
fprintf(stderr, "%s did not provide @list_owner_ranges@\n", libname);
113+
fprintf(shadow_logfd, "%s did not provide @list_owner_ranges@\n", libname);
114114
dlclose(h);
115115
free(subid_nss);
116116
subid_nss = NULL;
117117
goto done;
118118
}
119119
subid_nss->has_any_range = dlsym(h, "shadow_subid_has_any_range");
120120
if (!subid_nss->has_any_range) {
121-
fprintf(stderr, "%s did not provide @has_any_range@\n", libname);
121+
fprintf(shadow_logfd, "%s did not provide @has_any_range@\n", libname);
122122
dlclose(h);
123123
free(subid_nss);
124124
subid_nss = NULL;
125125
goto done;
126126
}
127127
subid_nss->find_subid_owners = dlsym(h, "shadow_subid_find_subid_owners");
128128
if (!subid_nss->find_subid_owners) {
129-
fprintf(stderr, "%s did not provide @find_subid_owners@\n", libname);
129+
fprintf(shadow_logfd, "%s did not provide @find_subid_owners@\n", libname);
130130
dlclose(h);
131131
free(subid_nss);
132132
subid_nss = NULL;
@@ -135,7 +135,7 @@ void nss_init(char *nsswitch_path) {
135135
subid_nss->handle = h;
136136
goto done;
137137
}
138-
fprintf(stderr, "No usable subid NSS module found, using files\n");
138+
fprintf(shadow_logfd, "No usable subid NSS module found, using files\n");
139139
// subid_nss has to be null here, but to ease reviews:
140140
free(subid_nss);
141141
subid_nss = NULL;

‎lib/prototypes.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
#include "defines.h"
6060
#include "commonio.h"
6161

62-
extern /*@observer@*/ const char *Prog;
62+
extern /*@observer@*/ const char *Prog; /* Program name showed in error messages */
63+
extern FILE *shadow_logfd; /* file descripter to which error messages are printed */
6364

6465
/* addgrps.c */
6566
#if defined (HAVE_SETGROUPS) && ! defined (USE_PAM)

‎lib/run_part.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <sys/types.h>
88
#include <sys/wait.h>
99
#include <unistd.h>
10+
#include <lib/prototypes.h>
1011

1112
int run_part (char *script_path, char *name, char *action)
1213
{
@@ -83,7 +84,7 @@ int run_parts (char *directory, char *name, char *action)
8384
free (s);
8485

8586
if (execute_result!=0) {
86-
fprintf (stderr,
87+
fprintf (shadow_logfd,
8788
"%s: did not exit cleanly.\n",
8889
namelist[n]->d_name);
8990
for (; n<scanlist; n++) {

‎lib/selinux.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static int selinux_log_cb (int type, const char *fmt, ...) {
154154
&& (errno != EAFNOSUPPORT)) {
155155

156156
(void) fputs (_("Cannot open audit interface.\n"),
157-
stderr);
157+
shadow_logfd);
158158
SYSLOG ((LOG_WARN, "Cannot open audit interface."));
159159
}
160160
}
@@ -207,7 +207,7 @@ int check_selinux_permit (const char *perm_name)
207207
selinux_set_callback (SELINUX_CB_LOG, (union selinux_callback) selinux_log_cb);
208208

209209
if (getprevcon_raw (&user_context_raw) != 0) {
210-
fprintf (stderr,
210+
fprintf (shadow_logfd,
211211
_("%s: can not get previous SELinux process context: %s\n"),
212212
Prog, strerror (errno));
213213
SYSLOG ((LOG_WARN,

0 commit comments

Comments
 (0)