Changeset 8219374


Ignore:
Timestamp:
Jul 16, 2011, 12:28:10 AM (13 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.10, 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.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tester.c

    r396505be r8219374  
    44#undef WINDOW
    55
    6 #include <errno.h>
    76#include <unistd.h>
    8 #include <pwd.h>
    97#include <stdio.h>
    108#include <stdlib.h>
    119#include <string.h>
    12 #include <sys/types.h>
    1310
    1411#undef instr
     
    136133{
    137134  int numfailed=0;
    138   const char *home;
    139   char *s, *path;
    140   struct passwd *pw;
     135  char *s, *path, *home;
    141136
    142137  printf("# BEGIN testing owl_util\n");
     
    274269  g_free(s);
    275270
    276   errno = 0;
    277   home = owl_global_get_homedir(&g);
     271  home = g_strdup(owl_global_get_homedir(&g));
    278272  s = owl_util_makepath("~");
    279273  FAIL_UNLESS("makepath ~", !strcmp(home, s));
     
    285279  g_free(s);
    286280  g_free(path);
    287 
    288   errno = 0;
    289   pw = getpwnam("root");
    290   if (pw) {
    291     home = pw->pw_dir;
    292   } else {
     281  g_free(home);
     282
     283  home = owl_util_homedir_for_user("root");
     284  if (home == NULL) {
    293285    /* Just make some noise so we notice. */
    294     home = "<WHAT>";
    295     fprintf(stderr, "getpwnam: %s", errno ? strerror(errno) : "No such user");
     286    home = g_strdup("<WHAT>");
     287    fprintf(stderr, "owl_util_homedir_for_user failed");
    296288  }
    297289
     
    305297  g_free(s);
    306298  g_free(path);
     299  g_free(home);
    307300
    308301  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
  • 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.