This looks like a good idea - just need to add the etra checking for case where sysconfg() returns -1, as per Guido's earlier patch for similar getgrnam_r usage. Daniel On Thu, Apr 16, 2009 at 11:24:10PM +0900, Ryota Ozaki wrote: > Signed-off-by: Ryota Ozaki <ozaki.ryota@xxxxxxxxx> > > >From de8c57e3a2c4e564ec989016c547ad6754e43871 Mon Sep 17 00:00:00 2001 > From: Ryota Ozaki <ozaki.ryota@xxxxxxxxx> > Date: Wed, 8 Apr 2009 23:10:46 +0900 > Subject: [PATCH] add virGetUIDByUsername() and virGetGIDByGroupname() > > --- > configure.in | 2 +- > src/util.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- > src/util.h | 8 ++++++++ > 3 files changed, 65 insertions(+), 2 deletions(-) > > diff --git a/configure.in b/configure.in > index ac2c89e..2ae15e3 100644 > --- a/configure.in > +++ b/configure.in > @@ -75,7 +75,7 @@ dnl Availability of various common functions > (non-fatal if missing). > AC_CHECK_FUNCS([cfmakeraw regexec uname sched_getaffinity getuid > getgid posix_fallocate mmap readlink]) > > dnl Availability of various not common threadsafe functions > -AC_CHECK_FUNCS([strerror_r strtok_r getmntent_r getgrnam_r getpwuid_r]) > +AC_CHECK_FUNCS([strerror_r strtok_r getmntent_r getgrnam_r getpwuid_r > getpwnam_r]) > > dnl Availability of various common headers (non-fatal if missing). > AC_CHECK_HEADERS([pwd.h paths.h regex.h sys/syslimits.h sys/utsname.h > sys/wait.h winsock2.h sched.h termios.h sys/poll.h syslog.h]) > diff --git a/src/util.c b/src/util.c > index 5abdbbc..b939282 100644 > --- a/src/util.c > +++ b/src/util.c > @@ -53,9 +53,12 @@ > #include <paths.h> > #endif > #include <netdb.h> > -#ifdef HAVE_GETPWUID_R > +#if defined(HAVE_GETPWUID_R) || defined(HAVE_GETPWNAM_R) > #include <pwd.h> > #endif > +#ifdef HAVE_GETGRNAM_R > +#include <grp.h> > +#endif > > #include "virterror_internal.h" > #include "logging.h" > @@ -1686,3 +1689,55 @@ char *virGetUserDirectory(virConnectPtr conn, > return ret; > } > #endif > + > + > +#ifdef HAVE_GETPWNAM_R > +int virGetUIDByUsername(virConnectPtr conn, char *name) > +{ > + struct passwd pwbuf; > + struct passwd *pw = NULL; > + char *strbuf; > + size_t strbuflen = sysconf(_SC_GETPW_R_SIZE_MAX); > + > + if (VIR_ALLOC_N(strbuf, strbuflen) < 0) { > + virReportOOMError(conn); > + return -1; > + } > + > + if (getpwnam_r(name, &pwbuf, strbuf, strbuflen, &pw) != 0 || pw == NULL) { > + virReportSystemError(conn, errno, > + _("Failed to find user record for name '%s'"), > + name); > + VIR_FREE(strbuf); > + return -1; > + } > + VIR_FREE(strbuf); > + return pw->pw_uid; > +} > +#endif > + > + > +#ifdef HAVE_GETGRNAM_R > +int virGetGIDByGroupname(virConnectPtr conn, char *name) > +{ > + struct group grbuf; > + struct group *gr = NULL; > + char *strbuf; > + size_t strbuflen = sysconf(_SC_GETGR_R_SIZE_MAX); > + > + if (VIR_ALLOC_N(strbuf, strbuflen) < 0) { > + virReportOOMError(conn); > + return -1; > + } > + > + if (getgrnam_r(name, &grbuf, strbuf, strbuflen, &gr) != 0 || gr == NULL) { > + virReportSystemError(conn, errno, > + _("Failed to find group record for name '%s'"), > + name); > + VIR_FREE(strbuf); > + return -1; > + } > + VIR_FREE(strbuf); > + return gr->gr_gid; > +} > +#endif > diff --git a/src/util.h b/src/util.h > index 6fe03b6..0f75439 100644 > --- a/src/util.h > +++ b/src/util.h > @@ -199,6 +199,14 @@ char *virGetUserDirectory(virConnectPtr conn, > uid_t uid); > #endif > > +#ifdef HAVE_GETPWNAM_R > +int virGetUIDByUsername(virConnectPtr conn, char *name); > +#endif > + > +#ifdef HAVE_GETGRNAM_R > +int virGetGIDByGroupname(virConnectPtr conn, char *name); > +#endif > + > int virRandomInitialize(unsigned int seed); > int virRandom(int max); > > -- > 1.6.0.6 > > -- > Libvir-list mailing list > Libvir-list@xxxxxxxxxx > https://www.redhat.com/mailman/listinfo/libvir-list -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :| -- Libvir-list mailing list Libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list