1 /* fhandler_proc.cc: fhandler for /proc virtual filesystem
3 Copyright 2002, 2003, 2004, 2005 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
11 #define _WIN32_WINNT 0x0501
16 #include <sys/cygwin.h>
26 #include <sys/utsname.h>
27 #include <sys/param.h>
32 #define _COMPILING_NEWLIB
35 /* offsets in proc_listing */
36 static const int PROC_LOADAVG = 2; // /proc/loadavg
37 static const int PROC_MEMINFO = 3; // /proc/meminfo
38 static const int PROC_REGISTRY = 4; // /proc/registry
39 static const int PROC_STAT = 5; // /proc/stat
40 static const int PROC_VERSION = 6; // /proc/version
41 static const int PROC_UPTIME = 7; // /proc/uptime
42 static const int PROC_CPUINFO = 8; // /proc/cpuinfo
43 static const int PROC_PARTITIONS = 9; // /proc/partitions
44 static const int PROC_SELF = 10; // /proc/self
46 /* names of objects in /proc */
47 static const char *proc_listing[] = {
62 static const int PROC_LINK_COUNT = (sizeof (proc_listing) / sizeof (const char *)) - 1;
64 /* FH_PROC in the table below means the file/directory is handles by
67 static const DWORD proc_fhandlers[PROC_LINK_COUNT] = {
81 /* name of the /proc filesystem */
82 const char proc[] = "/proc";
83 const int proc_len = sizeof (proc) - 1;
85 static _off64_t format_proc_meminfo (char *destbuf, size_t maxsize);
86 static _off64_t format_proc_stat (char *destbuf, size_t maxsize);
87 static _off64_t format_proc_uptime (char *destbuf, size_t maxsize);
88 static _off64_t format_proc_cpuinfo (char *destbuf, size_t maxsize);
89 static _off64_t format_proc_partitions (char *destbuf, size_t maxsize);
91 /* Auxillary function that returns the fhandler associated with the given path
92 this is where it would be nice to have pattern matching in C - polymorphism
93 just doesn't cut it. */
95 fhandler_proc::get_proc_fhandler (const char *path)
97 debug_printf ("get_proc_fhandler(%s)", path);
99 /* Since this method is called from path_conv::check we can't rely on
100 it being normalised and therefore the path may have runs of slashes
102 while (isdirsep (*path))
105 /* Check if this is the root of the virtual filesystem (i.e. /proc). */
109 for (int i = 0; proc_listing[i]; i++)
111 if (path_prefix_p (proc_listing[i], path, strlen (proc_listing[i])))
112 return proc_fhandlers[i];
115 if (pinfo (atoi (path)))
118 bool has_subdir = false;
120 if (isdirsep (*path++))
127 /* The user is trying to access a non-existent subdirectory of /proc. */
130 /* Return FH_PROC so that we can return EROFS if the user is trying to create
135 /* Returns 0 if path doesn't exist, >0 if path is a directory,
136 -1 if path is a file, -2 if it's a symlink. */
138 fhandler_proc::exists ()
140 const char *path = get_name ();
141 debug_printf ("exists (%s)", path);
145 for (int i = 0; proc_listing[i]; i++)
146 if (pathmatch (path + 1, proc_listing[i]))
149 return (proc_fhandlers[i] == FH_PROC) ? (i == PROC_SELF ? -2 : -1) : 1;
154 fhandler_proc::fhandler_proc ():
160 fhandler_proc::fstat (struct __stat64 *buf)
162 const char *path = get_name ();
163 debug_printf ("fstat (%s)", path);
166 (void) fhandler_base::fstat (buf);
168 buf->st_mode &= ~_IFMT & NO_W;
172 buf->st_nlink = PROC_LINK_COUNT;
173 buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
179 for (int i = 0; proc_listing[i]; i++)
180 if (pathmatch (path, proc_listing[i]))
182 if (proc_fhandlers[i] != FH_PROC)
183 buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
184 else if (i == PROC_SELF)
185 buf->st_mode = S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
188 buf->st_mode &= NO_X;
189 buf->st_mode |= S_IFREG;
199 fhandler_proc::readdir (DIR * dir)
201 if (dir->__d_position >= PROC_LINK_COUNT)
203 winpids pids ((DWORD) 0);
205 for (unsigned i = 0; i < pids.npids; i++)
206 if (found++ == dir->__d_position - PROC_LINK_COUNT)
208 __small_sprintf (dir->__d_dirent->d_name, "%d", pids[i]->pid);
210 return dir->__d_dirent;
215 strcpy (dir->__d_dirent->d_name, proc_listing[dir->__d_position++]);
216 syscall_printf ("%p = readdir (%p) (%s)", &dir->__d_dirent, dir,
217 dir->__d_dirent->d_name);
218 return dir->__d_dirent;
222 fhandler_proc::open (int flags, mode_t mode)
224 int proc_file_no = -1;
226 int res = fhandler_virtual::open (flags, mode);
234 path = get_name () + proc_len;
238 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
244 else if (flags & O_WRONLY)
258 for (int i = 0; proc_listing[i]; i++)
259 if (path_prefix_p (proc_listing[i], path + 1, strlen (proc_listing[i])))
262 if (proc_fhandlers[i] != FH_PROC)
264 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
270 else if (flags & O_WRONLY)
284 if (proc_file_no == -1)
299 if (flags & O_WRONLY)
306 fileid = proc_file_no;
307 if (!fill_filebuf ())
313 if (flags & O_APPEND)
320 set_flags ((flags & ~O_TEXT) | O_BINARY);
323 syscall_printf ("%d = fhandler_proc::open (%p, %d)", res, flags, mode);
328 fhandler_proc::fill_filebuf ()
336 struct utsname uts_name;
338 bufalloc = strlen (uts_name.sysname) + 1 + strlen (uts_name.release) +
339 1 + strlen (uts_name.version) + 2;
340 filebuf = (char *) realloc (filebuf, bufalloc);
341 filesize = __small_sprintf (filebuf, "%s %s %s\n", uts_name.sysname,
342 uts_name.release, uts_name.version);
348 filebuf = (char *) realloc (filebuf, bufalloc = 80);
349 filesize = format_proc_uptime (filebuf, bufalloc);
354 filebuf = (char *) realloc (filebuf, bufalloc = 16384);
355 filesize = format_proc_stat (filebuf, bufalloc);
361 * not really supported - Windows doesn't keep track of these values
362 * Windows 95/98/me does have the KERNEL/CPUUsage performance counter
365 filebuf = (char *) realloc (filebuf, bufalloc = 16);
366 filesize = __small_sprintf (filebuf, "%u.%02u %u.%02u %u.%02u\n",
372 filebuf = (char *) realloc (filebuf, bufalloc = 2048);
373 filesize = format_proc_meminfo (filebuf, bufalloc);
378 filebuf = (char *) realloc (filebuf, bufalloc = 16384);
379 filesize = format_proc_cpuinfo (filebuf, bufalloc);
382 case PROC_PARTITIONS:
384 filebuf = (char *) realloc (filebuf, bufalloc = 4096);
385 filesize = format_proc_partitions (filebuf, bufalloc);
390 filebuf = (char *) realloc (filebuf, bufalloc = 32);
391 filesize = __small_sprintf (filebuf, "%d", getpid ());
398 format_proc_meminfo (char *destbuf, size_t maxsize)
400 unsigned long mem_total = 0UL, mem_free = 0UL, swap_total = 0UL,
402 MEMORYSTATUS memory_status;
403 GlobalMemoryStatus (&memory_status);
404 mem_total = memory_status.dwTotalPhys;
405 mem_free = memory_status.dwAvailPhys;
406 PSYSTEM_PAGEFILE_INFORMATION spi = NULL;
408 NTSTATUS ret = STATUS_SUCCESS;
409 spi = (PSYSTEM_PAGEFILE_INFORMATION) malloc (size);
412 ret = NtQuerySystemInformation (SystemPagefileInformation, (PVOID) spi,
414 if (ret == STATUS_INFO_LENGTH_MISMATCH)
417 spi = (PSYSTEM_PAGEFILE_INFORMATION) malloc (size);
419 ret = NtQuerySystemInformation (SystemPagefileInformation,
420 (PVOID) spi, size, &size);
423 if (!spi || ret || (!ret && GetLastError () == ERROR_PROC_NOT_FOUND))
425 swap_total = memory_status.dwTotalPageFile - mem_total;
426 swap_free = memory_status.dwAvailPageFile - mem_total;
430 PSYSTEM_PAGEFILE_INFORMATION spp = spi;
433 swap_total += spp->CurrentSize * getpagesize ();
434 swap_free += (spp->CurrentSize - spp->TotalUsed) * getpagesize ();
436 while (spp->NextEntryOffset
437 && (spp = (PSYSTEM_PAGEFILE_INFORMATION)
438 ((char *) spp + spp->NextEntryOffset)));
442 return __small_sprintf (destbuf, " total: used: free:\n"
443 "Mem: %10lu %10lu %10lu\n"
444 "Swap: %10lu %10lu %10lu\n"
445 "MemTotal: %10lu kB\n"
446 "MemFree: %10lu kB\n"
450 "LowTotal: %10lu kB\n"
451 "LowFree: %10lu kB\n"
452 "SwapTotal: %10lu kB\n"
453 "SwapFree: %10lu kB\n",
454 mem_total, mem_total - mem_free, mem_free,
455 swap_total, swap_total - swap_free, swap_free,
456 mem_total >> 10, mem_free >> 10,
457 mem_total >> 10, mem_free >> 10,
458 swap_total >> 10, swap_free >> 10);
462 format_proc_uptime (char *destbuf, size_t maxsize)
464 unsigned long long uptime = 0ULL, idle_time = 0ULL;
465 SYSTEM_PROCESSOR_TIMES spt;
467 if (!GetSystemTimes ((FILETIME *) &spt.IdleTime, (FILETIME *) &spt.KernelTime,
468 (FILETIME *) &spt.UserTime)
469 && GetLastError () == ERROR_PROC_NOT_FOUND)
471 NTSTATUS ret = NtQuerySystemInformation (SystemProcessorTimes, (PVOID) &spt,
473 if (!ret && GetLastError () == ERROR_PROC_NOT_FOUND)
475 uptime = GetTickCount () / 10;
478 else if (ret != STATUS_SUCCESS)
480 __seterrno_from_nt_status (ret);
481 debug_printf("NtQuerySystemInformation: ret %d, Dos(ret) %E", ret);
485 idle_time = spt.IdleTime.QuadPart / 100000ULL;
486 uptime = (spt.KernelTime.QuadPart +
487 spt.UserTime.QuadPart) / 100000ULL;
490 return __small_sprintf (destbuf, "%U.%02u %U.%02u\n",
491 uptime / 100, long (uptime % 100),
492 idle_time / 100, long (idle_time % 100));
496 format_proc_stat (char *destbuf, size_t maxsize)
498 unsigned long pages_in = 0UL, pages_out = 0UL, interrupt_count = 0UL,
499 context_switches = 0UL, swap_in = 0UL, swap_out = 0UL;
500 time_t boot_time = 0;
502 char *eobuf = destbuf;
503 if (!wincap.is_winnt ())
504 eobuf += __small_sprintf (destbuf, "cpu %U %U %U %U\n", 0ULL, 0ULL, 0ULL, 0ULL);
508 SYSTEM_PERFORMANCE_INFORMATION spi;
509 SYSTEM_TIME_OF_DAY_INFORMATION stodi;
511 SYSTEM_BASIC_INFORMATION sbi;
512 if ((ret = NtQuerySystemInformation (SystemBasicInformation,
513 (PVOID) &sbi, sizeof sbi, NULL))
516 __seterrno_from_nt_status (ret);
517 debug_printf ("NtQuerySystemInformation: ret %d, Dos(ret) %E", ret);
518 sbi.NumberProcessors = 1;
521 SYSTEM_PROCESSOR_TIMES spt[sbi.NumberProcessors];
522 ret = NtQuerySystemInformation (SystemProcessorTimes, (PVOID) spt,
523 sizeof spt[0] * sbi.NumberProcessors, NULL);
525 if (ret == STATUS_SUCCESS)
527 unsigned long long user_time = 0ULL, kernel_time = 0ULL, idle_time = 0ULL;
528 for (int i = 0; i < sbi.NumberProcessors; i++)
530 kernel_time += (spt[i].KernelTime.QuadPart - spt[i].IdleTime.QuadPart) * HZ / 10000000ULL;
531 user_time += spt[i].UserTime.QuadPart * HZ / 10000000ULL;
532 idle_time += spt[i].IdleTime.QuadPart * HZ / 10000000ULL;
535 eobuf += __small_sprintf (eobuf, "cpu %U %U %U %U\n",
536 user_time, 0ULL, kernel_time, idle_time);
537 user_time = 0ULL, kernel_time = 0ULL, idle_time = 0ULL;
538 for (int i = 0; i < sbi.NumberProcessors; i++)
540 interrupt_count += spt[i].InterruptCount;
541 kernel_time = (spt[i].KernelTime.QuadPart - spt[i].IdleTime.QuadPart) * HZ / 10000000ULL;
542 user_time = spt[i].UserTime.QuadPart * HZ / 10000000ULL;
543 idle_time = spt[i].IdleTime.QuadPart * HZ / 10000000ULL;
544 eobuf += __small_sprintf (eobuf, "cpu%d %U %U %U %U\n", i,
545 user_time, 0ULL, kernel_time, idle_time);
548 ret = NtQuerySystemInformation (SystemPerformanceInformation,
549 (PVOID) &spi, sizeof spi, NULL);
551 if (ret == STATUS_SUCCESS)
552 ret = NtQuerySystemInformation (SystemTimeOfDayInformation,
555 if (ret != STATUS_SUCCESS)
557 __seterrno_from_nt_status (ret);
558 debug_printf("NtQuerySystemInformation: ret %d, Dos(ret) %E", ret);
561 pages_in = spi.PagesRead;
562 pages_out = spi.PagefilePagesWritten + spi.MappedFilePagesWritten;
564 * Note: there is no distinction made in this structure between pages
565 * read from the page file and pages read from mapped files, but there
566 * is such a distinction made when it comes to writing. Goodness knows
567 * why. The value of swap_in, then, will obviously be wrong but its our
570 swap_in = spi.PagesRead;
571 swap_out = spi.PagefilePagesWritten;
572 context_switches = spi.ContextSwitches;
573 boot_time = to_time_t ((FILETIME *) &stodi.BootTime.QuadPart);
578 * There are only two relevant performance counters on Windows 95/98/me,
579 * VMM/cPageIns and VMM/cPageOuts. The extra effort needed to read these
580 * counters is by no means worth it.
583 eobuf += __small_sprintf (eobuf, "page %u %u\n"
593 return eobuf - destbuf;
596 #define read_value(x,y) \
599 if ((dwError = RegQueryValueEx (hKey, x, NULL, &dwType, (BYTE *) szBuffer, &dwCount)), \
600 (dwError != ERROR_SUCCESS && dwError != ERROR_MORE_DATA)) \
602 __seterrno_from_win_error (dwError); \
603 debug_printf ("RegQueryValueEx failed retcode %d", dwError); \
608 debug_printf ("Value %s had an unexpected type (expected %d, found %d)", y, dwType); \
615 strcpy (bufptr, x), \
616 bufptr += sizeof (x) - 1; \
620 format_proc_cpuinfo (char *destbuf, size_t maxsize)
622 SYSTEM_INFO siSystemInfo;
624 DWORD dwError, dwCount, dwType;
625 DWORD dwOldThreadAffinityMask;
627 const int BUFSIZE = 256;
628 CHAR szBuffer[BUFSIZE];
629 char *bufptr = destbuf;
631 GetSystemInfo (&siSystemInfo);
633 for (cpu_number = 0;;cpu_number++)
635 __small_sprintf (szBuffer, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\%d", cpu_number);
637 if ((dwError = RegOpenKeyEx (HKEY_LOCAL_MACHINE, szBuffer, 0, KEY_QUERY_VALUE, &hKey)) != ERROR_SUCCESS)
639 if (dwError == ERROR_FILE_NOT_FOUND)
641 __seterrno_from_win_error (dwError);
642 debug_printf ("RegOpenKeyEx failed retcode %d", dwError);
646 dwOldThreadAffinityMask = SetThreadAffinityMask (GetCurrentThread (), 1 << cpu_number);
647 if (dwOldThreadAffinityMask == 0)
648 debug_printf ("SetThreadAffinityMask failed %E");
649 // I'm not sure whether the thread changes processor immediately
650 // and I'm not sure whether this function will cause the thread to be rescheduled
651 low_priority_sleep (0);
653 bool has_cpuid = false;
655 if (!can_set_flag (0x00040000))
656 debug_printf ("386 processor - no cpuid");
659 debug_printf ("486 processor");
660 if (can_set_flag (0x00200000))
662 debug_printf ("processor supports CPUID instruction");
666 debug_printf ("processor does not support CPUID instruction");
672 bufptr += __small_sprintf (bufptr, "processor : %d\n", cpu_number);
673 read_value ("VendorIdentifier", REG_SZ);
674 bufptr += __small_sprintf (bufptr, "vendor_id : %s\n", szBuffer);
675 read_value ("Identifier", REG_SZ);
676 bufptr += __small_sprintf (bufptr, "identifier : %s\n", szBuffer);
677 if (wincap.is_winnt ())
679 read_value ("~Mhz", REG_DWORD);
680 bufptr += __small_sprintf (bufptr, "cpu MHz : %u\n", *(DWORD *) szBuffer);
683 if (IsProcessorFeaturePresent (PF_3DNOW_INSTRUCTIONS_AVAILABLE))
685 if (IsProcessorFeaturePresent (PF_COMPARE_EXCHANGE_DOUBLE))
687 if (!IsProcessorFeaturePresent (PF_FLOATING_POINT_EMULATED))
689 if (IsProcessorFeaturePresent (PF_MMX_INSTRUCTIONS_AVAILABLE))
691 if (IsProcessorFeaturePresent (PF_PAE_ENABLED))
693 if (IsProcessorFeaturePresent (PF_RDTSC_INSTRUCTION_AVAILABLE))
695 if (IsProcessorFeaturePresent (PF_XMMI_INSTRUCTIONS_AVAILABLE))
697 if (IsProcessorFeaturePresent (PF_XMMI64_INSTRUCTIONS_AVAILABLE))
703 bufptr += __small_sprintf (bufptr, "processor : %d\n", cpu_number);
704 unsigned maxf, vendor_id[4], unused;
705 cpuid (&maxf, &vendor_id[0], &vendor_id[2], &vendor_id[1], 0);
709 // vendor identification
710 bool is_amd = false, is_intel = false;
711 if (!strcmp ((char*)vendor_id, "AuthenticAMD"))
713 else if (!strcmp ((char*)vendor_id, "GenuineIntel"))
716 bufptr += __small_sprintf (bufptr, "vendor_id : %s\n", (char *)vendor_id);
717 unsigned cpu_mhz = 0;
718 if (wincap.is_winnt ())
720 read_value ("~Mhz", REG_DWORD);
721 cpu_mhz = *(DWORD *)szBuffer;
725 unsigned features2, features1, extra_info, cpuid_sig;
726 cpuid (&cpuid_sig, &extra_info, &features2, &features1, 1);
727 /* unsigned extended_family = (cpuid_sig & 0x0ff00000) >> 20,
728 extended_model = (cpuid_sig & 0x000f0000) >> 16; */
729 unsigned type = (cpuid_sig & 0x00003000) >> 12,
730 family = (cpuid_sig & 0x00000f00) >> 8,
731 model = (cpuid_sig & 0x000000f0) >> 4,
732 stepping = cpuid_sig & 0x0000000f;
733 unsigned brand_id = extra_info & 0x0000000f,
734 cpu_count = (extra_info & 0x00ff0000) >> 16,
735 apic_id = (extra_info & 0xff000000) >> 24;
736 const char *type_str;
740 type_str = "primary processor";
743 type_str = "overdrive processor";
746 type_str = "secondary processor";
750 type_str = "reserved";
754 cpuid (&maxe, &unused, &unused, &unused, 0x80000000);
755 if (maxe >= 0x80000004)
757 unsigned *model_name = (unsigned *) szBuffer;
758 cpuid (&model_name[0], &model_name[1], &model_name[2], &model_name[3], 0x80000002);
759 cpuid (&model_name[4], &model_name[5], &model_name[6], &model_name[7], 0x80000003);
760 cpuid (&model_name[8], &model_name[9], &model_name[10], &model_name[11], 0x80000004);
765 // could implement a lookup table here if someone needs it
766 strcpy (szBuffer, "unknown");
768 if (wincap.is_winnt ())
770 bufptr += __small_sprintf (bufptr, "type : %s\n"
789 (features1 & (1 << 0)) ? "yes" : "no");
793 bufptr += __small_sprintf (bufptr, "type : %s\n"
810 (features1 & (1 << 0)) ? "yes" : "no");
813 if (features1 & (1 << 0))
815 if (features1 & (1 << 1))
817 if (features1 & (1 << 2))
819 if (features1 & (1 << 3))
821 if (features1 & (1 << 4))
823 if (features1 & (1 << 5))
825 if (features1 & (1 << 6))
827 if (features1 & (1 << 7))
829 if (features1 & (1 << 8))
831 if (features1 & (1 << 9))
833 if (features1 & (1 << 11))
835 if (features1 & (1 << 12))
837 if (features1 & (1 << 13))
839 if (features1 & (1 << 14))
841 if (features1 & (1 << 15))
843 if (features1 & (1 << 16))
845 if (features1 & (1 << 17))
847 if (features1 & (1 << 18))
849 if (features1 & (1 << 19))
851 if (is_intel && features1 & (1 << 21))
853 if (is_intel && features1 & (1 << 22))
855 if (features1 & (1 << 23))
857 if (features1 & (1 << 24))
859 if (features1 & (1 << 25))
863 if (features1 & (1 << 26))
865 if (features1 & (1 << 27))
867 if (features1 & (1 << 28))
869 if (features1 & (1 << 29))
871 if (features1 & (1 << 30))
873 if (features1 & (1 << 31))
876 if (features2 & (1 << 0))
878 if (features2 & (1 << 3))
880 if (features2 & (1 << 4))
882 if (features2 & (1 << 7))
884 if (features2 & (1 << 8))
886 if (features2 & (1 << 10))
890 if (is_amd && maxe >= 0x80000001)
892 // uses AMD extended calls to check
893 // for 3dnow and 3dnow extended support
894 // (source: AMD Athlon Processor Recognition Application Note)
896 if (maxe >= 0x80000001) // has basic capabilities
898 cpuid (&unused, &unused, &unused, &features2, 0x80000001);
900 if (features2 & (1 << 11))
902 if (features2 & (1 << 19))
904 if (features2 & (1 << 22))
906 if (features2 & (1 << 29))
908 if (features2 & (1 << 30)) // 31th bit is on
910 if (features2 & (1 << 31)) // 32th bit (highest) is on
915 else if (wincap.is_winnt ())
917 bufptr += __small_sprintf (bufptr, "cpu MHz : %d\n"
920 IsProcessorFeaturePresent (PF_FLOATING_POINT_EMULATED) ? "no" : "yes");
923 if (dwOldThreadAffinityMask != 0)
924 SetThreadAffinityMask (GetCurrentThread (), dwOldThreadAffinityMask);
927 bufptr += __small_sprintf (bufptr, "\n");
930 return bufptr - destbuf;
936 format_proc_partitions (char *destbuf, size_t maxsize)
938 char *bufptr = destbuf;
939 print ("major minor #blocks name\n\n");
941 if (wincap.is_winnt ())
943 for (int drive_number=0;;drive_number++)
945 CHAR szDriveName[CYG_MAX_PATH];
946 __small_sprintf (szDriveName, "\\\\.\\PHYSICALDRIVE%d", drive_number);
948 hDevice = CreateFile (szDriveName,
950 FILE_SHARE_READ | FILE_SHARE_WRITE,
955 if (hDevice == INVALID_HANDLE_VALUE)
957 if (GetLastError () == ERROR_PATH_NOT_FOUND)
960 debug_printf ("CreateFile %d %E", GetLastError ());
965 DWORD dwBytesReturned, dwRetCode;
967 dwRetCode = DeviceIoControl (hDevice,
968 IOCTL_DISK_GET_DRIVE_GEOMETRY,
976 debug_printf ("DeviceIoControl %E");
980 dev.parsedisk (drive_number, 0);
981 bufptr += __small_sprintf (bufptr, "%5d %5d %9U %s\n",
984 (long long)((dg.Cylinders.QuadPart * dg.TracksPerCylinder *
985 dg.SectorsPerTrack * dg.BytesPerSector) >> 10),
988 size_t buf_size = 8192;
993 memset (buf, 0, buf_size);
994 rc = DeviceIoControl (hDevice, IOCTL_DISK_GET_DRIVE_LAYOUT,
995 NULL, 0, (DRIVE_LAYOUT_INFORMATION *) buf,
996 buf_size, &dwBytesReturned, NULL);
999 else if (GetLastError () == ERROR_INSUFFICIENT_BUFFER)
1006 debug_printf ("DeviceIoControl %E");
1009 DRIVE_LAYOUT_INFORMATION *dli = (DRIVE_LAYOUT_INFORMATION *) buf;
1010 for (unsigned partition = 0; partition < dli->PartitionCount; partition++)
1012 if (!dli->PartitionEntry[partition].PartitionLength.QuadPart
1013 || !dli->PartitionEntry[partition].RecognizedPartition)
1016 dev.parsedisk (drive_number, dli->PartitionEntry[partition].PartitionNumber);
1017 bufptr += __small_sprintf (bufptr, "%5d %5d %9U %s\n",
1018 dev.major, dev.minor,
1019 (long long)(dli->PartitionEntry[partition].PartitionLength.QuadPart >> 10),
1024 CloseHandle (hDevice);
1030 // not worth the effort
1031 // you need a 16 bit thunk DLL to access the partition table on Win9x
1032 // and then you have to decode it yourself
1034 return bufptr - destbuf;