hyperic-sigar
view release on metacpan or search on metacpan
src/sigar_format.c view on Meta::CPAN
/*
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
* This file is part of SIGAR.
*
* SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
/* Utility functions to provide string formatting of SIGAR data */
#include "sigar.h"
#include "sigar_private.h"
#include "sigar_util.h"
#include "sigar_os.h"
#include "sigar_format.h"
#include <errno.h>
#include <stdio.h>
#ifndef WIN32
#include <netinet/in.h>
#include <arpa/inet.h>
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(_AIX)
#include <sys/socket.h>
#endif
#include <pwd.h>
#include <grp.h>
/* sysconf(_SC_GET{PW,GR}_R_SIZE_MAX) */
#define R_SIZE_MAX 1024
int sigar_user_name_get(sigar_t *sigar, int uid, char *buf, int buflen)
{
struct passwd *pw = NULL;
/* XXX cache lookup */
# ifdef HAVE_GETPWUID_R
struct passwd pwbuf;
char buffer[R_SIZE_MAX];
if (getpwuid_r(uid, &pwbuf, buffer, sizeof(buffer), &pw) != 0) {
return errno;
}
if (!pw) {
return ENOENT;
}
# else
if ((pw = getpwuid(uid)) == NULL) {
return errno;
}
# endif
strncpy(buf, pw->pw_name, buflen);
buf[buflen-1] = '\0';
return SIGAR_OK;
}
int sigar_group_name_get(sigar_t *sigar, int gid, char *buf, int buflen)
{
struct group *gr;
/* XXX cache lookup */
# ifdef HAVE_GETGRGID_R
struct group grbuf;
char buffer[R_SIZE_MAX];
if (getgrgid_r(gid, &grbuf, buffer, sizeof(buffer), &gr) != 0) {
return errno;
}
# else
if ((gr = getgrgid(gid)) == NULL) {
return errno;
}
# endif
if (gr && gr->gr_name) {
strncpy(buf, gr->gr_name, buflen);
}
else {
/* seen on linux.. apache httpd.conf has:
* Group #-1
* results in uid == -1 and gr == NULL.
* wtf getgrgid_r doesnt fail instead?
*/
sprintf(buf, "%d", gid);
}
buf[buflen-1] = '\0';
return SIGAR_OK;
}
int sigar_user_id_get(sigar_t *sigar, const char *name, int *uid)
{
/* XXX cache lookup */
struct passwd *pw;
# ifdef HAVE_GETPWNAM_R
struct passwd pwbuf;
char buf[R_SIZE_MAX];
if (getpwnam_r(name, &pwbuf, buf, sizeof(buf), &pw) != 0) {
return errno;
}
# else
if (!(pw = getpwnam(name))) {
return errno;
}
( run in 0.636 second using v1.01-cache-2.11-cpan-39bf76dae61 )