static const char *sgr_file = SGROUP_FILE;
static bool use_system_sgr_file = true;
static bool is_shadow = false;
+static bool sgr_locked = false;
#endif
+static bool gr_locked = false;
/* Options */
static bool read_only = false;
static bool sort_mode = false;
/* local function prototypes */
+static void fail_exit (int status);
static void usage (void);
static void delete_member (char **, const char *);
static void process_flags (int argc, char **argv);
static void check_sgr_file (int *errors, bool *changed);
#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
*/
fprintf (stderr,
_("%s: cannot lock %s; try again later.\n"),
Prog, grp_file);
- closelog ();
- exit (E_CANT_LOCK);
+ fail_exit (E_CANT_LOCK);
}
+ gr_locked = true;
#ifdef SHADOWGRP
- if (is_shadow && (sgr_lock () == 0)) {
- fprintf (stderr,
- _("%s: cannot lock %s; try again later.\n"),
- Prog, sgr_file);
- closelog ();
- exit (E_CANT_LOCK);
+ if (is_shadow) {
+ if (sgr_lock () == 0) {
+ fprintf (stderr,
+ _("%s: cannot lock %s; try again later.\n"),
+ Prog, sgr_file);
+ fail_exit (E_CANT_LOCK);
+ }
+ sgr_locked = true;
}
#endif
}
if (use_system_grp_file) {
SYSLOG ((LOG_WARN, "cannot open %s", grp_file));
}
- closelog ();
- exit (E_CANT_OPEN);
+ fail_exit (E_CANT_OPEN);
}
#ifdef SHADOWGRP
if (is_shadow && (sgr_open (read_only ? O_RDONLY : O_RDWR) == 0)) {
if (use_system_sgr_file) {
SYSLOG ((LOG_WARN, "cannot open %s", sgr_file));
}
- closelog ();
- exit (E_CANT_OPEN);
+ fail_exit (E_CANT_OPEN);
}
#endif
}
if (gr_close () == 0) {
fprintf (stderr, _("%s: failure while writing changes to %s\n"),
Prog, grp_file);
- exit (E_CANT_UPDATE);
+ fail_exit (E_CANT_UPDATE);
}
#ifdef SHADOWGRP
if (is_shadow && (sgr_close () == 0)) {
fprintf (stderr, _("%s: failure while writing changes to %s\n"),
Prog, sgr_file);
- exit (E_CANT_UPDATE);
+ fail_exit (E_CANT_UPDATE);
}
#endif
}
* Don't be anti-social - unlock the files when you're done.
*/
#ifdef SHADOWGRP
- if (is_shadow) {
- sgr_unlock ();
+ 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 */
+ }
+ sgr_locked = false;
}
#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;
+ }
}
/*
_
("%s: can't update shadow entry for %s\n"),
Prog, sg.sg_name);
- exit (E_CANT_UPDATE);
+ fail_exit (E_CANT_UPDATE);
}
/* remove password from /etc/group */
gr = *grp;
_
("%s: can't update entry for group %s\n"),
Prog, gr.gr_name);
- exit (E_CANT_UPDATE);
+ fail_exit (E_CANT_UPDATE);
}
}
} else {
static void fail_exit (int status)
{
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) {
- 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);
}
Prog, gr_dbname ());
fail_exit (3);
}
- sgr_unlock ();
- gr_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;
+ 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");