Skip to content

Commit 6c60f0d

Browse files
committed
gridinit: Adaptive buffer size for getpwnam() and getpwnam()
1 parent c33a285 commit 6c60f0d

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

main/gridinit.c

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -662,50 +662,67 @@ _str_is_num(const gchar *s)
662662
return TRUE;
663663
}
664664

665-
/** XXX JFS Linux-specific code */
665+
typedef gboolean (_int_resolver_f) (const char *str, gint32 *out, gchar *buf, gsize len);
666+
666667
static gboolean
667-
uid_exists(const gchar *str, gint32 *id)
668+
_load_integer(_int_resolver_f *cb, const char *str, gint32 *out)
668669
{
669-
struct passwd pwd, *p_pwd;
670-
gchar buf[32768];
670+
static gsize buflen = 2048;
671+
gchar *buf = NULL;
672+
gboolean rc = FALSE;
671673

672674
if (_str_is_num(str)) {
673-
gint64 i64;
674-
675-
i64 = g_ascii_strtoll(str, NULL, 10);
676-
*id = i64;
675+
gint64 i64 = g_ascii_strtoll(str, NULL, 10);
676+
*out = i64;
677677
return TRUE;
678678
}
679679

680-
if (0 != getpwnam_r(str, &pwd, buf, sizeof(buf), &p_pwd))
681-
return FALSE;
680+
retry:
681+
buf = g_try_realloc(buf, buflen);
682+
errno = 0;
683+
if (cb(str, out, buf, buflen)) {
684+
rc = TRUE;
685+
} else if (errno == ERANGE && buflen < 16 * 1024 * 1024) {
686+
buflen = buflen * 2;
687+
goto retry;
688+
}
682689

683-
*id = pwd.pw_uid;
684-
return TRUE;
690+
g_free(buf);
691+
return rc;
685692
}
686693

687-
/** XXX JFS Linux-specific code */
688694
static gboolean
689-
gid_exists(const gchar *str, gint32 *id)
695+
_load_uid(const char *str, gint32 *out, gchar *buf, gsize len)
690696
{
691-
struct group grp, *p_grp;
692-
gchar buf[32768];
693-
694-
if (_str_is_num(str)) {
695-
gint64 i64;
696-
697-
i64 = g_ascii_strtoll(str, NULL, 10);
698-
*id = i64;
699-
return TRUE;
700-
}
701-
702-
if (0 != getgrnam_r(str, &grp, buf, sizeof(buf), &p_grp))
697+
struct passwd pwd = {}, *p_pwd = NULL;
698+
if (0 != getpwnam_r(str, &pwd, buf, len, &p_pwd))
703699
return FALSE;
700+
*out = pwd.pw_uid;
701+
return TRUE;
702+
}
704703

705-
*id = grp.gr_gid;
704+
static gboolean
705+
_load_gid(const char *str, gint32 *out, gchar *buf, gsize len)
706+
{
707+
struct group grp = {}, *p_grp = NULL;
708+
if (0 != getgrnam_r(str, &grp, buf, len, &p_grp))
709+
return FALSE;
710+
*out = grp.gr_gid;
706711
return TRUE;
707712
}
708713

714+
static gboolean
715+
uid_exists(const gchar *str, gint32 *id)
716+
{
717+
return _load_integer(_load_uid, str, id);
718+
}
719+
720+
static gboolean
721+
gid_exists(const gchar *str, gint32 *id)
722+
{
723+
return _load_integer(_load_gid, str, id);
724+
}
725+
709726
static void
710727
_cfg_service_load_env(GKeyFile *kf, const gchar *section, const gchar *str_key)
711728
{

0 commit comments

Comments
 (0)