* fhandler_proc.cc: Reorganize global proc content data into a new

struct proc_tab_t.  Accommodate throughout.
	(format_proc_version): New function.
	(format_proc_loadavg): New function.
	(format_proc_self): New function.

	* resource.cc (getrlimit): Return correct rlim_max value for
	RLIMIT_NOFILE.
This commit is contained in:
Corinna Vinschen 2009-01-17 10:16:42 +00:00
parent 165cb24581
commit 40255b64d4
3 changed files with 104 additions and 139 deletions

View File

@ -1,3 +1,14 @@
2009-01-17 Corinna Vinschen <corinna@vinschen.de>
* fhandler_proc.cc: Reorganize global proc content data into a new
struct proc_tab_t. Accommodate throughout.
(format_proc_version): New function.
(format_proc_loadavg): New function.
(format_proc_self): New function.
* resource.cc (getrlimit): Return correct rlim_max value for
RLIMIT_NOFILE.
2009-01-16 Corinna Vinschen <corinna@vinschen.de> 2009-01-16 Corinna Vinschen <corinna@vinschen.de>
* Fix copyright dates. * Fix copyright dates.

View File

@ -1,6 +1,6 @@
/* fhandler_proc.cc: fhandler for /proc virtual filesystem /* fhandler_proc.cc: fhandler for /proc virtual filesystem
Copyright 2002, 2003, 2004, 2005, 2006, 2007 Red Hat, Inc. Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2009 Red Hat, Inc.
This file is part of Cygwin. This file is part of Cygwin.
@ -30,73 +30,58 @@ details. */
#define _COMPILING_NEWLIB #define _COMPILING_NEWLIB
#include <dirent.h> #include <dirent.h>
/* offsets in proc_listing */ enum proc_type_t {
static const int PROC_LOADAVG = 2; // /proc/loadavg proc_symlink = -2,
static const int PROC_MEMINFO = 3; // /proc/meminfo proc_file = -1,
static const int PROC_REGISTRY = 4; // /proc/registry proc_none = 0,
static const int PROC_STAT = 5; // /proc/stat proc_directory = 1,
static const int PROC_VERSION = 6; // /proc/version proc_rootdir = 2
static const int PROC_UPTIME = 7; // /proc/uptime };
static const int PROC_CPUINFO = 8; // /proc/cpuinfo
static const int PROC_PARTITIONS = 9; // /proc/partitions struct proc_tab_t {
static const int PROC_SELF = 10; // /proc/self const char *name;
static const int PROC_REGISTRY32 = 11; // /proc/registry32 __dev32_t fhandler;
static const int PROC_REGISTRY64 = 12; // /proc/registry64 proc_type_t type;
static const int PROC_NET = 13; // /proc/net size_t bufsize;
_off64_t (*format_func)(char *, size_t);
};
static _off64_t format_proc_loadavg (char *, size_t);
static _off64_t format_proc_meminfo (char *, size_t);
static _off64_t format_proc_stat (char *, size_t);
static _off64_t format_proc_version (char *, size_t);
static _off64_t format_proc_uptime (char *, size_t);
static _off64_t format_proc_cpuinfo (char *, size_t);
static _off64_t format_proc_partitions (char *, size_t);
static _off64_t format_proc_self (char *, size_t);
/* names of objects in /proc */ /* names of objects in /proc */
static const char *proc_listing[] = { static const proc_tab_t proc_tab[] = {
".", { ".", FH_PROC, proc_directory, 0, NULL },
"..", { "..", FH_PROC, proc_directory, 0, NULL },
"loadavg", { "loadavg", FH_PROC, proc_file, 16, format_proc_loadavg },
"meminfo", { "meminfo", FH_PROC, proc_file, 2048, format_proc_meminfo },
"registry", { "registry", FH_REGISTRY, proc_directory, 0, NULL },
"stat", { "stat", FH_PROC, proc_file, 16384, format_proc_stat },
"version", { "version", FH_PROC, proc_file, 100, format_proc_version },
"uptime", { "uptime", FH_PROC, proc_file, 80, format_proc_uptime },
"cpuinfo", { "cpuinfo", FH_PROC, proc_file, 16384, format_proc_cpuinfo },
"partitions", { "partitions", FH_PROC, proc_file, 4096, format_proc_partitions },
"self", { "self", FH_PROC, proc_symlink, 16, format_proc_self },
"registry32", { "registry32", FH_REGISTRY, proc_directory, 0, NULL },
"registry64", { "registry64", FH_REGISTRY, proc_directory, 0, NULL },
"net", { "net", FH_PROCNET, proc_directory, 0, NULL },
NULL { NULL, 0, proc_none, 0, NULL },
}; };
#define PROC_DIR_COUNT 4 #define PROC_DIR_COUNT 4
static const int PROC_LINK_COUNT = (sizeof (proc_listing) / sizeof (const char *)) - 1; static const int PROC_LINK_COUNT = (sizeof (proc_tab) / sizeof (proc_tab_t)) - 1;
/* FH_PROC in the table below means the file/directory is handles by
* fhandler_proc.
*/
static const DWORD proc_fhandlers[PROC_LINK_COUNT] = {
FH_PROC,
FH_PROC,
FH_PROC,
FH_PROC,
FH_REGISTRY,
FH_PROC,
FH_PROC,
FH_PROC,
FH_PROC,
FH_PROC,
FH_PROC,
FH_REGISTRY,
FH_REGISTRY,
FH_PROCNET,
};
/* name of the /proc filesystem */ /* name of the /proc filesystem */
const char proc[] = "/proc"; const char proc[] = "/proc";
const int proc_len = sizeof (proc) - 1; const int proc_len = sizeof (proc) - 1;
static _off64_t format_proc_meminfo (char *destbuf, size_t maxsize);
static _off64_t format_proc_stat (char *destbuf, size_t maxsize);
static _off64_t format_proc_uptime (char *destbuf, size_t maxsize);
static _off64_t format_proc_cpuinfo (char *destbuf, size_t maxsize);
static _off64_t format_proc_partitions (char *destbuf, size_t maxsize);
/* Auxillary function that returns the fhandler associated with the given path /* Auxillary function that returns the fhandler associated with the given path
this is where it would be nice to have pattern matching in C - polymorphism this is where it would be nice to have pattern matching in C - polymorphism
just doesn't cut it. */ just doesn't cut it. */
@ -115,11 +100,11 @@ fhandler_proc::get_proc_fhandler (const char *path)
if (*path == 0) if (*path == 0)
return FH_PROC; return FH_PROC;
for (int i = 0; proc_listing[i]; i++) for (int i = 0; proc_tab[i].name; i++)
{ {
if (path_prefix_p (proc_listing[i], path, strlen (proc_listing[i]), if (path_prefix_p (proc_tab[i].name, path, strlen (proc_tab[i].name),
false)) false))
return proc_fhandlers[i]; return proc_tab[i].fhandler;
} }
if (pinfo (atoi (path))) if (pinfo (atoi (path)))
@ -151,12 +136,12 @@ fhandler_proc::exists ()
debug_printf ("exists (%s)", path); debug_printf ("exists (%s)", path);
path += proc_len; path += proc_len;
if (*path == 0) if (*path == 0)
return 2; return proc_rootdir;
for (int i = 0; proc_listing[i]; i++) for (int i = 0; proc_tab[i].name; i++)
if (!strcmp (path + 1, proc_listing[i])) if (!strcmp (path + 1, proc_tab[i].name))
{ {
fileid = i; fileid = i;
return (proc_fhandlers[i] == FH_PROC) ? (i == PROC_SELF ? -2 : -1) : 1; return proc_tab[i].type;
} }
return 0; return 0;
} }
@ -188,12 +173,12 @@ fhandler_proc::fstat (struct __stat64 *buf)
else else
{ {
path++; path++;
for (int i = 0; proc_listing[i]; i++) for (int i = 0; proc_tab[i].name; i++)
if (!strcmp (path, proc_listing[i])) if (!strcmp (path, proc_tab[i].name))
{ {
if (proc_fhandlers[i] != FH_PROC) if (proc_tab[i].type == proc_directory)
buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH; buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
else if (i == PROC_SELF) else if (proc_tab[i].type == proc_symlink)
buf->st_mode = S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO; buf->st_mode = S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
else else
{ {
@ -213,7 +198,7 @@ fhandler_proc::readdir (DIR *dir, dirent *de)
int res; int res;
if (dir->__d_position < PROC_LINK_COUNT) if (dir->__d_position < PROC_LINK_COUNT)
{ {
strcpy (de->d_name, proc_listing[dir->__d_position++]); strcpy (de->d_name, proc_tab[dir->__d_position++].name);
dir->__flags |= dirent_saw_dot | dirent_saw_dot_dot; dir->__flags |= dirent_saw_dot | dirent_saw_dot_dot;
res = 0; res = 0;
} }
@ -273,12 +258,12 @@ fhandler_proc::open (int flags, mode_t mode)
} }
proc_file_no = -1; proc_file_no = -1;
for (int i = 0; proc_listing[i]; i++) for (int i = 0; proc_tab[i].name; i++)
if (path_prefix_p (proc_listing[i], path + 1, strlen (proc_listing[i]), if (path_prefix_p (proc_tab[i].name, path + 1, strlen (proc_tab[i].name),
false)) false))
{ {
proc_file_no = i; proc_file_no = i;
if (proc_fhandlers[i] != FH_PROC) if (proc_tab[i].fhandler != FH_PROC)
{ {
if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
{ {
@ -327,7 +312,7 @@ fhandler_proc::open (int flags, mode_t mode)
{ {
res = 0; res = 0;
goto out; goto out;
} }
if (flags & O_APPEND) if (flags & O_APPEND)
position = filesize; position = filesize;
@ -346,73 +331,31 @@ out:
bool bool
fhandler_proc::fill_filebuf () fhandler_proc::fill_filebuf ()
{ {
switch (fileid) if (fileid < PROC_LINK_COUNT && proc_tab[fileid].format_func)
{ {
case PROC_VERSION: filebuf = (char *) crealloc_abort (filebuf,
{ bufalloc = proc_tab[fileid].bufsize);
if (!filebuf) filesize = proc_tab[fileid].format_func (filebuf, bufalloc);
{ return true;
struct utsname uts_name;
uname (&uts_name);
bufalloc = strlen (uts_name.sysname) + 1
+ strlen (uts_name.release) + 1
+ strlen (uts_name.version) + 2;
filebuf = (char *) crealloc_abort (filebuf, bufalloc);
filesize = __small_sprintf (filebuf, "%s %s %s\n",
uts_name.sysname, uts_name.release,
uts_name.version);
}
break;
}
case PROC_UPTIME:
{
filebuf = (char *) crealloc_abort (filebuf, bufalloc = 80);
filesize = format_proc_uptime (filebuf, bufalloc);
break;
}
case PROC_STAT:
{
filebuf = (char *) crealloc_abort (filebuf, bufalloc = 16384);
filesize = format_proc_stat (filebuf, bufalloc);
break;
}
case PROC_LOADAVG:
{
/*
* not really supported - Windows doesn't keep track of these values
* Windows 95/98/me does have the KERNEL/CPUUsage performance counter
* which is similar.
*/
filebuf = (char *) crealloc_abort (filebuf, bufalloc = 16);
filesize = __small_sprintf (filebuf, "%u.%02u %u.%02u %u.%02u\n",
0, 0, 0, 0, 0, 0);
break;
}
case PROC_MEMINFO:
{
filebuf = (char *) crealloc_abort (filebuf, bufalloc = 2048);
filesize = format_proc_meminfo (filebuf, bufalloc);
break;
}
case PROC_CPUINFO:
{
filebuf = (char *) crealloc_abort (filebuf, bufalloc = 16384);
filesize = format_proc_cpuinfo (filebuf, bufalloc);
break;
}
case PROC_PARTITIONS:
{
filebuf = (char *) crealloc_abort (filebuf, bufalloc = 4096);
filesize = format_proc_partitions (filebuf, bufalloc);
break;
}
case PROC_SELF:
{
filebuf = (char *) crealloc_abort (filebuf, bufalloc = 32);
filesize = __small_sprintf (filebuf, "%d", getpid ());
}
} }
return true; return false;
}
static _off64_t
format_proc_version (char *destbuf, size_t maxsize)
{
struct utsname uts_name;
uname (&uts_name);
return __small_sprintf (destbuf, "%s %s %s\n",
uts_name.sysname, uts_name.release, uts_name.version);
}
static _off64_t
format_proc_loadavg (char *destbuf, size_t maxsize)
{
return __small_sprintf (destbuf, "%u.%02u %u.%02u %u.%02u\n",
0, 0, 0, 0, 0, 0);
} }
static _off64_t static _off64_t
@ -1173,4 +1116,10 @@ format_proc_partitions (char *destbuf, size_t maxsize)
return bufptr - destbuf; return bufptr - destbuf;
} }
static _off64_t
format_proc_self (char *destbuf, size_t maxsize)
{
return __small_sprintf (destbuf, "%d", getpid ());
}
#undef print #undef print

View File

@ -1,6 +1,6 @@
/* resource.cc: getrusage () and friends. /* resource.cc: getrusage () and friends.
Copyright 1996, 1997, 1998, 2000, 2001, 2002 Red Hat, Inc. Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2009 Red Hat, Inc.
Written by Steve Chamberlain (sac@cygnus.com), Doug Evans (dje@cygnus.com), Written by Steve Chamberlain (sac@cygnus.com), Doug Evans (dje@cygnus.com),
Geoffrey Noer (noer@cygnus.com) of Cygnus Support. Geoffrey Noer (noer@cygnus.com) of Cygnus Support.
@ -17,6 +17,10 @@ details. */
#include "pinfo.h" #include "pinfo.h"
#include "psapi.h" #include "psapi.h"
#include "cygtls.h" #include "cygtls.h"
#include "path.h"
#include "fhandler.h"
#include "pinfo.h"
#include "dtable.h"
/* add timeval values */ /* add timeval values */
static void static void
@ -139,6 +143,7 @@ getrlimit (int resource, struct rlimit *rlp)
rlp->rlim_cur = getdtablesize (); rlp->rlim_cur = getdtablesize ();
if (rlp->rlim_cur < OPEN_MAX) if (rlp->rlim_cur < OPEN_MAX)
rlp->rlim_cur = OPEN_MAX; rlp->rlim_cur = OPEN_MAX;
rlp->rlim_max = 100 * NOFILE_INCR;
break; break;
case RLIMIT_CORE: case RLIMIT_CORE:
rlp->rlim_cur = rlim_core; rlp->rlim_cur = rlim_core;