Changeset 8219374 for util.c


Ignore:
Timestamp:
Jul 16, 2011, 12:28:10 AM (10 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.8, release-1.9
Children:
f5f6ec0
Parents:
99ac28a
git-author:
David Benjamin <davidben@mit.edu> (07/10/11 19:11:46)
git-committer:
David Benjamin <davidben@mit.edu> (07/16/11 00:28:10)
Message:
Use getpwnam_r instead of getpwnam

It's re-entrant, so that's probably a good thing in case some other
thread calls getpwnam. More relevantly, Solaris has both a standard and
a non-standard version, and perl redefines getpwnam to a getpwnam_r. We
manage to convince Solaris to use the standard form, but perl doesn't
seem to notice and defines the macro wrong.

So just avoid the function altogether. Seems as reasonable of an excuse
to use the ostensibly better (and infinitely less friendly) one as any.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • util.c

    r396505be r8219374  
    3232}
    3333
     34CALLER_OWN char *owl_util_homedir_for_user(const char *name)
     35{
     36  int err;
     37  struct passwd pw_buf;
     38  struct passwd *pw;
     39
     40  char *pw_strbuf, *ret;
     41  long pw_strbuf_len = sysconf(_SC_GETPW_R_SIZE_MAX);
     42  if (pw_strbuf_len < 0) {
     43    /* If we really hate ourselves, we can be fancy and loop until we stop
     44     * getting ERANGE. For now just pick a random number. */
     45    owl_function_error("owl_util_homedir_for_user: Could not get _SC_GETPW_R_SIZE_MAX");
     46    pw_strbuf_len = 16384;
     47  }
     48  pw_strbuf = g_new0(char, pw_strbuf_len);
     49  err = getpwnam_r(name, &pw_buf, pw_strbuf, pw_strbuf_len, &pw);
     50  if (err) {
     51    owl_function_error("getpwuid_r: %s", strerror(err));
     52    /* Fall through; pw will be NULL. */
     53  }
     54  ret = pw ? g_strdup(pw->pw_dir) : NULL;
     55  g_free(pw_strbuf);
     56  return ret;
     57}
     58
    3459/* Return a "nice" version of the path.  Tilde expansion is done, and
    3560 * duplicate slashes are removed.  Caller must free the return.
     
    5580      /* Someone else's home directory. */
    5681      char *user = g_strndup(in + 1, end - (in + 1));
    57       struct passwd *pw = getpwnam(user);
    58       g_free(user);
    59       if (pw) {
    60         out = g_strconcat(pw->pw_dir, end, NULL);
     82      char *home = owl_util_homedir_for_user(user);
     83      if (home) {
     84        out = g_strconcat(home, end, NULL);
    6185      } else {
    6286        out = g_strdup(in);
    6387      }
     88      g_free(home);
     89      g_free(user);
    6490    }
    6591  } else {
Note: See TracChangeset for help on using the changeset viewer.