1 /* fhandler_proc.cc: fhandler for /proc virtual filesystem
3 Copyright 2002 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
15 #include <sys/cygwin.h>
25 #include <sys/utsname.h>
28 #define _COMPILING_NEWLIB
31 /* offsets in proc_listing */
32 static const int PROC_LOADAVG = 2; // /proc/loadavg
33 static const int PROC_MEMINFO = 3; // /proc/meminfo
34 static const int PROC_REGISTRY = 4; // /proc/registry
35 static const int PROC_STAT = 5; // /proc/stat
36 static const int PROC_VERSION = 6; // /proc/version
37 static const int PROC_UPTIME = 7; // /proc/uptime
39 /* names of objects in /proc */
40 static const char *proc_listing[] = {
52 static const int PROC_LINK_COUNT = (sizeof(proc_listing) / sizeof(const char *)) - 1;
54 /* FH_PROC in the table below means the file/directory is handles by
57 static const DWORD proc_fhandlers[PROC_LINK_COUNT] = {
68 /* name of the /proc filesystem */
69 const char proc[] = "/proc";
70 const int proc_len = sizeof (proc) - 1;
72 static off_t format_proc_meminfo (char *destbuf, size_t maxsize);
73 static off_t format_proc_stat (char *destbuf, size_t maxsize);
74 static off_t format_proc_uptime (char *destbuf, size_t maxsize);
76 /* auxillary function that returns the fhandler associated with the given path
77 * this is where it would be nice to have pattern matching in C - polymorphism
81 fhandler_proc::get_proc_fhandler (const char *path)
83 debug_printf ("get_proc_fhandler(%s)", path);
85 /* Since this method is called from path_conv::check we can't rely on
86 * it being normalised and therefore the path may have runs of slashes
89 while (SLASH_P (*path))
92 /* Check if this is the root of the virtual filesystem (i.e. /proc). */
96 for (int i = 0; proc_listing[i]; i++)
98 if (path_prefix_p (proc_listing[i], path, strlen (proc_listing[i])))
99 return proc_fhandlers[i];
102 if (pinfo (atoi (path)))
105 bool has_subdir = false;
107 if (SLASH_P (*path++))
114 /* The user is trying to access a non-existent subdirectory of /proc. */
117 /* Return FH_PROC so that we can return EROFS if the user is trying to create
122 /* Returns 0 if path doesn't exist, >0 if path is a directory,
123 * <0 if path is a file.
126 fhandler_proc::exists ()
128 const char *path = get_name ();
129 debug_printf ("exists (%s)", path);
133 for (int i = 0; proc_listing[i]; i++)
134 if (pathmatch (path + 1, proc_listing[i]))
135 return (proc_fhandlers[i] == FH_PROC) ? -1 : 1;
139 fhandler_proc::fhandler_proc ():
140 fhandler_virtual (FH_PROC)
144 fhandler_proc::fhandler_proc (DWORD devtype):
145 fhandler_virtual (devtype)
150 fhandler_proc::fstat (struct __stat64 *buf, path_conv *pc)
152 const char *path = get_name ();
153 debug_printf ("fstat (%s)", path);
156 (void) fhandler_base::fstat (buf, pc);
158 buf->st_mode &= ~_IFMT & NO_W;
162 buf->st_nlink = PROC_LINK_COUNT;
163 buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
169 for (int i = 0; proc_listing[i]; i++)
170 if (pathmatch (path, proc_listing[i]))
172 if (proc_fhandlers[i] != FH_PROC)
173 buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
176 buf->st_mode &= NO_X;
177 buf->st_mode |= S_IFREG;
187 fhandler_proc::readdir (DIR * dir)
189 if (dir->__d_position >= PROC_LINK_COUNT)
193 for (unsigned i = 0; i < pids.npids; i++)
194 if (found++ == dir->__d_position - PROC_LINK_COUNT)
196 __small_sprintf (dir->__d_dirent->d_name, "%d", pids[i]->pid);
198 return dir->__d_dirent;
204 strcpy (dir->__d_dirent->d_name, proc_listing[dir->__d_position++]);
205 syscall_printf ("%p = readdir (%p) (%s)", &dir->__d_dirent, dir,
206 dir->__d_dirent->d_name);
207 return dir->__d_dirent;
211 fhandler_proc::open (path_conv *pc, int flags, mode_t mode)
213 int proc_file_no = -1;
215 int res = fhandler_virtual::open (pc, flags, mode);
221 path = get_name () + proc_len;
225 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
231 else if (flags & O_WRONLY)
245 for (int i = 0; proc_listing[i]; i++)
246 if (path_prefix_p (proc_listing[i], path + 1, strlen (proc_listing[i])))
249 if (proc_fhandlers[i] != FH_PROC)
251 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
257 else if (flags & O_WRONLY)
271 if (proc_file_no == -1)
286 if (flags & O_WRONLY)
293 fileid = proc_file_no;
296 if (flags & O_APPEND)
303 set_flags (flags & ~O_TEXT, O_BINARY);
306 syscall_printf ("%d = fhandler_proc::open (%p, %d)", res, flags, mode);
311 fhandler_proc::fill_filebuf ()
319 struct utsname uts_name;
321 bufalloc = strlen (uts_name.sysname) + 1 + strlen (uts_name.release) +
322 1 + strlen (uts_name.version) + 2;
323 filebuf = (char *) cmalloc (HEAP_BUF, bufalloc);
324 filesize = __small_sprintf (filebuf, "%s %s %s\n", uts_name.sysname,
325 uts_name.release, uts_name.version);
332 filebuf = (char *) cmalloc (HEAP_BUF, bufalloc = 80);
333 filesize = format_proc_uptime (filebuf, bufalloc);
339 filebuf = (char *) cmalloc (HEAP_BUF, bufalloc = 2048);
340 filesize = format_proc_stat (filebuf, bufalloc);
346 * not really supported - Windows doesn't keep track of these values
347 * Windows 95/98/me does have the KERNEL/CPUUsage performance counter
351 filebuf = (char *) cmalloc (HEAP_BUF, bufalloc = 16);
352 filesize = __small_sprintf (filebuf, "%u.%02u %u.%02u %u.%02u\n",
359 filebuf = (char *) cmalloc (HEAP_BUF, bufalloc = 2048);
360 filesize = format_proc_meminfo (filebuf, bufalloc);
368 format_proc_meminfo (char *destbuf, size_t maxsize)
370 unsigned long mem_total = 0UL, mem_free = 0UL, swap_total = 0UL,
372 MEMORYSTATUS memory_status;
373 GlobalMemoryStatus (&memory_status);
374 mem_total = memory_status.dwTotalPhys;
375 mem_free = memory_status.dwAvailPhys;
376 swap_total = memory_status.dwTotalPageFile;
377 swap_free = memory_status.dwAvailPageFile;
378 return __small_sprintf (destbuf, " total: used: free:\n"
379 "Mem: %10lu %10lu %10lu\n"
380 "Swap: %10lu %10lu %10lu\n"
381 "MemTotal: %10lu kB\n"
382 "MemFree: %10lu kB\n"
386 "LowTotal: %10lu kB\n"
387 "LowFree: %10lu kB\n"
388 "SwapTotal: %10lu kB\n"
389 "SwapFree: %10lu kB\n",
390 mem_total, mem_total - mem_free, mem_free,
391 swap_total, swap_total - swap_free, swap_free,
392 mem_total >> 10, mem_free >> 10,
393 mem_total >> 10, mem_free >> 10,
394 swap_total >> 10, swap_free >> 10);
399 format_proc_uptime (char *destbuf, size_t maxsize)
401 unsigned long long uptime = 0ULL, idle_time = 0ULL;
402 SYSTEM_PROCESSOR_TIMES spt;
404 NTSTATUS ret = NtQuerySystemInformation (SystemProcessorTimes, (PVOID) &spt,
406 if (!ret && GetLastError () == ERROR_PROC_NOT_FOUND)
407 uptime = GetTickCount() / 10;
408 else if (ret != STATUS_SUCCESS)
410 __seterrno_from_win_error (RtlNtStatusToDosError (ret));
411 debug_printf("NtQuerySystemInformation: ret = %d, "
413 ret, RtlNtStatusToDosError (ret));
418 idle_time = spt.IdleTime.QuadPart / 100000ULL;
419 uptime = (spt.InterruptTime.QuadPart + spt.KernelTime.QuadPart +
420 spt.IdleTime.QuadPart + spt.UserTime.QuadPart +
421 spt.DpcTime.QuadPart) / 100000ULL;
424 return __small_sprintf (destbuf, "%U.%02u %U.%02u\n",
425 uptime / 100, long (uptime % 100),
426 idle_time / 100, long (idle_time % 100));
431 format_proc_stat (char *destbuf, size_t maxsize)
433 unsigned long long user_time = 0ULL, kernel_time = 0ULL, idle_time = 0ULL;
434 unsigned long pages_in = 0UL, pages_out = 0UL, interrupt_count = 0UL,
435 context_switches = 0UL, swap_in = 0UL, swap_out = 0UL;
436 time_t boot_time = 0;
438 if (wincap.is_winnt ())
441 SYSTEM_PROCESSOR_TIMES spt;
442 SYSTEM_PERFORMANCE_INFORMATION spi;
443 SYSTEM_TIME_OF_DAY_INFORMATION stodi;
444 ret = NtQuerySystemInformation (SystemProcessorTimes,
447 if (ret == STATUS_SUCCESS)
448 ret = NtQuerySystemInformation (SystemPerformanceInformation,
451 if (ret == STATUS_SUCCESS)
452 ret = NtQuerySystemInformation (SystemTimeOfDayInformation,
455 if (ret != STATUS_SUCCESS)
457 __seterrno_from_win_error (RtlNtStatusToDosError (ret));
458 debug_printf("NtQuerySystemInformation: ret = %d, "
460 ret, RtlNtStatusToDosError (ret));
463 kernel_time = (spt.KernelTime.QuadPart + spt.InterruptTime.QuadPart + spt.DpcTime.QuadPart) / 100000ULL;
464 user_time = spt.UserTime.QuadPart / 100000ULL;
465 idle_time = spt.IdleTime.QuadPart / 100000ULL;
466 interrupt_count = spt.InterruptCount;
467 pages_in = spi.PagesRead;
468 pages_out = spi.PagefilePagesWritten + spi.MappedFilePagesWritten;
470 * Note: there is no distinction made in this structure between pages
471 * read from the page file and pages read from mapped files, but there
472 * is such a distinction made when it comes to writing. Goodness knows
473 * why. The value of swap_in, then, will obviously be wrong but its our
476 swap_in = spi.PagesRead;
477 swap_out = spi.PagefilePagesWritten;
478 context_switches = spi.ContextSwitches;
479 boot_time = to_time_t ((FILETIME *) &stodi.BootTime.QuadPart);
484 * There are only two relevant performance counters on Windows 95/98/me,
485 * VMM/cPageIns and VMM/cPageOuts. The extra effort needed to read these
486 * counters is by no means worth it.
489 return __small_sprintf (destbuf, "cpu %U %U %U %U\n"
496 kernel_time, idle_time,