2012-03-29 20:02:54 +02:00
|
|
|
/* fhandler_dev.cc, Implement /dev.
|
|
|
|
|
|
|
|
Copyright 2012 Red Hat, Inc.
|
|
|
|
|
|
|
|
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 13:08:07 +02:00
|
|
|
#include <sys/statvfs.h>
|
2012-03-29 20:02:54 +02:00
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
2012-03-31 19:38:00 +02:00
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2012-03-29 20:02:54 +02:00
|
|
|
#include "devices.h"
|
|
|
|
|
|
|
|
#define _COMPILING_NEWLIB
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#define dev_prefix_len (sizeof ("/dev"))
|
2012-03-31 19:38:00 +02:00
|
|
|
#define dev_storage_scan_start (dev_storage + 1)
|
|
|
|
#define dev_storage_size (dev_storage_end - dev_storage_scan_start)
|
2012-03-29 20:02:54 +02:00
|
|
|
|
|
|
|
static int
|
|
|
|
device_cmp (const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return strcmp (((const device *) a)->name,
|
|
|
|
((const device *) b)->name + dev_prefix_len);
|
|
|
|
}
|
|
|
|
|
2012-03-31 19:38:00 +02:00
|
|
|
fhandler_dev::fhandler_dev () :
|
|
|
|
fhandler_disk_file (), devidx (NULL), dir_exists (true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-02 13:08:07 +02: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;
|
|
|
|
}
|
|
|
|
int ret = fhandler_disk_file::open (flags, mode);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
flags |= O_DIROPEN;
|
|
|
|
set_flags (flags);
|
|
|
|
nohandle (true);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_dev::close ()
|
|
|
|
{
|
|
|
|
if (nohandle ())
|
|
|
|
return 0;
|
|
|
|
return fhandler_disk_file::close ();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_dev::fstat (struct __stat64 *st)
|
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int __stdcall
|
|
|
|
fhandler_dev::fstatvfs (struct statvfs *sfs)
|
|
|
|
{
|
|
|
|
int ret = -1, opened = 0;
|
|
|
|
HANDLE fh = get_handle ();
|
|
|
|
|
|
|
|
if (!fh && !nohandle ())
|
|
|
|
{
|
|
|
|
if (!open (O_RDONLY, 0))
|
|
|
|
return -1;
|
|
|
|
opened = 1;
|
|
|
|
}
|
|
|
|
if (!nohandle ())
|
|
|
|
{
|
|
|
|
ret = fhandler_disk_file::fstatvfs (sfs);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* Virtual file system. Just return an empty buffer with a few values
|
|
|
|
set to something useful. Just as on Linux. */
|
|
|
|
memset (sfs, 0, sizeof (*sfs));
|
|
|
|
sfs->f_bsize = sfs->f_frsize = 4096;
|
|
|
|
sfs->f_namemax = NAME_MAX;
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (opened)
|
|
|
|
close ();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-31 19:38:00 +02:00
|
|
|
DIR *
|
|
|
|
fhandler_dev::opendir (int fd)
|
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
DIR *res = NULL;
|
|
|
|
|
|
|
|
dir = fhandler_disk_file::opendir (fd);
|
|
|
|
if (dir)
|
|
|
|
return dir;
|
|
|
|
if ((dir = (DIR *) malloc (sizeof (DIR))) == NULL)
|
|
|
|
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;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cfd = this;
|
|
|
|
dir->__d_fd = cfd;
|
|
|
|
cfd->nohandle (true);
|
|
|
|
}
|
|
|
|
set_close_on_exec (true);
|
|
|
|
dir->__fh = this;
|
|
|
|
devidx = dev_storage_scan_start;
|
|
|
|
res = dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
syscall_printf ("%p = opendir (%s)", res, get_name ());
|
|
|
|
return res;
|
|
|
|
|
|
|
|
free_dirent:
|
|
|
|
free (dir->__d_dirent);
|
|
|
|
free_dir:
|
|
|
|
free (dir);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-03-29 20:02:54 +02:00
|
|
|
int
|
|
|
|
fhandler_dev::readdir (DIR *dir, dirent *de)
|
|
|
|
{
|
|
|
|
int ret;
|
2012-04-01 12:38:07 +02:00
|
|
|
const device *curdev;
|
2012-03-30 11:26:45 +02:00
|
|
|
device dev;
|
2012-03-29 20:02:54 +02:00
|
|
|
|
2012-03-31 19:38:00 +02:00
|
|
|
if (!devidx)
|
2012-03-29 20:02:54 +02: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. */
|
|
|
|
dev.name = de->d_name;
|
2012-04-01 12:38:07 +02:00
|
|
|
if (!bsearch (&dev, dev_storage_scan_start, dev_storage_size,
|
|
|
|
sizeof dev, device_cmp))
|
2012-03-29 20:02:54 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-04-01 12:38:07 +02:00
|
|
|
if (ret != ENMFILE)
|
2012-03-31 19:38:00 +02:00
|
|
|
goto out;
|
2012-04-01 12:38:07 +02:00
|
|
|
devidx = dev_storage_scan_start;
|
2012-03-29 20:02:54 +02:00
|
|
|
}
|
2012-03-31 19:38:00 +02:00
|
|
|
|
|
|
|
/* Now start processing our internal dev table. */
|
|
|
|
ret = ENMFILE;
|
2012-04-01 12:38:07 +02:00
|
|
|
while ((curdev = devidx++) < dev_storage_end)
|
2012-03-29 20:02:54 +02:00
|
|
|
{
|
2012-04-01 09:19:52 +02: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((). */
|
2012-04-01 12:38:07 +02:00
|
|
|
if (curdev->exists () <= 0)
|
2012-03-31 19:38:00 +02:00
|
|
|
continue;
|
|
|
|
++dir->__d_position;
|
2012-04-01 12:38:07 +02:00
|
|
|
strcpy (de->d_name, curdev->name + dev_prefix_len);
|
|
|
|
if (curdev->get_major () == DEV_TTY_MAJOR
|
|
|
|
&& (curdev->is_device (FH_CONIN)
|
|
|
|
|| curdev->is_device (FH_CONOUT)
|
|
|
|
|| curdev->is_device (FH_CONSOLE)))
|
2012-03-31 19:38:00 +02:00
|
|
|
{
|
2012-04-01 12:38:07 +02:00
|
|
|
/* Make sure conin, conout, and console have the same inode number
|
|
|
|
as the current consX. */
|
2012-04-02 21:25:58 +02:00
|
|
|
de->d_ino = myself->ctty;
|
2012-03-29 20:02:54 +02:00
|
|
|
}
|
2012-04-01 12:38:07 +02:00
|
|
|
else
|
2012-04-02 21:25:58 +02:00
|
|
|
de->d_ino = curdev->get_device ();
|
2012-04-01 12:38:07 +02:00
|
|
|
de->d_type = curdev->type ();
|
2012-03-31 19:38:00 +02:00
|
|
|
ret = 0;
|
|
|
|
break;
|
2012-03-29 20:02:54 +02:00
|
|
|
}
|
2012-03-31 19:38:00 +02:00
|
|
|
|
|
|
|
out:
|
|
|
|
debug_printf ("returning %d", ret);
|
2012-03-29 20:02:54 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_dev::rewinddir (DIR *dir)
|
|
|
|
{
|
2012-03-31 19:38:00 +02:00
|
|
|
devidx = dir_exists ? NULL : dev_storage_scan_start;
|
2012-03-29 20:02:54 +02:00
|
|
|
fhandler_disk_file::rewinddir (dir);
|
|
|
|
}
|