2012-03-30 02:02:54 +08:00
|
|
|
/* fhandler_dev.cc, Implement /dev.
|
|
|
|
|
|
|
|
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. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
|
|
|
#include <stdlib.h>
|
2012-04-02 19:08:07 +08:00
|
|
|
#include <sys/statvfs.h>
|
2012-03-30 02:02:54 +08:00
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
2012-04-01 01:38:00 +08:00
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2012-03-30 02:02:54 +08:00
|
|
|
#include "devices.h"
|
|
|
|
|
|
|
|
#define _COMPILING_NEWLIB
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#define dev_prefix_len (sizeof ("/dev"))
|
2012-04-01 01:38:00 +08:00
|
|
|
#define dev_storage_scan_start (dev_storage + 1)
|
|
|
|
#define dev_storage_size (dev_storage_end - dev_storage_scan_start)
|
2012-03-30 02:02:54 +08:00
|
|
|
|
|
|
|
static int
|
|
|
|
device_cmp (const void *a, const void *b)
|
|
|
|
{
|
2016-06-23 22:56:41 +08:00
|
|
|
return strcmp (((const device *) a)->name (),
|
|
|
|
((const device *) b)->name () + dev_prefix_len);
|
2012-03-30 02:02:54 +08:00
|
|
|
}
|
|
|
|
|
2012-04-01 01:38:00 +08:00
|
|
|
fhandler_dev::fhandler_dev () :
|
|
|
|
fhandler_disk_file (), devidx (NULL), dir_exists (true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-02 19:08:07 +08:00
|
|
|
int
|
|
|
|
fhandler_dev::open (int flags, mode_t mode)
|
|
|
|
{
|
|
|
|
if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
|
|
|
|
{
|
|
|
|
set_errno (EEXIST);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (flags & O_WRONLY)
|
|
|
|
{
|
|
|
|
set_errno (EISDIR);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-30 17:44:47 +08:00
|
|
|
/* Filter O_CREAT flag to avoid creating a file called /dev accidentally. */
|
|
|
|
int ret = fhandler_disk_file::open (flags & ~O_CREAT, mode);
|
2012-04-02 19:08:07 +08:00
|
|
|
if (!ret)
|
|
|
|
{
|
2013-10-31 22:26:42 +08:00
|
|
|
/* Open a fake handle to \\Device\\Null */
|
|
|
|
ret = open_null (flags);
|
2013-10-30 17:44:47 +08:00
|
|
|
dir_exists = false;
|
2012-04-02 19:08:07 +08:00
|
|
|
}
|
2013-10-30 17:44:47 +08:00
|
|
|
return ret;
|
2012-04-02 19:08:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_dev::close ()
|
|
|
|
{
|
|
|
|
return fhandler_disk_file::close ();
|
|
|
|
}
|
|
|
|
|
2013-01-21 12:34:52 +08:00
|
|
|
int __reg2
|
2013-04-23 17:44:36 +08:00
|
|
|
fhandler_dev::fstat (struct stat *st)
|
2012-04-02 19:08:07 +08:00
|
|
|
{
|
|
|
|
/* If /dev really exists on disk, return correct disk information. */
|
|
|
|
if (pc.fs_got_fs ())
|
|
|
|
return fhandler_disk_file::fstat (st);
|
|
|
|
/* Otherwise fake virtual filesystem. */
|
|
|
|
fhandler_base::fstat (st);
|
|
|
|
st->st_ino = 2;
|
|
|
|
st->st_mode = S_IFDIR | STD_RBITS | STD_XBITS;
|
|
|
|
st->st_nlink = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-01 09:20:37 +08:00
|
|
|
int __reg2
|
2012-04-02 19:08:07 +08:00
|
|
|
fhandler_dev::fstatvfs (struct statvfs *sfs)
|
|
|
|
{
|
|
|
|
int ret = -1, opened = 0;
|
|
|
|
HANDLE fh = get_handle ();
|
|
|
|
|
2013-10-30 17:44:47 +08:00
|
|
|
if (!fh)
|
2012-04-02 19:08:07 +08:00
|
|
|
{
|
|
|
|
if (!open (O_RDONLY, 0))
|
|
|
|
return -1;
|
|
|
|
opened = 1;
|
|
|
|
}
|
2013-10-30 17:44:47 +08:00
|
|
|
if (pc.fs_got_fs ())
|
|
|
|
ret = fhandler_disk_file::fstatvfs (sfs);
|
|
|
|
else
|
2012-04-02 19:08:07 +08:00
|
|
|
{
|
2013-10-30 17:44:47 +08:00
|
|
|
/* Virtual file system. Just return an empty buffer with a few values
|
|
|
|
set to something useful similar to Linux. */
|
|
|
|
memset (sfs, 0, sizeof (*sfs));
|
|
|
|
sfs->f_bsize = sfs->f_frsize = 4096;
|
|
|
|
sfs->f_flag = ST_RDONLY;
|
|
|
|
sfs->f_namemax = NAME_MAX;
|
|
|
|
ret = 0;
|
2012-04-02 19:08:07 +08:00
|
|
|
}
|
|
|
|
if (opened)
|
|
|
|
close ();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-19 17:50:36 +08:00
|
|
|
int
|
|
|
|
fhandler_dev::rmdir ()
|
|
|
|
{
|
|
|
|
set_errno (ENOTEMPTY);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-01 01:38:00 +08:00
|
|
|
DIR *
|
|
|
|
fhandler_dev::opendir (int fd)
|
|
|
|
{
|
2013-10-30 17:44:47 +08:00
|
|
|
DIR *dir = fhandler_disk_file::opendir (fd);
|
2012-04-01 01:38:00 +08:00
|
|
|
if (dir)
|
2013-10-30 17:44:47 +08:00
|
|
|
dir_exists = true;
|
|
|
|
else if ((dir = (DIR *) malloc (sizeof (DIR))) == NULL)
|
2012-04-01 01:38:00 +08:00
|
|
|
set_errno (ENOMEM);
|
|
|
|
else if ((dir->__d_dirent =
|
|
|
|
(struct dirent *) malloc (sizeof (struct dirent))) == NULL)
|
|
|
|
{
|
|
|
|
set_errno (ENOMEM);
|
|
|
|
goto free_dir;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cygheap_fdnew cfd;
|
|
|
|
if (cfd < 0 && fd < 0)
|
|
|
|
goto free_dirent;
|
|
|
|
|
|
|
|
dir->__d_dirname = NULL;
|
|
|
|
dir->__d_dirent->__d_version = __DIRENT_VERSION;
|
|
|
|
dir->__d_cookie = __DIRENT_COOKIE;
|
|
|
|
dir->__handle = INVALID_HANDLE_VALUE;
|
|
|
|
dir->__d_position = 0;
|
|
|
|
dir->__flags = 0;
|
|
|
|
dir->__d_internal = 0;
|
|
|
|
|
|
|
|
if (fd >= 0)
|
|
|
|
dir->__d_fd = fd;
|
2013-10-30 17:44:47 +08:00
|
|
|
else if (!open (O_RDONLY, 0))
|
|
|
|
goto free_dirent;
|
2012-04-01 01:38:00 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
cfd = this;
|
|
|
|
dir->__d_fd = cfd;
|
|
|
|
}
|
|
|
|
set_close_on_exec (true);
|
|
|
|
dir->__fh = this;
|
2013-10-30 17:44:47 +08:00
|
|
|
dir_exists = false;
|
2016-06-23 22:56:41 +08:00
|
|
|
drive = part = 0;
|
2012-04-01 01:38:00 +08:00
|
|
|
}
|
|
|
|
|
2013-10-30 17:44:47 +08:00
|
|
|
devidx = dir_exists ? NULL : dev_storage_scan_start;
|
|
|
|
|
|
|
|
syscall_printf ("%p = opendir (%s)", dir, get_name ());
|
|
|
|
return dir;
|
2012-04-01 01:38:00 +08:00
|
|
|
|
|
|
|
free_dirent:
|
|
|
|
free (dir->__d_dirent);
|
|
|
|
free_dir:
|
|
|
|
free (dir);
|
2013-10-30 17:44:47 +08:00
|
|
|
return NULL;
|
2012-04-01 01:38:00 +08:00
|
|
|
}
|
|
|
|
|
2016-06-23 22:56:41 +08:00
|
|
|
static const WCHAR *hd_pattern = L"\\Device\\Harddisk%u\\Partition%u";
|
|
|
|
|
2012-03-30 02:02:54 +08:00
|
|
|
int
|
|
|
|
fhandler_dev::readdir (DIR *dir, dirent *de)
|
|
|
|
{
|
|
|
|
int ret;
|
2016-06-23 22:56:41 +08:00
|
|
|
const _device *curdev;
|
2012-03-30 17:26:45 +08:00
|
|
|
device dev;
|
2012-03-30 02:02:54 +08:00
|
|
|
|
2012-04-01 01:38:00 +08:00
|
|
|
if (!devidx)
|
2012-03-30 02:02:54 +08:00
|
|
|
{
|
|
|
|
while ((ret = fhandler_disk_file::readdir (dir, de)) == 0)
|
|
|
|
{
|
|
|
|
/* Avoid to print devices for which users have created files under
|
|
|
|
/dev already, for instance by using the old script from Igor
|
|
|
|
Peshansky. */
|
2016-06-23 22:56:41 +08:00
|
|
|
dev.name (de->d_name);
|
2012-04-01 18:38:07 +08:00
|
|
|
if (!bsearch (&dev, dev_storage_scan_start, dev_storage_size,
|
|
|
|
sizeof dev, device_cmp))
|
2012-03-30 02:02:54 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-04-01 18:38:07 +08:00
|
|
|
if (ret != ENMFILE)
|
2012-04-01 01:38:00 +08:00
|
|
|
goto out;
|
2012-04-01 18:38:07 +08:00
|
|
|
devidx = dev_storage_scan_start;
|
2012-03-30 02:02:54 +08:00
|
|
|
}
|
2012-04-01 01:38:00 +08:00
|
|
|
|
|
|
|
/* Now start processing our internal dev table. */
|
|
|
|
ret = ENMFILE;
|
2012-04-01 18:38:07 +08:00
|
|
|
while ((curdev = devidx++) < dev_storage_end)
|
2012-03-30 02:02:54 +08:00
|
|
|
{
|
2012-04-01 15:19:52 +08:00
|
|
|
/* If exists returns < 0 it means that the device can be used by a
|
|
|
|
program but its use is deprecated and, so, it is not returned
|
|
|
|
by readdir((). */
|
2016-06-23 22:56:41 +08:00
|
|
|
device *cdev = (device *) curdev;
|
|
|
|
if (cdev->exists () <= 0)
|
2012-04-01 01:38:00 +08:00
|
|
|
continue;
|
|
|
|
++dir->__d_position;
|
2016-06-23 22:56:41 +08:00
|
|
|
strcpy (de->d_name, cdev->name () + dev_prefix_len);
|
|
|
|
if (cdev->get_major () == DEV_TTY_MAJOR
|
|
|
|
&& (cdev->is_device (FH_CONIN)
|
|
|
|
|| cdev->is_device (FH_CONOUT)
|
|
|
|
|| cdev->is_device (FH_CONSOLE)))
|
2012-04-01 01:38:00 +08:00
|
|
|
{
|
2012-04-01 18:38:07 +08:00
|
|
|
/* Make sure conin, conout, and console have the same inode number
|
|
|
|
as the current consX. */
|
2012-04-03 03:25:58 +08:00
|
|
|
de->d_ino = myself->ctty;
|
2012-03-30 02:02:54 +08:00
|
|
|
}
|
2012-04-01 18:38:07 +08:00
|
|
|
else
|
2016-06-23 22:56:41 +08:00
|
|
|
de->d_ino = cdev->get_device ();
|
|
|
|
de->d_type = cdev->type ();
|
2012-04-01 01:38:00 +08:00
|
|
|
ret = 0;
|
|
|
|
break;
|
2012-03-30 02:02:54 +08:00
|
|
|
}
|
2016-06-23 22:56:41 +08:00
|
|
|
/* Last but not least, scan for existing disks/partitions. */
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
UNICODE_STRING upath;
|
|
|
|
WCHAR buf[(sizeof *hd_pattern + 32) / sizeof (wchar_t)];
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
FILE_BASIC_INFORMATION fbi;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
InitializeObjectAttributes (&attr, &upath, 0, NULL, NULL);
|
|
|
|
while (drive < 128)
|
|
|
|
{
|
|
|
|
while (part < 64)
|
|
|
|
{
|
|
|
|
USHORT len = __small_swprintf (buf, hd_pattern, drive, part);
|
|
|
|
RtlInitCountedUnicodeString (&upath, buf, len * sizeof (WCHAR));
|
|
|
|
status = NtQueryAttributesFile (&attr, &fbi);
|
|
|
|
debug_printf ("%S %y", &upath, status);
|
|
|
|
if (status != STATUS_OBJECT_NAME_NOT_FOUND
|
|
|
|
&& status != STATUS_OBJECT_PATH_NOT_FOUND)
|
|
|
|
{
|
|
|
|
device dev (drive, part);
|
|
|
|
strcpy (de->d_name, dev.name () + 5);
|
|
|
|
de->d_ino = dev.get_device ();
|
|
|
|
de->d_type = DT_BLK;
|
|
|
|
++part;
|
|
|
|
ret = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (part == 0)
|
|
|
|
break;
|
|
|
|
++part;
|
|
|
|
}
|
|
|
|
part = 0;
|
|
|
|
++drive;
|
|
|
|
}
|
|
|
|
}
|
2012-04-01 01:38:00 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
debug_printf ("returning %d", ret);
|
2012-03-30 02:02:54 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_dev::rewinddir (DIR *dir)
|
|
|
|
{
|
2012-04-01 01:38:00 +08:00
|
|
|
devidx = dir_exists ? NULL : dev_storage_scan_start;
|
2013-10-30 17:44:47 +08:00
|
|
|
dir->__d_position = 0;
|
|
|
|
if (dir_exists)
|
|
|
|
fhandler_disk_file::rewinddir (dir);
|
2012-03-30 02:02:54 +08:00
|
|
|
}
|