* src/grpck.c: Added function fail_exit(). Check failure to unlock

files. Report errors to stderr and syslog, but continue.
	* src/grpconv.c: Check failure to unlock files. Report errors to
	stderr and syslog, but continue.
This commit is contained in:
nekral-guest 2008-08-22 02:26:46 +00:00
parent 7ae6b8fc34
commit 2bdcb9c33d
3 changed files with 93 additions and 23 deletions

View File

@ -1,3 +1,10 @@
2008-08-18 Nicolas François <nicolas.francois@centraliens.net>
* src/grpck.c: Added function fail_exit(). Check failure to unlock
files. Report errors to stderr and syslog, but continue.
* src/grpconv.c: Check failure to unlock files. Report errors to
stderr and syslog, but continue.
2008-08-18 Nicolas François <nicolas.francois@centraliens.net> 2008-08-18 Nicolas François <nicolas.francois@centraliens.net>
* src/passwd.c: Check failure to unlock files. Report errors to * src/passwd.c: Check failure to unlock files. Report errors to

View File

@ -72,12 +72,15 @@ static bool use_system_grp_file = true;
static const char *sgr_file = SGROUP_FILE; static const char *sgr_file = SGROUP_FILE;
static bool use_system_sgr_file = true; static bool use_system_sgr_file = true;
static bool is_shadow = false; static bool is_shadow = false;
static bool sgr_locked = false;
#endif #endif
static bool gr_locked = false;
/* Options */ /* Options */
static bool read_only = false; static bool read_only = false;
static bool sort_mode = false; static bool sort_mode = false;
/* local function prototypes */ /* local function prototypes */
static void fail_exit (int status);
static void usage (void); static void usage (void);
static void delete_member (char **, const char *); static void delete_member (char **, const char *);
static void process_flags (int argc, char **argv); static void process_flags (int argc, char **argv);
@ -99,6 +102,34 @@ static void compare_members_lists (const char *groupname,
static void check_sgr_file (int *errors, bool *changed); static void check_sgr_file (int *errors, bool *changed);
#endif #endif
/*
* fail_exit - exit with an error code after unlocking files
*/
static void fail_exit (int status)
{
if (gr_locked) {
if (gr_unlock () == 0) {
fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
/* continue */
}
}
#ifdef SHADOWGRP
if (sgr_locked) {
if (sgr_unlock () == 0) {
fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
/* continue */
}
}
#endif
closelog ();
exit (status);
}
/* /*
* usage - print syntax message and exit * usage - print syntax message and exit
*/ */
@ -214,16 +245,18 @@ static void open_files (void)
fprintf (stderr, fprintf (stderr,
_("%s: cannot lock %s; try again later.\n"), _("%s: cannot lock %s; try again later.\n"),
Prog, grp_file); Prog, grp_file);
closelog (); fail_exit (E_CANT_LOCK);
exit (E_CANT_LOCK);
} }
gr_locked = true;
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (is_shadow && (sgr_lock () == 0)) { if (is_shadow) {
fprintf (stderr, if (sgr_lock () == 0) {
_("%s: cannot lock %s; try again later.\n"), fprintf (stderr,
Prog, sgr_file); _("%s: cannot lock %s; try again later.\n"),
closelog (); Prog, sgr_file);
exit (E_CANT_LOCK); fail_exit (E_CANT_LOCK);
}
sgr_locked = true;
} }
#endif #endif
} }
@ -238,8 +271,7 @@ static void open_files (void)
if (use_system_grp_file) { if (use_system_grp_file) {
SYSLOG ((LOG_WARN, "cannot open %s", grp_file)); SYSLOG ((LOG_WARN, "cannot open %s", grp_file));
} }
closelog (); fail_exit (E_CANT_OPEN);
exit (E_CANT_OPEN);
} }
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (is_shadow && (sgr_open (read_only ? O_RDONLY : O_RDWR) == 0)) { if (is_shadow && (sgr_open (read_only ? O_RDONLY : O_RDWR) == 0)) {
@ -248,8 +280,7 @@ static void open_files (void)
if (use_system_sgr_file) { if (use_system_sgr_file) {
SYSLOG ((LOG_WARN, "cannot open %s", sgr_file)); SYSLOG ((LOG_WARN, "cannot open %s", sgr_file));
} }
closelog (); fail_exit (E_CANT_OPEN);
exit (E_CANT_OPEN);
} }
#endif #endif
} }
@ -271,13 +302,13 @@ static void close_files (bool changed)
if (gr_close () == 0) { if (gr_close () == 0) {
fprintf (stderr, _("%s: failure while writing changes to %s\n"), fprintf (stderr, _("%s: failure while writing changes to %s\n"),
Prog, grp_file); Prog, grp_file);
exit (E_CANT_UPDATE); fail_exit (E_CANT_UPDATE);
} }
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (is_shadow && (sgr_close () == 0)) { if (is_shadow && (sgr_close () == 0)) {
fprintf (stderr, _("%s: failure while writing changes to %s\n"), fprintf (stderr, _("%s: failure while writing changes to %s\n"),
Prog, sgr_file); Prog, sgr_file);
exit (E_CANT_UPDATE); fail_exit (E_CANT_UPDATE);
} }
#endif #endif
} }
@ -286,11 +317,23 @@ static void close_files (bool changed)
* Don't be anti-social - unlock the files when you're done. * Don't be anti-social - unlock the files when you're done.
*/ */
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (is_shadow) { if (sgr_locked) {
sgr_unlock (); if (sgr_unlock () == 0) {
fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
/* continue */
}
sgr_locked = false;
} }
#endif #endif
(void) gr_unlock (); if (gr_locked) {
if (gr_unlock () == 0) {
fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
/* continue */
}
gr_locked = false;
}
} }
/* /*
@ -552,7 +595,7 @@ static void check_grp_file (int *errors, bool *changed)
_ _
("%s: can't update shadow entry for %s\n"), ("%s: can't update shadow entry for %s\n"),
Prog, sg.sg_name); Prog, sg.sg_name);
exit (E_CANT_UPDATE); fail_exit (E_CANT_UPDATE);
} }
/* remove password from /etc/group */ /* remove password from /etc/group */
gr = *grp; gr = *grp;
@ -562,7 +605,7 @@ static void check_grp_file (int *errors, bool *changed)
_ _
("%s: can't update entry for group %s\n"), ("%s: can't update entry for group %s\n"),
Prog, gr.gr_name); Prog, gr.gr_name);
exit (E_CANT_UPDATE); fail_exit (E_CANT_UPDATE);
} }
} }
} else { } else {

View File

@ -62,11 +62,21 @@ static void fail_exit (int);
static void fail_exit (int status) static void fail_exit (int status)
{ {
if (gr_locked) { if (gr_locked) {
gr_unlock (); if (gr_unlock () == 0) {
fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
/* continue */
}
} }
if (sgr_locked) { if (sgr_locked) {
sgr_unlock (); if (sgr_unlock () == 0) {
fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
/* continue */
}
} }
exit (status); exit (status);
} }
@ -186,8 +196,18 @@ int main (int argc, char **argv)
Prog, gr_dbname ()); Prog, gr_dbname ());
fail_exit (3); fail_exit (3);
} }
sgr_unlock (); if (sgr_unlock () == 0) {
gr_unlock (); fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, sgr_dbname ());
SYSLOG ((LOG_ERR, "failed to unlock %s", sgr_dbname ()));
/* continue */
}
sgr_locked = false;
if (gr_unlock () == 0) {
fprintf (stderr, _("%s: failed to unlock %s\n"), Prog, gr_dbname ());
SYSLOG ((LOG_ERR, "failed to unlock %s", gr_dbname ()));
/* continue */
}
gr_locked = false;
nscd_flush_cache ("group"); nscd_flush_cache ("group");