2002-05-02 12:26:05 +08:00
|
|
|
/* fhandler_proc.cc: fhandler for /proc virtual filesystem
|
|
|
|
|
2003-01-10 20:32:49 +08:00
|
|
|
Copyright 2002, 2003 Red Hat, Inc.
|
2002-05-02 12:26:05 +08:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2003-03-10 04:10:25 +08:00
|
|
|
#define _WIN32_WINNT 0x0501
|
|
|
|
|
2002-05-02 12:26:05 +08:00
|
|
|
#include "winsup.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/cygwin.h>
|
2002-05-12 09:37:48 +08:00
|
|
|
#include <ntdef.h>
|
2002-05-02 12:26:05 +08:00
|
|
|
#include "cygerrno.h"
|
|
|
|
#include "security.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "pinfo.h"
|
2002-05-04 11:24:35 +08:00
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2002-05-02 12:26:05 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/utsname.h>
|
2002-09-01 00:37:51 +08:00
|
|
|
#include <sys/param.h>
|
2002-05-12 09:37:48 +08:00
|
|
|
#include "ntdll.h"
|
2003-03-10 04:10:25 +08:00
|
|
|
#include <winioctl.h>
|
2002-05-02 12:26:05 +08:00
|
|
|
|
|
|
|
#define _COMPILING_NEWLIB
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
/* offsets in proc_listing */
|
2002-05-12 09:37:48 +08:00
|
|
|
static const int PROC_LOADAVG = 2; // /proc/loadavg
|
|
|
|
static const int PROC_MEMINFO = 3; // /proc/meminfo
|
|
|
|
static const int PROC_REGISTRY = 4; // /proc/registry
|
|
|
|
static const int PROC_STAT = 5; // /proc/stat
|
|
|
|
static const int PROC_VERSION = 6; // /proc/version
|
|
|
|
static const int PROC_UPTIME = 7; // /proc/uptime
|
2003-03-10 04:10:25 +08:00
|
|
|
static const int PROC_CPUINFO = 8; // /proc/cpuinfo
|
|
|
|
static const int PROC_PARTITIONS = 9; // /proc/partitions
|
2002-05-02 12:26:05 +08:00
|
|
|
|
|
|
|
/* names of objects in /proc */
|
|
|
|
static const char *proc_listing[] = {
|
2002-05-04 11:24:35 +08:00
|
|
|
".",
|
|
|
|
"..",
|
2002-05-12 09:37:48 +08:00
|
|
|
"loadavg",
|
|
|
|
"meminfo",
|
2002-05-02 12:26:05 +08:00
|
|
|
"registry",
|
2002-05-12 09:37:48 +08:00
|
|
|
"stat",
|
2002-05-02 12:26:05 +08:00
|
|
|
"version",
|
|
|
|
"uptime",
|
2003-03-10 04:10:25 +08:00
|
|
|
"cpuinfo",
|
|
|
|
"partitions",
|
2002-05-02 12:26:05 +08:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2002-09-22 11:38:57 +08:00
|
|
|
static const int PROC_LINK_COUNT = (sizeof (proc_listing) / sizeof (const char *)) - 1;
|
2002-05-02 12:26:05 +08:00
|
|
|
|
|
|
|
/* FH_PROC in the table below means the file/directory is handles by
|
|
|
|
* fhandler_proc.
|
|
|
|
*/
|
2002-05-12 09:37:48 +08:00
|
|
|
static const DWORD proc_fhandlers[PROC_LINK_COUNT] = {
|
|
|
|
FH_PROC,
|
|
|
|
FH_PROC,
|
2002-05-04 11:24:35 +08:00
|
|
|
FH_PROC,
|
|
|
|
FH_PROC,
|
2002-05-02 12:26:05 +08:00
|
|
|
FH_REGISTRY,
|
|
|
|
FH_PROC,
|
2002-05-12 09:37:48 +08:00
|
|
|
FH_PROC,
|
2003-03-10 04:10:25 +08:00
|
|
|
FH_PROC,
|
|
|
|
FH_PROC,
|
|
|
|
FH_PROC,
|
2002-05-02 12:26:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* name of the /proc filesystem */
|
|
|
|
const char proc[] = "/proc";
|
|
|
|
const int proc_len = sizeof (proc) - 1;
|
|
|
|
|
2003-03-09 00:44:25 +08:00
|
|
|
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);
|
2003-03-10 04:10:25 +08:00
|
|
|
static __off64_t format_proc_cpuinfo (char *destbuf, size_t maxsize);
|
|
|
|
static __off64_t format_proc_partitions (char *destbuf, size_t maxsize);
|
2002-05-12 09:37:48 +08:00
|
|
|
|
2003-03-10 04:10:25 +08:00
|
|
|
/* 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
|
|
|
|
just doesn't cut it. */
|
2002-05-02 12:26:05 +08:00
|
|
|
DWORD
|
|
|
|
fhandler_proc::get_proc_fhandler (const char *path)
|
|
|
|
{
|
|
|
|
debug_printf ("get_proc_fhandler(%s)", path);
|
|
|
|
path += proc_len;
|
|
|
|
/* Since this method is called from path_conv::check we can't rely on
|
2003-03-10 04:10:25 +08:00
|
|
|
it being normalised and therefore the path may have runs of slashes
|
|
|
|
in it. */
|
2003-01-09 16:22:05 +08:00
|
|
|
while (isdirsep (*path))
|
2002-05-02 12:26:05 +08:00
|
|
|
path++;
|
|
|
|
|
|
|
|
/* Check if this is the root of the virtual filesystem (i.e. /proc). */
|
|
|
|
if (*path == 0)
|
|
|
|
return FH_PROC;
|
|
|
|
|
|
|
|
for (int i = 0; proc_listing[i]; i++)
|
|
|
|
{
|
|
|
|
if (path_prefix_p (proc_listing[i], path, strlen (proc_listing[i])))
|
2002-05-28 09:55:40 +08:00
|
|
|
return proc_fhandlers[i];
|
2002-05-02 12:26:05 +08:00
|
|
|
}
|
|
|
|
|
2002-06-02 14:07:01 +08:00
|
|
|
if (pinfo (atoi (path)))
|
|
|
|
return FH_PROCESS;
|
2002-05-04 11:24:35 +08:00
|
|
|
|
2002-06-02 14:07:01 +08:00
|
|
|
bool has_subdir = false;
|
|
|
|
while (*path)
|
2003-01-09 16:22:05 +08:00
|
|
|
if (isdirsep (*path++))
|
2002-06-02 14:07:01 +08:00
|
|
|
{
|
|
|
|
has_subdir = true;
|
|
|
|
break;
|
|
|
|
}
|
2002-05-04 11:24:35 +08:00
|
|
|
|
2002-06-02 14:07:01 +08:00
|
|
|
if (has_subdir)
|
|
|
|
/* The user is trying to access a non-existent subdirectory of /proc. */
|
|
|
|
return FH_BAD;
|
|
|
|
else
|
|
|
|
/* Return FH_PROC so that we can return EROFS if the user is trying to create
|
|
|
|
a file. */
|
|
|
|
return FH_PROC;
|
2002-05-02 12:26:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns 0 if path doesn't exist, >0 if path is a directory,
|
2003-03-10 04:10:25 +08:00
|
|
|
<0 if path is a file. */
|
2002-05-02 12:26:05 +08:00
|
|
|
int
|
2002-05-23 06:09:58 +08:00
|
|
|
fhandler_proc::exists ()
|
2002-05-02 12:26:05 +08:00
|
|
|
{
|
2002-05-23 06:09:58 +08:00
|
|
|
const char *path = get_name ();
|
2002-05-02 12:26:05 +08:00
|
|
|
debug_printf ("exists (%s)", path);
|
|
|
|
path += proc_len;
|
|
|
|
if (*path == 0)
|
|
|
|
return 2;
|
|
|
|
for (int i = 0; proc_listing[i]; i++)
|
|
|
|
if (pathmatch (path + 1, proc_listing[i]))
|
|
|
|
return (proc_fhandlers[i] == FH_PROC) ? -1 : 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_proc::fhandler_proc ():
|
|
|
|
fhandler_virtual (FH_PROC)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_proc::fhandler_proc (DWORD devtype):
|
|
|
|
fhandler_virtual (devtype)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_proc::fstat (struct __stat64 *buf, path_conv *pc)
|
|
|
|
{
|
|
|
|
const char *path = get_name ();
|
2002-06-02 11:13:22 +08:00
|
|
|
debug_printf ("fstat (%s)", path);
|
|
|
|
|
2002-05-02 12:26:05 +08:00
|
|
|
path += proc_len;
|
|
|
|
(void) fhandler_base::fstat (buf, pc);
|
|
|
|
|
|
|
|
buf->st_mode &= ~_IFMT & NO_W;
|
|
|
|
|
|
|
|
if (!*path)
|
|
|
|
{
|
|
|
|
buf->st_nlink = PROC_LINK_COUNT;
|
|
|
|
buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
path++;
|
|
|
|
for (int i = 0; proc_listing[i]; i++)
|
2002-05-28 09:55:40 +08:00
|
|
|
if (pathmatch (path, proc_listing[i]))
|
|
|
|
{
|
2002-05-02 12:26:05 +08:00
|
|
|
if (proc_fhandlers[i] != FH_PROC)
|
|
|
|
buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf->st_mode &= NO_X;
|
|
|
|
buf->st_mode |= S_IFREG;
|
|
|
|
}
|
2002-05-28 09:55:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2002-05-02 12:26:05 +08:00
|
|
|
}
|
|
|
|
set_errno (ENOENT);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent *
|
|
|
|
fhandler_proc::readdir (DIR * dir)
|
|
|
|
{
|
|
|
|
if (dir->__d_position >= PROC_LINK_COUNT)
|
|
|
|
{
|
|
|
|
winpids pids;
|
|
|
|
int found = 0;
|
|
|
|
for (unsigned i = 0; i < pids.npids; i++)
|
2002-06-02 14:07:01 +08:00
|
|
|
if (found++ == dir->__d_position - PROC_LINK_COUNT)
|
|
|
|
{
|
|
|
|
__small_sprintf (dir->__d_dirent->d_name, "%d", pids[i]->pid);
|
|
|
|
dir->__d_position++;
|
|
|
|
return dir->__d_dirent;
|
|
|
|
}
|
2002-06-01 04:30:36 +08:00
|
|
|
set_errno (ENMFILE);
|
2002-05-02 12:26:05 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy (dir->__d_dirent->d_name, proc_listing[dir->__d_position++]);
|
|
|
|
syscall_printf ("%p = readdir (%p) (%s)", &dir->__d_dirent, dir,
|
2002-05-28 09:55:40 +08:00
|
|
|
dir->__d_dirent->d_name);
|
2002-05-02 12:26:05 +08:00
|
|
|
return dir->__d_dirent;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_proc::open (path_conv *pc, int flags, mode_t mode)
|
|
|
|
{
|
|
|
|
int proc_file_no = -1;
|
|
|
|
|
|
|
|
int res = fhandler_virtual::open (pc, flags, mode);
|
|
|
|
if (!res)
|
|
|
|
goto out;
|
|
|
|
|
2002-07-02 09:36:15 +08:00
|
|
|
set_nohandle (true);
|
|
|
|
|
2002-05-02 12:26:05 +08:00
|
|
|
const char *path;
|
|
|
|
|
2002-05-03 10:43:45 +08:00
|
|
|
path = get_name () + proc_len;
|
2002-05-02 12:26:05 +08:00
|
|
|
|
|
|
|
if (!*path)
|
|
|
|
{
|
2002-05-04 11:24:35 +08:00
|
|
|
if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
|
2002-05-28 09:55:40 +08:00
|
|
|
{
|
|
|
|
set_errno (EEXIST);
|
|
|
|
res = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
2002-05-04 11:24:35 +08:00
|
|
|
else if (flags & O_WRONLY)
|
2002-05-28 09:55:40 +08:00
|
|
|
{
|
|
|
|
set_errno (EISDIR);
|
|
|
|
res = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
2002-05-02 12:26:05 +08:00
|
|
|
else
|
2002-05-28 09:55:40 +08:00
|
|
|
{
|
|
|
|
flags |= O_DIROPEN;
|
|
|
|
goto success;
|
|
|
|
}
|
2002-05-02 12:26:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
proc_file_no = -1;
|
|
|
|
for (int i = 0; proc_listing[i]; i++)
|
|
|
|
if (path_prefix_p (proc_listing[i], path + 1, strlen (proc_listing[i])))
|
|
|
|
{
|
2002-05-28 09:55:40 +08:00
|
|
|
proc_file_no = i;
|
|
|
|
if (proc_fhandlers[i] != FH_PROC)
|
|
|
|
{
|
|
|
|
if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
|
|
|
|
{
|
|
|
|
set_errno (EEXIST);
|
|
|
|
res = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else if (flags & O_WRONLY)
|
|
|
|
{
|
|
|
|
set_errno (EISDIR);
|
|
|
|
res = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flags |= O_DIROPEN;
|
|
|
|
goto success;
|
|
|
|
}
|
|
|
|
}
|
2002-05-02 12:26:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (proc_file_no == -1)
|
|
|
|
{
|
2002-05-04 11:24:35 +08:00
|
|
|
if (flags & O_CREAT)
|
2002-05-28 09:55:40 +08:00
|
|
|
{
|
|
|
|
set_errno (EROFS);
|
|
|
|
res = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
2002-05-02 12:26:05 +08:00
|
|
|
else
|
2002-05-28 09:55:40 +08:00
|
|
|
{
|
|
|
|
set_errno (ENOENT);
|
|
|
|
res = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
2002-05-02 12:26:05 +08:00
|
|
|
}
|
2002-05-04 11:24:35 +08:00
|
|
|
if (flags & O_WRONLY)
|
2002-05-02 12:26:05 +08:00
|
|
|
{
|
|
|
|
set_errno (EROFS);
|
|
|
|
res = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
2002-05-12 09:37:48 +08:00
|
|
|
|
|
|
|
fileid = proc_file_no;
|
2002-07-02 09:36:15 +08:00
|
|
|
if (!fill_filebuf ())
|
|
|
|
{
|
|
|
|
res = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
2002-05-12 09:37:48 +08:00
|
|
|
|
|
|
|
if (flags & O_APPEND)
|
|
|
|
position = filesize;
|
|
|
|
else
|
|
|
|
position = 0;
|
|
|
|
|
|
|
|
success:
|
|
|
|
res = 1;
|
2002-07-02 03:03:26 +08:00
|
|
|
set_flags ((flags & ~O_TEXT) | O_BINARY);
|
2002-05-12 09:37:48 +08:00
|
|
|
set_open_status ();
|
|
|
|
out:
|
|
|
|
syscall_printf ("%d = fhandler_proc::open (%p, %d)", res, flags, mode);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-07-02 09:36:15 +08:00
|
|
|
bool
|
2002-05-12 09:37:48 +08:00
|
|
|
fhandler_proc::fill_filebuf ()
|
|
|
|
{
|
|
|
|
switch (fileid)
|
2002-05-02 12:26:05 +08:00
|
|
|
{
|
|
|
|
case PROC_VERSION:
|
|
|
|
{
|
2002-05-28 09:55:40 +08:00
|
|
|
if (!filebuf)
|
|
|
|
{
|
2002-06-03 00:42:02 +08:00
|
|
|
struct utsname uts_name;
|
|
|
|
uname (&uts_name);
|
|
|
|
bufalloc = strlen (uts_name.sysname) + 1 + strlen (uts_name.release) +
|
|
|
|
1 + strlen (uts_name.version) + 2;
|
2002-10-31 05:05:18 +08:00
|
|
|
filebuf = (char *) realloc (filebuf, bufalloc);
|
2002-06-03 00:42:02 +08:00
|
|
|
filesize = __small_sprintf (filebuf, "%s %s %s\n", uts_name.sysname,
|
|
|
|
uts_name.release, uts_name.version);
|
2002-05-28 09:55:40 +08:00
|
|
|
}
|
|
|
|
break;
|
2002-05-02 12:26:05 +08:00
|
|
|
}
|
|
|
|
case PROC_UPTIME:
|
|
|
|
{
|
2002-10-31 05:05:18 +08:00
|
|
|
filebuf = (char *) realloc (filebuf, bufalloc = 80);
|
2002-05-28 09:55:40 +08:00
|
|
|
filesize = format_proc_uptime (filebuf, bufalloc);
|
|
|
|
break;
|
2002-05-12 09:37:48 +08:00
|
|
|
}
|
|
|
|
case PROC_STAT:
|
|
|
|
{
|
2002-10-31 05:05:18 +08:00
|
|
|
filebuf = (char *) realloc (filebuf, bufalloc = 2048);
|
2002-05-28 09:55:40 +08:00
|
|
|
filesize = format_proc_stat (filebuf, bufalloc);
|
|
|
|
break;
|
2002-05-12 09:37:48 +08:00
|
|
|
}
|
|
|
|
case PROC_LOADAVG:
|
|
|
|
{
|
2002-05-28 09:55:40 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2002-10-31 05:05:18 +08:00
|
|
|
filebuf = (char *) realloc (filebuf, bufalloc = 16);
|
2002-05-28 09:55:40 +08:00
|
|
|
filesize = __small_sprintf (filebuf, "%u.%02u %u.%02u %u.%02u\n",
|
|
|
|
0, 0, 0, 0, 0, 0);
|
|
|
|
break;
|
2002-05-12 09:37:48 +08:00
|
|
|
}
|
|
|
|
case PROC_MEMINFO:
|
|
|
|
{
|
2002-10-31 05:05:18 +08:00
|
|
|
filebuf = (char *) realloc (filebuf, bufalloc = 2048);
|
2002-05-28 09:55:40 +08:00
|
|
|
filesize = format_proc_meminfo (filebuf, bufalloc);
|
|
|
|
break;
|
2002-05-02 12:26:05 +08:00
|
|
|
}
|
2003-03-10 04:10:25 +08:00
|
|
|
case PROC_CPUINFO:
|
|
|
|
{
|
|
|
|
filebuf = (char *) realloc (filebuf, bufalloc = 16384);
|
|
|
|
filesize = format_proc_cpuinfo (filebuf, bufalloc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PROC_PARTITIONS:
|
|
|
|
{
|
|
|
|
filebuf = (char *) realloc (filebuf, bufalloc = 4096);
|
|
|
|
filesize = format_proc_partitions (filebuf, bufalloc);
|
|
|
|
break;
|
|
|
|
}
|
2002-05-02 12:26:05 +08:00
|
|
|
}
|
2002-07-02 09:36:15 +08:00
|
|
|
return true;
|
2002-05-12 09:37:48 +08:00
|
|
|
}
|
2002-05-02 12:26:05 +08:00
|
|
|
|
2003-03-09 01:56:13 +08:00
|
|
|
static __off64_t
|
2002-05-12 09:37:48 +08:00
|
|
|
format_proc_meminfo (char *destbuf, size_t maxsize)
|
|
|
|
{
|
|
|
|
unsigned long mem_total = 0UL, mem_free = 0UL, swap_total = 0UL,
|
2002-05-28 09:55:40 +08:00
|
|
|
swap_free = 0UL;
|
2002-05-12 09:37:48 +08:00
|
|
|
MEMORYSTATUS memory_status;
|
|
|
|
GlobalMemoryStatus (&memory_status);
|
|
|
|
mem_total = memory_status.dwTotalPhys;
|
|
|
|
mem_free = memory_status.dwAvailPhys;
|
|
|
|
swap_total = memory_status.dwTotalPageFile;
|
|
|
|
swap_free = memory_status.dwAvailPageFile;
|
|
|
|
return __small_sprintf (destbuf, " total: used: free:\n"
|
2002-05-28 09:55:40 +08:00
|
|
|
"Mem: %10lu %10lu %10lu\n"
|
|
|
|
"Swap: %10lu %10lu %10lu\n"
|
|
|
|
"MemTotal: %10lu kB\n"
|
|
|
|
"MemFree: %10lu kB\n"
|
|
|
|
"MemShared: 0 kB\n"
|
|
|
|
"HighTotal: 0 kB\n"
|
|
|
|
"HighFree: 0 kB\n"
|
|
|
|
"LowTotal: %10lu kB\n"
|
|
|
|
"LowFree: %10lu kB\n"
|
|
|
|
"SwapTotal: %10lu kB\n"
|
|
|
|
"SwapFree: %10lu kB\n",
|
|
|
|
mem_total, mem_total - mem_free, mem_free,
|
|
|
|
swap_total, swap_total - swap_free, swap_free,
|
|
|
|
mem_total >> 10, mem_free >> 10,
|
|
|
|
mem_total >> 10, mem_free >> 10,
|
|
|
|
swap_total >> 10, swap_free >> 10);
|
2002-05-12 09:37:48 +08:00
|
|
|
}
|
|
|
|
|
2003-03-09 01:56:13 +08:00
|
|
|
static __off64_t
|
2002-05-12 09:37:48 +08:00
|
|
|
format_proc_uptime (char *destbuf, size_t maxsize)
|
|
|
|
{
|
|
|
|
unsigned long long uptime = 0ULL, idle_time = 0ULL;
|
|
|
|
SYSTEM_PROCESSOR_TIMES spt;
|
|
|
|
|
2003-03-10 04:10:25 +08:00
|
|
|
if (!GetSystemTimes ((FILETIME *) &spt.IdleTime, (FILETIME *) &spt.KernelTime,
|
|
|
|
(FILETIME *) &spt.UserTime)
|
|
|
|
&& GetLastError () == ERROR_PROC_NOT_FOUND)
|
2002-05-12 09:37:48 +08:00
|
|
|
{
|
2003-03-10 04:10:25 +08:00
|
|
|
NTSTATUS ret = NtQuerySystemInformation (SystemProcessorTimes, (PVOID) &spt,
|
|
|
|
sizeof spt, NULL);
|
|
|
|
if (!ret && GetLastError () == ERROR_PROC_NOT_FOUND)
|
|
|
|
{
|
|
|
|
uptime = GetTickCount () / 10;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else if (ret != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
__seterrno_from_win_error (RtlNtStatusToDosError (ret));
|
|
|
|
debug_printf("NtQuerySystemInformation: ret = %d, "
|
|
|
|
"Dos(ret) = %d",
|
|
|
|
ret, RtlNtStatusToDosError (ret));
|
|
|
|
return 0;
|
|
|
|
}
|
2002-05-12 09:37:48 +08:00
|
|
|
}
|
2003-03-10 04:10:25 +08:00
|
|
|
idle_time = spt.IdleTime.QuadPart / 100000ULL;
|
|
|
|
uptime = (spt.KernelTime.QuadPart +
|
|
|
|
spt.UserTime.QuadPart) / 100000ULL;
|
|
|
|
out:
|
2002-05-02 12:26:05 +08:00
|
|
|
|
2002-05-12 09:37:48 +08:00
|
|
|
return __small_sprintf (destbuf, "%U.%02u %U.%02u\n",
|
2002-05-28 09:55:40 +08:00
|
|
|
uptime / 100, long (uptime % 100),
|
|
|
|
idle_time / 100, long (idle_time % 100));
|
2002-05-12 09:37:48 +08:00
|
|
|
}
|
|
|
|
|
2003-03-09 01:56:13 +08:00
|
|
|
static __off64_t
|
2002-05-12 09:37:48 +08:00
|
|
|
format_proc_stat (char *destbuf, size_t maxsize)
|
|
|
|
{
|
|
|
|
unsigned long long user_time = 0ULL, kernel_time = 0ULL, idle_time = 0ULL;
|
|
|
|
unsigned long pages_in = 0UL, pages_out = 0UL, interrupt_count = 0UL,
|
2002-05-28 09:55:40 +08:00
|
|
|
context_switches = 0UL, swap_in = 0UL, swap_out = 0UL;
|
2002-05-12 09:37:48 +08:00
|
|
|
time_t boot_time = 0;
|
|
|
|
|
|
|
|
if (wincap.is_winnt ())
|
|
|
|
{
|
|
|
|
NTSTATUS ret;
|
|
|
|
SYSTEM_PROCESSOR_TIMES spt;
|
|
|
|
SYSTEM_PERFORMANCE_INFORMATION spi;
|
|
|
|
SYSTEM_TIME_OF_DAY_INFORMATION stodi;
|
2002-05-30 15:45:30 +08:00
|
|
|
ret = NtQuerySystemInformation (SystemProcessorTimes,
|
2002-05-28 09:55:40 +08:00
|
|
|
(PVOID) &spt,
|
|
|
|
sizeof spt, NULL);
|
2002-05-12 09:37:48 +08:00
|
|
|
if (ret == STATUS_SUCCESS)
|
2002-05-30 15:45:30 +08:00
|
|
|
ret = NtQuerySystemInformation (SystemPerformanceInformation,
|
2002-05-28 09:55:40 +08:00
|
|
|
(PVOID) &spi,
|
|
|
|
sizeof spi, NULL);
|
2002-05-12 09:37:48 +08:00
|
|
|
if (ret == STATUS_SUCCESS)
|
2002-05-30 15:45:30 +08:00
|
|
|
ret = NtQuerySystemInformation (SystemTimeOfDayInformation,
|
2002-05-28 09:55:40 +08:00
|
|
|
(PVOID) &stodi,
|
|
|
|
sizeof stodi, NULL);
|
2002-05-12 09:37:48 +08:00
|
|
|
if (ret != STATUS_SUCCESS)
|
2002-05-28 09:55:40 +08:00
|
|
|
{
|
|
|
|
__seterrno_from_win_error (RtlNtStatusToDosError (ret));
|
|
|
|
debug_printf("NtQuerySystemInformation: ret = %d, "
|
|
|
|
"Dos(ret) = %d",
|
|
|
|
ret, RtlNtStatusToDosError (ret));
|
|
|
|
return 0;
|
|
|
|
}
|
2002-09-01 00:37:51 +08:00
|
|
|
kernel_time = (spt.KernelTime.QuadPart - spt.IdleTime.QuadPart) * HZ / 10000000ULL;
|
|
|
|
user_time = spt.UserTime.QuadPart * HZ / 10000000ULL;
|
|
|
|
idle_time = spt.IdleTime.QuadPart * HZ / 10000000ULL;
|
2002-05-12 09:37:48 +08:00
|
|
|
interrupt_count = spt.InterruptCount;
|
|
|
|
pages_in = spi.PagesRead;
|
|
|
|
pages_out = spi.PagefilePagesWritten + spi.MappedFilePagesWritten;
|
|
|
|
/*
|
|
|
|
* Note: there is no distinction made in this structure between pages
|
|
|
|
* read from the page file and pages read from mapped files, but there
|
|
|
|
* is such a distinction made when it comes to writing. Goodness knows
|
|
|
|
* why. The value of swap_in, then, will obviously be wrong but its our
|
|
|
|
* best guess.
|
|
|
|
*/
|
|
|
|
swap_in = spi.PagesRead;
|
|
|
|
swap_out = spi.PagefilePagesWritten;
|
|
|
|
context_switches = spi.ContextSwitches;
|
|
|
|
boot_time = to_time_t ((FILETIME *) &stodi.BootTime.QuadPart);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* else
|
|
|
|
* {
|
|
|
|
* There are only two relevant performance counters on Windows 95/98/me,
|
|
|
|
* VMM/cPageIns and VMM/cPageOuts. The extra effort needed to read these
|
|
|
|
* counters is by no means worth it.
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
return __small_sprintf (destbuf, "cpu %U %U %U %U\n"
|
2002-05-28 09:55:40 +08:00
|
|
|
"page %u %u\n"
|
|
|
|
"swap %u %u\n"
|
|
|
|
"intr %u\n"
|
|
|
|
"ctxt %u\n"
|
|
|
|
"btime %u\n",
|
|
|
|
user_time, 0ULL,
|
|
|
|
kernel_time, idle_time,
|
|
|
|
pages_in, pages_out,
|
|
|
|
swap_in, swap_out,
|
|
|
|
interrupt_count,
|
|
|
|
context_switches,
|
|
|
|
boot_time);
|
2002-05-02 12:26:05 +08:00
|
|
|
}
|
2003-03-10 04:10:25 +08:00
|
|
|
|
|
|
|
#define read_value(x,y) \
|
|
|
|
do {\
|
|
|
|
dwCount = BUFSIZE; \
|
|
|
|
if ((dwError = RegQueryValueEx (hKey, x, NULL, &dwType, (BYTE *) szBuffer, &dwCount)), \
|
|
|
|
(dwError != ERROR_SUCCESS && dwError != ERROR_MORE_DATA)) \
|
|
|
|
{ \
|
|
|
|
__seterrno_from_win_error (dwError); \
|
|
|
|
debug_printf ("RegQueryValueEx failed retcode %d", dwError); \
|
|
|
|
return 0; \
|
|
|
|
} \
|
|
|
|
if (dwType != y) \
|
|
|
|
{ \
|
|
|
|
debug_printf ("Value %s had an unexpected type (expected %d, found %d)", y, dwType); \
|
|
|
|
return 0; \
|
|
|
|
}\
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define print(x) \
|
|
|
|
do { \
|
|
|
|
strcpy (bufptr, x), \
|
|
|
|
bufptr += sizeof (x) - 1; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
cpuid (unsigned *a, unsigned *b, unsigned *c, unsigned *d, unsigned in)
|
|
|
|
{
|
|
|
|
asm ("cpuid"
|
|
|
|
: "=a" (*a),
|
|
|
|
"=b" (*b),
|
|
|
|
"=c" (*c),
|
|
|
|
"=d" (*d)
|
|
|
|
: "a" (in));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
can_set_flag (unsigned flag)
|
|
|
|
{
|
|
|
|
unsigned r1, r2;
|
|
|
|
asm("pushfl\n"
|
|
|
|
"popl %0\n"
|
|
|
|
"movl %0, %1\n"
|
|
|
|
"xorl %2, %0\n"
|
|
|
|
"pushl %0\n"
|
|
|
|
"popfl\n"
|
|
|
|
"pushfl\n"
|
|
|
|
"popl %0\n"
|
|
|
|
"pushl %1\n"
|
|
|
|
"popfl\n"
|
|
|
|
: "=&r" (r1), "=&r" (r2)
|
|
|
|
: "ir" (flag)
|
|
|
|
);
|
|
|
|
return ((r1 ^ r2) & flag) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __off64_t
|
|
|
|
format_proc_cpuinfo (char *destbuf, size_t maxsize)
|
|
|
|
{
|
|
|
|
SYSTEM_INFO siSystemInfo;
|
|
|
|
HKEY hKey;
|
|
|
|
DWORD dwError, dwCount, dwType;
|
|
|
|
DWORD dwOldThreadAffinityMask;
|
|
|
|
int cpu_number;
|
|
|
|
const int BUFSIZE = 256;
|
|
|
|
CHAR szBuffer[BUFSIZE];
|
|
|
|
char *bufptr = destbuf;
|
|
|
|
|
|
|
|
GetSystemInfo (&siSystemInfo);
|
|
|
|
|
|
|
|
for (cpu_number = 0;;cpu_number++)
|
|
|
|
{
|
|
|
|
__small_sprintf (szBuffer, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\%d", cpu_number);
|
|
|
|
|
|
|
|
if ((dwError = RegOpenKeyEx (HKEY_LOCAL_MACHINE, szBuffer, 0, KEY_QUERY_VALUE, &hKey)) != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
if (dwError == ERROR_FILE_NOT_FOUND)
|
|
|
|
break;
|
|
|
|
__seterrno_from_win_error (dwError);
|
|
|
|
debug_printf ("RegOpenKeyEx failed retcode %d", dwError);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
dwOldThreadAffinityMask = SetThreadAffinityMask (GetCurrentThread (), 1 << cpu_number);
|
|
|
|
if (dwOldThreadAffinityMask == 0)
|
|
|
|
debug_printf ("SetThreadAffinityMask failed %E");
|
|
|
|
// I'm not sure whether the thread changes processor immediately
|
|
|
|
// and I'm not sure whether this function will cause the thread to be rescheduled
|
|
|
|
low_priority_sleep (0);
|
|
|
|
|
|
|
|
bool has_cpuid = false;
|
|
|
|
|
|
|
|
if (!can_set_flag (0x00040000))
|
|
|
|
debug_printf ("386 processor - no cpuid");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
debug_printf ("486 processor");
|
|
|
|
if (can_set_flag (0x00200000))
|
|
|
|
{
|
|
|
|
debug_printf ("processor supports CPUID instruction");
|
|
|
|
has_cpuid = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
debug_printf ("processor does not support CPUID instruction");
|
|
|
|
}
|
|
|
|
|
2003-03-18 17:30:03 +08:00
|
|
|
|
|
|
|
if (!has_cpuid)
|
2003-03-10 04:10:25 +08:00
|
|
|
{
|
|
|
|
bufptr += __small_sprintf (bufptr, "processor : %d\n", cpu_number);
|
|
|
|
read_value ("VendorIdentifier", REG_SZ);
|
|
|
|
bufptr += __small_sprintf (bufptr, "vendor id : %s\n", szBuffer);
|
|
|
|
read_value ("Identifier", REG_SZ);
|
|
|
|
bufptr += __small_sprintf (bufptr, "identifier : %s\n", szBuffer);
|
2003-03-18 17:30:03 +08:00
|
|
|
if (wincap.is_winnt ())
|
|
|
|
{
|
|
|
|
read_value ("~Mhz", REG_DWORD);
|
|
|
|
bufptr += __small_sprintf (bufptr, "cpu MHz : %u\n", *(DWORD *) szBuffer);
|
|
|
|
|
|
|
|
print ("flags :");
|
|
|
|
if (IsProcessorFeaturePresent (PF_3DNOW_INSTRUCTIONS_AVAILABLE))
|
|
|
|
print (" 3dnow");
|
|
|
|
if (IsProcessorFeaturePresent (PF_COMPARE_EXCHANGE_DOUBLE))
|
|
|
|
print (" cx8");
|
|
|
|
if (!IsProcessorFeaturePresent (PF_FLOATING_POINT_EMULATED))
|
|
|
|
print (" fpu");
|
|
|
|
if (IsProcessorFeaturePresent (PF_MMX_INSTRUCTIONS_AVAILABLE))
|
|
|
|
print (" mmx");
|
|
|
|
if (IsProcessorFeaturePresent (PF_PAE_ENABLED))
|
|
|
|
print (" pae");
|
|
|
|
if (IsProcessorFeaturePresent (PF_RDTSC_INSTRUCTION_AVAILABLE))
|
|
|
|
print (" tsc");
|
|
|
|
if (IsProcessorFeaturePresent (PF_XMMI_INSTRUCTIONS_AVAILABLE))
|
|
|
|
print (" sse");
|
|
|
|
if (IsProcessorFeaturePresent (PF_XMMI64_INSTRUCTIONS_AVAILABLE))
|
|
|
|
print (" sse2");
|
|
|
|
}
|
|
|
|
}
|
2003-03-10 04:10:25 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
bufptr += __small_sprintf (bufptr, "processor : %d\n", cpu_number);
|
|
|
|
unsigned maxf, vendor_id[4], unused;
|
2003-03-14 06:32:40 +08:00
|
|
|
cpuid (&maxf, &vendor_id[0], &vendor_id[2], &vendor_id[1], 0);
|
2003-03-10 04:10:25 +08:00
|
|
|
maxf &= 0xffff;
|
|
|
|
vendor_id[3] = 0;
|
|
|
|
bufptr += __small_sprintf (bufptr, "vendor id : %s\n", (char *)vendor_id);
|
2003-03-18 17:30:03 +08:00
|
|
|
unsigned cpu_mhz = 0;
|
|
|
|
if (wincap.is_winnt ())
|
|
|
|
{
|
|
|
|
read_value ("~Mhz", REG_DWORD);
|
|
|
|
cpu_mhz = *(DWORD *)szBuffer;
|
|
|
|
}
|
2003-03-10 04:10:25 +08:00
|
|
|
if (maxf >= 1)
|
|
|
|
{
|
|
|
|
unsigned features2, features1, extra_info, cpuid_sig;
|
|
|
|
cpuid (&cpuid_sig, &extra_info, &features2, &features1, 1);
|
|
|
|
/* unsigned extended_family = (cpuid_sig & 0x0ff00000) >> 20,
|
|
|
|
extended_model = (cpuid_sig & 0x000f0000) >> 16; */
|
|
|
|
unsigned type = (cpuid_sig & 0x00003000) >> 12,
|
|
|
|
family = (cpuid_sig & 0x00000f00) >> 8,
|
|
|
|
model = (cpuid_sig & 0x000000f0) >> 4,
|
|
|
|
stepping = cpuid_sig & 0x0000000f;
|
|
|
|
unsigned brand_id = extra_info & 0x0000000f,
|
|
|
|
cpu_count = (extra_info & 0x00ff0000) >> 16,
|
|
|
|
apic_id = (extra_info & 0xff000000) >> 24;
|
|
|
|
const char *type_str;
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
type_str = "primary processor";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
type_str = "overdrive processor";
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
type_str = "secondary processor";
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
default:
|
|
|
|
type_str = "reserved";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
unsigned maxe = 0;
|
|
|
|
cpuid (&maxe, &unused, &unused, &unused, 0x80000000);
|
|
|
|
if (maxe >= 0x80000004)
|
|
|
|
{
|
|
|
|
unsigned *model_name = (unsigned *) szBuffer;
|
|
|
|
cpuid (&model_name[0], &model_name[1], &model_name[2], &model_name[3], 0x80000002);
|
|
|
|
cpuid (&model_name[4], &model_name[5], &model_name[6], &model_name[7], 0x80000003);
|
|
|
|
cpuid (&model_name[8], &model_name[9], &model_name[10], &model_name[11], 0x80000004);
|
|
|
|
model_name[12] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// could implement a lookup table here if someone needs it
|
|
|
|
strcpy (szBuffer, "unknown");
|
|
|
|
}
|
2003-03-18 17:30:03 +08:00
|
|
|
if (wincap.is_winnt ())
|
|
|
|
{
|
|
|
|
bufptr += __small_sprintf (bufptr, "type : %s\n"
|
|
|
|
"cpu family : %d\n"
|
|
|
|
"model : %d\n"
|
|
|
|
"model name : %s\n"
|
|
|
|
"stepping : %d\n"
|
|
|
|
"brand id : %d\n"
|
|
|
|
"cpu count : %d\n"
|
|
|
|
"apic id : %d\n"
|
|
|
|
"cpu MHz : %d\n"
|
|
|
|
"fpu : %s\n",
|
|
|
|
type_str,
|
|
|
|
family,
|
|
|
|
model,
|
|
|
|
szBuffer,
|
|
|
|
stepping,
|
|
|
|
brand_id,
|
|
|
|
cpu_count,
|
|
|
|
apic_id,
|
|
|
|
cpu_mhz,
|
|
|
|
(features1 & (1 << 0)) ? "yes" : "no");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bufptr += __small_sprintf (bufptr, "type : %s\n"
|
|
|
|
"cpu family : %d\n"
|
|
|
|
"model : %d\n"
|
|
|
|
"model name : %s\n"
|
|
|
|
"stepping : %d\n"
|
|
|
|
"brand id : %d\n"
|
|
|
|
"cpu count : %d\n"
|
|
|
|
"apic id : %d\n"
|
|
|
|
"fpu : %s\n",
|
|
|
|
type_str,
|
|
|
|
family,
|
|
|
|
model,
|
|
|
|
szBuffer,
|
|
|
|
stepping,
|
|
|
|
brand_id,
|
|
|
|
cpu_count,
|
|
|
|
apic_id,
|
|
|
|
(features1 & (1 << 0)) ? "yes" : "no");
|
|
|
|
}
|
2003-03-10 04:10:25 +08:00
|
|
|
print ("flags :");
|
|
|
|
if (features1 & (1 << 0))
|
|
|
|
print (" fpu");
|
|
|
|
if (features1 & (1 << 1))
|
|
|
|
print (" vme");
|
|
|
|
if (features1 & (1 << 2))
|
|
|
|
print (" de");
|
|
|
|
if (features1 & (1 << 3))
|
|
|
|
print (" pse");
|
|
|
|
if (features1 & (1 << 4))
|
|
|
|
print (" tsc");
|
|
|
|
if (features1 & (1 << 5))
|
|
|
|
print (" msr");
|
|
|
|
if (features1 & (1 << 6))
|
|
|
|
print (" pae");
|
|
|
|
if (features1 & (1 << 7))
|
|
|
|
print (" mce");
|
|
|
|
if (features1 & (1 << 8))
|
|
|
|
print (" cx8");
|
|
|
|
if (features1 & (1 << 9))
|
|
|
|
print (" apic");
|
|
|
|
if (features1 & (1 << 11))
|
|
|
|
print (" sep");
|
|
|
|
if (features1 & (1 << 12))
|
|
|
|
print (" mtrr");
|
|
|
|
if (features1 & (1 << 13))
|
|
|
|
print (" pge");
|
|
|
|
if (features1 & (1 << 14))
|
|
|
|
print (" mca");
|
|
|
|
if (features1 & (1 << 15))
|
|
|
|
print (" cmov");
|
|
|
|
if (features1 & (1 << 16))
|
|
|
|
print (" pat");
|
|
|
|
if (features1 & (1 << 17))
|
|
|
|
print (" pse36");
|
|
|
|
if (features1 & (1 << 18))
|
|
|
|
print (" psn");
|
|
|
|
if (features1 & (1 << 19))
|
|
|
|
print (" clfl");
|
|
|
|
if (features1 & (1 << 21))
|
|
|
|
print (" dtes");
|
|
|
|
if (features1 & (1 << 22))
|
|
|
|
print (" acpi");
|
|
|
|
if (features1 & (1 << 23))
|
|
|
|
print (" mmx");
|
|
|
|
if (features1 & (1 << 24))
|
|
|
|
print (" fxsr");
|
|
|
|
if (features1 & (1 << 25))
|
|
|
|
print (" sse");
|
|
|
|
if (features1 & (1 << 26))
|
|
|
|
print (" sse2");
|
|
|
|
if (features1 & (1 << 27))
|
|
|
|
print (" ss");
|
|
|
|
if (features1 & (1 << 28))
|
|
|
|
print (" htt");
|
|
|
|
if (features1 & (1 << 29))
|
|
|
|
print (" tmi");
|
|
|
|
if (features1 & (1 << 30))
|
|
|
|
print (" ia-64");
|
|
|
|
if (features1 & (1 << 31))
|
|
|
|
print (" pbe");
|
|
|
|
if (features2 & (1 << 0))
|
|
|
|
print (" sse3");
|
|
|
|
if (features2 & (1 << 3))
|
|
|
|
print (" mon");
|
|
|
|
if (features2 & (1 << 4))
|
|
|
|
print (" dscpl");
|
|
|
|
if (features2 & (1 << 8))
|
|
|
|
print (" tm2");
|
|
|
|
if (features2 & (1 << 10))
|
|
|
|
print (" cid");
|
|
|
|
}
|
2003-03-18 17:30:03 +08:00
|
|
|
else if (wincap.is_winnt ())
|
2003-03-10 04:10:25 +08:00
|
|
|
{
|
|
|
|
bufptr += __small_sprintf (bufptr, "cpu MHz : %d\n"
|
|
|
|
"fpu : %s\n",
|
|
|
|
cpu_mhz,
|
|
|
|
IsProcessorFeaturePresent (PF_FLOATING_POINT_EMULATED) ? "no" : "yes");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dwOldThreadAffinityMask != 0)
|
|
|
|
SetThreadAffinityMask (GetCurrentThread (), dwOldThreadAffinityMask);
|
|
|
|
|
|
|
|
RegCloseKey (hKey);
|
|
|
|
bufptr += __small_sprintf (bufptr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return bufptr - destbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef read_value
|
|
|
|
|
|
|
|
static __off64_t
|
|
|
|
format_proc_partitions (char *destbuf, size_t maxsize)
|
|
|
|
{
|
|
|
|
char *bufptr = destbuf;
|
|
|
|
print ("major minor #blocks name\n\n");
|
|
|
|
|
|
|
|
if (wincap.is_winnt ())
|
|
|
|
{
|
|
|
|
for (int drive_number=0;;drive_number++)
|
|
|
|
{
|
|
|
|
CHAR szDriveName[MAX_PATH];
|
|
|
|
__small_sprintf (szDriveName, "\\\\.\\PHYSICALDRIVE%d", drive_number);
|
|
|
|
HANDLE hDevice;
|
|
|
|
hDevice = CreateFile (szDriveName,
|
|
|
|
GENERIC_READ,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
if (hDevice == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
if (GetLastError () == ERROR_PATH_NOT_FOUND)
|
|
|
|
break;
|
|
|
|
__seterrno ();
|
|
|
|
debug_printf ("CreateFile %d %E", GetLastError ());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DWORD dwBytesReturned, dwRetCode;
|
|
|
|
DISK_GEOMETRY dg;
|
|
|
|
int buf_size = 4096;
|
|
|
|
char buf[buf_size];
|
|
|
|
dwRetCode = DeviceIoControl (hDevice,
|
|
|
|
IOCTL_DISK_GET_DRIVE_GEOMETRY,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
&dg,
|
|
|
|
sizeof (dg),
|
|
|
|
&dwBytesReturned,
|
|
|
|
NULL);
|
|
|
|
if (!dwRetCode)
|
|
|
|
debug_printf ("DeviceIoControl %E");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bufptr += __small_sprintf (bufptr, "%5d %5d %9U sd%c\n",
|
|
|
|
FH_FLOPPY,
|
|
|
|
drive_number * 16 + 32,
|
|
|
|
(long long)((dg.Cylinders.QuadPart * dg.TracksPerCylinder *
|
|
|
|
dg.SectorsPerTrack * dg.BytesPerSector) >> 6),
|
|
|
|
drive_number + 'a');
|
|
|
|
}
|
|
|
|
while (dwRetCode = DeviceIoControl (hDevice,
|
|
|
|
IOCTL_DISK_GET_DRIVE_LAYOUT,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
(DRIVE_LAYOUT_INFORMATION *) buf,
|
|
|
|
buf_size,
|
|
|
|
&dwBytesReturned,
|
|
|
|
NULL),
|
|
|
|
!dwRetCode && GetLastError () == ERROR_INSUFFICIENT_BUFFER)
|
|
|
|
buf_size *= 2;
|
|
|
|
if (!dwRetCode)
|
|
|
|
debug_printf ("DeviceIoControl %E");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DRIVE_LAYOUT_INFORMATION *dli = (DRIVE_LAYOUT_INFORMATION *) buf;
|
|
|
|
for (unsigned partition = 0; partition < dli->PartitionCount; partition++)
|
|
|
|
{
|
|
|
|
if (dli->PartitionEntry[partition].PartitionLength.QuadPart == 0)
|
|
|
|
continue;
|
|
|
|
bufptr += __small_sprintf (bufptr, "%5d %5d %9U sd%c%d\n",
|
|
|
|
FH_FLOPPY,
|
|
|
|
drive_number * 16 + partition + 33,
|
|
|
|
(long long)(dli->PartitionEntry[partition].PartitionLength.QuadPart >> 6),
|
|
|
|
drive_number + 'a',
|
|
|
|
partition + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle (hDevice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// not worth the effort
|
|
|
|
// you need a 16 bit thunk DLL to access the partition table on Win9x
|
|
|
|
// and then you have to decode it yourself
|
|
|
|
}
|
|
|
|
return bufptr - destbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef print
|