On Aug 16 10:56, Brian Inglis wrote:
> On 2021-08-16 08:51, Corinna Vinschen via Cygwin wrote:
> > On Aug 13 12:56, David Balažic via Cygwin wrote:
> > > [...]
> > > $ cat /proc/partitions
> > > major minor  #blocks  name   win-mounts
> > > 
> > >      8     0 1000204632 sda
> > >      8     1    102400 sda1
> > >      8     2     16384 sda2
> > >      8     3 999571820 sda3   C:\
> > >      8     4    510976 sda4
> > >      8    16  62522712 sdb
> > >      8    17   4096000 sdb1   D:\
> > >      8    18  58424664 sdb2
> > >      8    16  62522712 sdb
> > >      8    17   4096000 sdb1   D:\
> > >      8    18  58424664 sdb2   E:\
> > > [...]
> > > So the second listing shows sdb twice. Also E: does not seem to exist
> > > (see below).
> > 
> > So, second report.  Which OS is that on?  I don't have any physical
> > Windows machine anymore, so I have a bit of a problem to reproduce this.
> > 
> > As I wrote in https://cygwin.com/pipermail/cygwin/2021-August/249012.html,
> > I don't see how Cygwin would be able to duplicate entries, unless the OS
> > itself duplicates entries.
> > 
> > Somebody who can duplicate this issue would have to single step through
> > function format_proc_partitions, see
> > 
> > https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=winsup/cygwin/fhandler_proc.cc;h=66d19ab82f;hb=HEAD#l1686
> > 
> > and check what actually happens in the above cases.
> 
> Could those with the symptoms not provide useful information using a script
> run in an elevated admin shell listing the relevant contents of Windows
> objects accessed by format_proc_partitions from /proc/sys/ similar to the
> attached script and log from my system, where the correspondence is obvious?

Not sure if that helps, but it's worth a try.

Other than that, I created a standalone application, shamelessly copying
the code from Cygwin's function format_proc_partitions() and tweaked it
to drop Cygwin internals.

The core of the function doesn't actually need Cygwin internals. It's
basically a double loop calling Windows functions and just using Cygwin
internals to print out major and minor numbers and the device name.  The
standalone application simply fakes those.

This allows any person who *can* reproduce the problem to run the code
under a debugger like GDB and debug what's going wrong.

I attached the code to this mail.  Store it under the name "proc_partition.c".  
Compile it in two different ways, first with optimization:

$ gcc -g -O2 -o proc_partition_O2 proc_partition.c -lntdll

Run it:

$ ./proc_partition_O2

If the output contains duplicates, take a note, but otherwise
ignore it for now.

Next, build it without optimization for easier debugging:

$ gcc -g -o proc_partition proc_partition.c -lntdll

Run it:

$ ./proc_partition

If the output contains duplicates, great!  Now you can run it under
GDB and step through and see where it goes awry.  If it doesn't, you'll
have to debug the optimized version, which is sometimes a bit weird.
I'd be really glad if this allows us to reproduce the issue.

If none of them reproduces the issue... uhm... anyway, let's try first.


Thanks,
Corinna
#include <stdio.h>
#include <windows.h>
#include <winioctl.h>
#include <winternl.h>
#include <wchar.h>
#include <wctype.h>

NTSTATUS NTAPI NtOpenDirectoryObject (PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES);
NTSTATUS NTAPI NtQueryDirectoryObject (HANDLE, PVOID, ULONG, BOOLEAN,
                                       BOOLEAN, PULONG, PULONG);

typedef struct _DIRECTORY_BASIC_INFORMATION
{
  UNICODE_STRING ObjectName;
  UNICODE_STRING ObjectTypeName;
} DIRECTORY_BASIC_INFORMATION, *PDIRECTORY_BASIC_INFORMATION;

#define DIRECTORY_QUERY 1
#define NT_MAX_PATH     32768

char buf[NT_MAX_PATH];
char ioctl_buf[NT_MAX_PATH];
WCHAR mp_buf[NT_MAX_PATH];

typedef enum { false, true } bool;

int
main ()
{
  OBJECT_ATTRIBUTES attr;
  IO_STATUS_BLOCK io;
  NTSTATUS status;
  HANDLE dirhdl;
  WCHAR fpath[MAX_PATH];
  WCHAR gpath[MAX_PATH];
  DWORD len;

  /* Note that device ids and names are just faked here, not using the internal
     Cygwin code to generate device major/minor numbers and device names.
     There's *no* guarantee that the device name and the device major number
     is the same as in Cygwin. */
  char dev_name = '`';

  /* Open \Device object directory. */
  wchar_t wpath[MAX_PATH] = L"\\Device";
  UNICODE_STRING upath = {14, 16, wpath};

  InitializeObjectAttributes (&attr, &upath, OBJ_CASE_INSENSITIVE, NULL, NULL);
  status = NtOpenDirectoryObject (&dirhdl, DIRECTORY_QUERY, &attr);
  if (!NT_SUCCESS (status))
    {
      fprintf (stderr, "NtOpenDirectoryObject, status 0x%08x", status);
      return 1;
    }

  /* Traverse \Device directory ... */
  PDIRECTORY_BASIC_INFORMATION dbi = (PDIRECTORY_BASIC_INFORMATION)
                                     alloca (640);
  BOOLEAN restart = TRUE;
  bool got_one = false;
  ULONG context = 0;
  while (NT_SUCCESS (NtQueryDirectoryObject (dirhdl, dbi, 640, TRUE, restart,
                                             &context, NULL)))
    {
      HANDLE devhdl;
      PARTITION_INFORMATION_EX *pix = NULL;
      PARTITION_INFORMATION *pi = NULL;
      DWORD bytes_read;
      DWORD part_cnt = 0;
      unsigned long long size;

      restart = FALSE;
      /* ... and check for a "Harddisk[0-9]*" entry. */
      if (dbi->ObjectName.Length < 9 * sizeof (WCHAR)
          || wcsncasecmp (dbi->ObjectName.Buffer, L"Harddisk", 8) != 0
          || !iswdigit (dbi->ObjectName.Buffer[8]))
        continue;
      /* Got it.  Now construct the path to the entire disk, which is
         "\\Device\\HarddiskX\\Partition0", and open the disk with
         minimum permissions. */
      //unsigned long drive_num = wcstoul (dbi->ObjectName.Buffer + 8, NULL, 
10);
      wcscpy (wpath, dbi->ObjectName.Buffer);
      PWCHAR wpart = wpath + dbi->ObjectName.Length / sizeof (WCHAR);
      wcpcpy (wpart, L"\\Partition0");
      upath.Length = dbi->ObjectName.Length + 22;
      upath.MaximumLength = upath.Length + sizeof (WCHAR);
      InitializeObjectAttributes (&attr, &upath, OBJ_CASE_INSENSITIVE,
                                  dirhdl, NULL);
      status = NtOpenFile (&devhdl, READ_CONTROL, &attr, &io,
                           FILE_SHARE_VALID_FLAGS, 0);
      if (!NT_SUCCESS (status))
        {
          fprintf (stderr, "NtOpenFile(%ls), status 0x%08x",
                   upath.Buffer, status);
          continue;
        }
      if (!got_one)
        {
          printf ("major minor  #blocks  name   win-mounts\n\n");
          got_one = true;
        }
      /* Fetch partition info for the entire disk to get its size. */
      if (DeviceIoControl (devhdl, IOCTL_DISK_GET_PARTITION_INFO_EX, NULL, 0,
                           ioctl_buf, NT_MAX_PATH, &bytes_read, NULL))
        {
          pix = (PARTITION_INFORMATION_EX *) ioctl_buf;
          size = pix->PartitionLength.QuadPart;
        }
      else if (DeviceIoControl (devhdl, IOCTL_DISK_GET_PARTITION_INFO, NULL, 0,
                                ioctl_buf, NT_MAX_PATH, &bytes_read, NULL))
        {
          pi = (PARTITION_INFORMATION *) ioctl_buf;
          size = pi->PartitionLength.QuadPart;
        }
      else
        {
          fprintf (stderr, "DeviceIoControl (%ls, "
                   "IOCTL_DISK_GET_PARTITION_INFO{_EX}) %lu",
                   upath.Buffer, (unsigned long) GetLastError ());
          size = 0;
        }
      //device dev (drive_num, 0);
      ++dev_name;
      printf ("%5d %5d %9llu sd%c\n",
              8, (dev_name - 'a') * 16, size >> 10, dev_name);

      /* Fetch drive layout info to get size of all partitions on the disk. */
      if (DeviceIoControl (devhdl, IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
                           NULL, 0, ioctl_buf, NT_MAX_PATH, &bytes_read, NULL))
        {
          PDRIVE_LAYOUT_INFORMATION_EX pdlix = (PDRIVE_LAYOUT_INFORMATION_EX)
                                               ioctl_buf;
          part_cnt = pdlix->PartitionCount;
          pix = pdlix->PartitionEntry;
        }
      else if (DeviceIoControl (devhdl, IOCTL_DISK_GET_DRIVE_LAYOUT,
                                NULL, 0, ioctl_buf, NT_MAX_PATH, &bytes_read, 
NULL))
        {
          PDRIVE_LAYOUT_INFORMATION pdli = (PDRIVE_LAYOUT_INFORMATION) 
ioctl_buf;
          part_cnt = pdli->PartitionCount;
          pi = pdli->PartitionEntry;
        }
      else
        fprintf (stderr, "DeviceIoControl(%ls, "
                  "IOCTL_DISK_GET_DRIVE_LAYOUT{_EX}): %lu",
                  upath.Buffer, (unsigned long) GetLastError ());
      /* Loop over partitions. */
      if (pix || pi)
        for (DWORD i = 0; i < part_cnt && i < 64; ++i)
          {
            DWORD part_num;

            if (pix)
              {
                size = pix->PartitionLength.QuadPart;
                part_num = pix->PartitionNumber;
                ++pix;
              }
            else
              {
                size = pi->PartitionLength.QuadPart;
                part_num = pi->PartitionNumber;
                ++pi;
              }
            /* A partition number of 0 denotes an extended partition or a
               filler entry as described in fhandler_dev_floppy::lock_partition.
               Just skip. */
            if (part_num == 0)
              continue;

            //device dev (drive_num, part_num);
            printf ("%5d %5d %9llu sd%c%d   ",
                    8, (dev_name - 'a') * 16 + part_num, size >> 10,
                    dev_name, part_num);
            /* Check if the partition is mounted in Windows and, if so,
               print the mount point list. */
            swprintf (fpath, sizeof fpath,
                      L"\\\\?\\GLOBALROOT\\Device\\%ls\\Partition%u\\",
                      dbi->ObjectName.Buffer, part_num);
            if (GetVolumeNameForVolumeMountPointW (fpath, gpath, MAX_PATH)
                && GetVolumePathNamesForVolumeNameW (gpath, mp_buf,
                                                     NT_MAX_PATH, &len))
              {
                for (PWCHAR p = mp_buf; *p; p = wcschr (p, L'\0') + 1)
                  printf (" %ls", p);
              }
            puts ("");
          }
      NtClose (devhdl);
    }
  NtClose (dirhdl);

  if (!got_one)
    return 1;

  return 0;
}
-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

Reply via email to