+2001-10-15 Christopher Faylor <cgf@redhat.com>
+
+ * fhandler.cc (fhandler_base::fork_fixup): Don't protect handle.
+
+ * dlfcn.cc: Fix to confirm to coding standards.
+
+ Reorganize includes throughout to accomodate new cygheap.h usage.
+ * cygheap.h (cygheap_fdmanip): New class: simplifies locking and
+ retrieval of fds from cygheap->fdtab.
+ (cygheap_fdget): Ditto.
+ (cygheap_fdnew): Ditto.
+ * fcntl.cc (_fcntl): Use new method to lock fdtab and retrieve info.
+ * ioctl.cc (ioctl): Ditto.
+ * mmap.cc (mmap): Ditto.
+ * net.cc: Ditto, throughout.
+ * passwd.cc (getpass): Ditto.
+ * path.cc (fchdir): Ditto.
+ * pipe.cc (make_pipe): Ditto.
+ * sec_acl.cc (facl): Ditto.
+ * syscalls.cc: Ditto, throughout.
+ * termios.cc: Ditto, throughout.
+
2001-10-15 Corinna Vinschen <corinna@vinschen.de>
* uname.cc (uname): Use `wProcessorLevel' unless OS sets it wrong.
* dtable.cc (dtable::build_fhandler_from_name): Use PC_FULL to
determine path name.
+ * path.cc (fchdir): Remove rel -> abs path conversion.
Sun Oct 14 08:10:12 2001 Gary R. Van Sickle
#include "fhandler.h"
#include "path.h"
#include "dtable.h"
+#include "cygerrno.h"
#include "cygheap.h"
#include "child_info.h"
#include "heap.h"
-#include "cygerrno.h"
#include "sync.h"
#include "shared_info.h"
extern init_cygheap *cygheap;
extern void *cygheap_max;
+class cygheap_fdmanip
+{
+ protected:
+ int fd;
+ fhandler_base **fh;
+ bool locked;
+ public:
+ cygheap_fdmanip () {}
+ virtual ~cygheap_fdmanip ()
+ {
+ if (locked)
+ ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygheap_fdmanip");
+ }
+ void release ()
+ {
+ cygheap->fdtab.release (fd);
+ }
+ operator int &() {return fd;}
+ operator fhandler_base* &() {return *fh;}
+ void operator = (fhandler_base *fh) {*this->fh = fh;}
+ fhandler_base *operator -> () const {return *fh;}
+};
+
+class cygheap_fdnew : public cygheap_fdmanip
+{
+ public:
+ cygheap_fdnew (int seed_fd = -1, bool lockit = true)
+ {
+ if (lockit)
+ SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygheap_fdnew");
+ if (seed_fd < 0)
+ fd = cygheap->fdtab.find_unused_handle ();
+ else
+ fd = cygheap->fdtab.find_unused_handle (seed_fd + 1);
+ if (fd >= 0)
+ {
+ locked = lockit;
+ fh = cygheap->fdtab + fd;
+ }
+ else
+ {
+ set_errno (EMFILE);
+ if (lockit)
+ ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygheap_fdnew");
+ locked = false;
+ }
+ }
+};
+
+class cygheap_fdget : public cygheap_fdmanip
+{
+ public:
+ cygheap_fdget (int fd, bool lockit = false)
+ {
+ if (lockit)
+ SetResourceLock (LOCK_FD_LIST, READ_LOCK, "cygheap_fdget");
+ if (fd >= 0 && fd < (int) cygheap->fdtab.size
+ && *(fh = cygheap->fdtab + fd) != NULL)
+ {
+ this->fd = fd;
+ locked = lockit;
+ }
+ else
+ {
+ this->fd = -1;
+ set_errno (EBADF);
+ if (lockit)
+ ReleaseResourceLock (LOCK_FD_LIST, READ_LOCK, "cygheap_fdget");
+ locked = false;
+ }
+ }
+};
+
class child_info;
void *__stdcall cygheap_setup_for_child (child_info *ci, bool dup_later) __attribute__ ((regparm(2)));
void __stdcall cygheap_setup_for_child_cleanup (void *, child_info *, bool) __attribute__ ((regparm(3)));
#include <limits.h>
#include <wingdi.h>
#include <winuser.h>
+#include <errno.h>
#include "sync.h"
#include "sigproc.h"
#include "pinfo.h"
#include "dll_init.h"
#include "cygerrno.h"
-#define _dl_error _reent_winsup()->_dl_error
-#define _dl_buffer _reent_winsup()->_dl_buffer
+#define _dl_error _reent_winsup ()->_dl_error
+#define _dl_buffer _reent_winsup ()->_dl_buffer
static void __stdcall
set_dl_error (const char *str)
void *
dlopen (const char *name, int)
{
- SetResourceLock(LOCK_DLL_LIST,READ_LOCK|WRITE_LOCK," dlopen");
+ SetResourceLock (LOCK_DLL_LIST, READ_LOCK | WRITE_LOCK, "dlopen");
void *ret;
set_dl_error ("dlopen");
debug_printf ("ret %p", ret);
- ReleaseResourceLock(LOCK_DLL_LIST,READ_LOCK|WRITE_LOCK," dlopen");
+ ReleaseResourceLock (LOCK_DLL_LIST, READ_LOCK | WRITE_LOCK, "dlopen");
return ret;
}
int
dlclose (void *handle)
{
- SetResourceLock(LOCK_DLL_LIST,READ_LOCK|WRITE_LOCK," dlclose");
+ SetResourceLock (LOCK_DLL_LIST, READ_LOCK | WRITE_LOCK, "dlclose");
int ret = -1;
void *temphandle = (void *) GetModuleHandle (NULL);
set_dl_error ("dlclose");
CloseHandle ((HMODULE) temphandle);
- ReleaseResourceLock(LOCK_DLL_LIST,READ_LOCK|WRITE_LOCK," dlclose");
+ ReleaseResourceLock (LOCK_DLL_LIST, READ_LOCK | WRITE_LOCK, "dlclose");
return ret;
}
#include "winsup.h"
#include <stdlib.h>
+#include <errno.h>
#include "exceptions.h"
#include "cygerrno.h"
#include "perprocess.h"
#include "fhandler.h"
#include "path.h"
#include "dtable.h"
-#include "cygheap.h"
#include "cygerrno.h"
+#include "cygheap.h"
#include "thread.h"
extern "C"
va_list args;
int res;
- if (cygheap->fdtab.not_open (fd))
+ cygheap_fdget cfd (fd, true);
+ if (cfd < 0)
{
- set_errno (EBADF);
res = -1;
goto done;
}
- SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK, "_fcntl");
va_start (args, cmd);
arg = va_arg (args, void *);
- if (cmd == F_DUPFD)
- res = dup2 (fd, cygheap->fdtab.find_unused_handle ((int) arg));
+ if (cmd != F_DUPFD)
+ res = cfd->fcntl(cmd, arg);
else
- res = cygheap->fdtab[fd]->fcntl(cmd, arg);
+ {
+ cygheap_fdnew newfd;
+ if (newfd >= 0)
+ res = dup2 (fd, newfd);
+ else
+ res = -1;
+ }
va_end (args);
- ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"_fcntl");
done:
syscall_printf ("%d = fcntl (%d, %d, %p)", res, fd, cmd, arg);
else
{
debug_printf ("%s success - oldh %p, h %p", get_name (), oh, h);
- ProtectHandle1 (h, name);
+ // someday, maybe ProtectHandle2 (h, name);
setclexec_pid (h, !get_close_on_exec ());
}
#endif
#include "fhandler.h"
#include "path.h"
#include "dtable.h"
-#include "cygheap.h"
#include "cygerrno.h"
+#include "cygheap.h"
#include "pwdgrp.h"
/* Read /etc/group only once for better performance. This is done
extern "C" int
ioctl (int fd, int cmd, void *buf)
{
- if (cygheap->fdtab.not_open (fd))
- {
- set_errno (EBADF);
- return -1;
- }
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ return -1;
debug_printf ("fd %d, cmd %x\n", fd, cmd);
- fhandler_base *fh = cygheap->fdtab[fd];
- if (fh->is_tty () && fh->get_device () != FH_PTYM)
+ if (cfd->is_tty () && cfd->get_device () != FH_PTYM)
switch (cmd)
{
case TCGETA:
return tcsetattr (fd, TCSAFLUSH, (struct termios *) buf);
}
- return fh->ioctl (cmd, buf);
+ return cfd->ioctl (cmd, buf);
}
#include "fhandler.h"
#include "path.h"
#include "dtable.h"
+#include <errno.h>
+#include "cygerrno.h"
#include "cygheap.h"
#include "heap.h"
#include "sync.h"
if (fd != -1)
{
/* Ensure that fd is open */
- if (cygheap->fdtab.not_open (fd))
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
{
- set_errno (EBADF);
syscall_printf ("-1 = mmap(): EBADF");
ReleaseResourceLock(LOCK_MMAP_LIST, READ_LOCK | WRITE_LOCK, "mmap");
return MAP_FAILED;
}
- fh = cygheap->fdtab[fd];
- if (fh->get_device () == FH_DISK)
+ if (cfd->get_device () == FH_DISK)
{
DWORD fsiz = GetFileSize (fh->get_handle (), NULL);
fsiz -= gran_off;
if (gran_len > fsiz)
gran_len = fsiz;
}
- else if (fh->get_device () == FH_ZERO)
+ else if (cfd->get_device () == FH_ZERO)
/* mmap /dev/zero is like MAP_ANONYMOUS. */
fd = -1;
}
}
fhandler_socket *
-fdsock (int fd, const char *name, SOCKET soc)
+fdsock (int& fd, const char *name, SOCKET soc)
{
if (!winsock2_active)
soc = set_socket_inheritance (soc);
cygwin_socket (int af, int type, int protocol)
{
int res = -1;
- SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "socket");
-
SOCKET soc = 0;
- int fd = cygheap->fdtab.find_unused_handle ();
+ cygheap_fdnew fd;
- if (fd < 0)
- set_errno (EMFILE);
- else
+ if (fd >= 0)
{
debug_printf ("socket (%d, %d, %d)", af, type, protocol);
done:
syscall_printf ("%d = socket (%d, %d, %d)", res, af, type, protocol);
- ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "socket");
return res;
}
fhandler_socket *
get (int fd)
{
- if (cygheap->fdtab.not_open (fd))
- {
- set_errno (EINVAL);
- return 0;
- }
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ return 0;
- return cygheap->fdtab[fd]->is_socket ();
+ return cfd->is_socket ();
}
/* exported as setsockopt: standards? */
}
}
- SetResourceLock (LOCK_FD_LIST, WRITE_LOCK|READ_LOCK, "accept");
- int res_fd = cygheap->fdtab.find_unused_handle ();
- if (res_fd == -1)
- /* FIXME: what is correct errno? */
- set_errno (EMFILE);
+ cygheap_fdnew res_fd;
+ if (res_fd < 0)
+ /* FIXME: what is correct errno? */;
else if ((SOCKET) res == (SOCKET) INVALID_SOCKET)
set_winsock_errno ();
else
res_fh->set_addr_family (sock->get_addr_family ());
res = res_fd;
}
- ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK|READ_LOCK, "accept");
}
done:
syscall_printf ("%d = accept (%d, %x, %x)", res, fd, peer, len);
SOCKET fd2s;
sigframe thisframe (mainthread);
- int res_fd = cygheap->fdtab.find_unused_handle ();
- if (res_fd == -1)
+ cygheap_fdnew res_fd;
+ if (res_fd < 0)
goto done;
if (fd2p)
{
- SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygwin_rcmd");
- *fd2p = cygheap->fdtab.find_unused_handle (res_fd + 1);
- if (*fd2p == -1)
+ cygheap_fdnew newfd (res_fd, false);
+ if (*fd2p < 0)
goto done;
+ *fd2p = newfd;
}
- res = rcmd (ahost, inport, locuser, remuser, cmd, fd2p? &fd2s: NULL);
+ res = rcmd (ahost, inport, locuser, remuser, cmd, fd2p ? &fd2s : NULL);
if (res == (int) INVALID_SOCKET)
goto done;
else
fdsock (res_fd, "/dev/tcp", res);
res = res_fd;
}
+
if (fd2p)
fdsock (*fd2p, "/dev/tcp", fd2s);
+
done:
- if (fd2p)
- ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK|READ_LOCK, "cygwin_rcmd");
syscall_printf ("%d = rcmd (...)", res);
return res;
}
extern "C" int
cygwin_rresvport (int *port)
{
- int res = -1;
+ int res;
sigframe thisframe (mainthread);
- SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygwin_rresvport");
- int res_fd = cygheap->fdtab.find_unused_handle ();
- if (res_fd == -1)
- goto done;
- res = rresvport (port);
-
- if (res == (int) INVALID_SOCKET)
- goto done;
+ cygheap_fdnew res_fd;
+ if (res_fd < 0)
+ res = -1;
else
{
- fdsock (res_fd, "/dev/tcp", res);
- res = res_fd;
+ res = rresvport (port);
+
+ if (res != (int) INVALID_SOCKET)
+ {
+ fdsock (res_fd, "/dev/tcp", res);
+ res = res_fd;
+ }
}
-done:
- ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygwin_rresvport");
+
syscall_printf ("%d = rresvport (%d)", res, port ? *port : 0);
return res;
}
SOCKET fd2s;
sigframe thisframe (mainthread);
- ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygwin_rexec");
- int res_fd = cygheap->fdtab.find_unused_handle ();
- if (res_fd == -1)
+ cygheap_fdnew res_fd;
+ if (res_fd < 0)
goto done;
if (fd2p)
{
- *fd2p = cygheap->fdtab.find_unused_handle (res_fd + 1);
- if (*fd2p == -1)
+ cygheap_fdnew newfd (res_fd);
+ if (newfd < 0)
goto done;
+ *fd2p = newfd;
}
res = rexec (ahost, inport, locuser, password, cmd, fd2p ? &fd2s : NULL);
if (res == (int) INVALID_SOCKET)
fdsock (*fd2p, "/dev/tcp", fd2s);
done:
- ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygwin_rexec");
syscall_printf ("%d = rexec (...)", res);
return res;
}
struct sockaddr_in sock_in;
int len = sizeof (sock_in);
- SetResourceLock (LOCK_FD_LIST, WRITE_LOCK|READ_LOCK, "socketpair");
-
- sb[0] = cygheap->fdtab.find_unused_handle ();
- if (sb[0] == -1)
- {
- set_errno (EMFILE);
- goto done;
- }
- sb[1] = cygheap->fdtab.find_unused_handle (sb[0] + 1);
- if (sb[1] == -1)
+ cygheap_fdnew sb0;
+ if (sb0 < 0)
+ goto done;
+ else
{
- set_errno (EMFILE);
- goto done;
- }
+ sb[0] = sb0;
+ cygheap_fdnew sb1 (sb0, false);
+ if (sb1 < 0)
+ goto done;
+ sb[1] = sb1;
+ }
/* create a listening socket */
newsock = socket (AF_INET, type, 0);
if (newsock == INVALID_SOCKET)
done:
syscall_printf ("%d = socketpair (...)", res);
- ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK|READ_LOCK, "socketpair");
return res;
}
if (passwd_state <= initializing)
read_etc_passwd ();
- if (cygheap->fdtab.not_open (0))
- {
- set_errno (EBADF);
- pass[0] = '\0';
- }
+ cygheap_fdget fhstdin (0);
+
+ if (fhstdin < 0)
+ pass[0] = '\0';
else
{
- fhandler_base *fhstdin = cygheap->fdtab[0];
fhstdin->tcgetattr (&ti);
newti = ti;
newti.c_lflag &= ~ECHO;
int
fchdir (int fd)
{
+ int res;
sigframe thisframe (mainthread);
- if (cygheap->fdtab.not_open (fd))
- {
- syscall_printf ("-1 = fchdir (%d)", fd);
- set_errno (EBADF);
- return -1;
- }
- SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fchdir");
- int ret = chdir (cygheap->fdtab[fd]->get_name ());
- ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fchdir");
- syscall_printf ("%d = fchdir (%d)", ret, fd);
- return ret;
+ cygheap_fdget cfd (fd);
+ if (cfd >= 0)
+ res = chdir (cfd->get_name ());
+ else
+ res = -1;
+
+ syscall_printf ("%d = fchdir (%d)", res, fd);
+ return res;
}
/******************** Exported Path Routines *********************/
int
make_pipe (int fildes[2], unsigned int psize, int mode)
{
- SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "make_pipe");
-
HANDLE r, w;
- int fdr = -1, fdw = -1;
SECURITY_ATTRIBUTES *sa = (mode & O_NOINHERIT) ? &sec_none_nih : &sec_none;
int res = -1;
- if ((fdr = cygheap->fdtab.find_unused_handle ()) < 0)
- set_errno (ENMFILE);
- else if ((fdw = cygheap->fdtab.find_unused_handle (fdr + 1)) < 0)
- set_errno (ENMFILE);
- else if (!CreatePipe (&r, &w, sa, psize))
- __seterrno ();
+ cygheap_fdnew fdr;
+ if (fdr < 0)
+ /* saw an error */;
else
{
- fhandler_pipe *fhr = (fhandler_pipe *) cygheap->fdtab.build_fhandler (fdr, FH_PIPER, "/dev/piper");
- fhandler_pipe *fhw = (fhandler_pipe *) cygheap->fdtab.build_fhandler (fdw, FH_PIPEW, "/dev/pipew");
-
- int binmode = mode & O_TEXT ? 0 : 1;
- fhr->init (r, GENERIC_READ, binmode);
- fhw->init (w, GENERIC_WRITE, binmode);
- if (mode & O_NOINHERIT)
- {
- fhr->set_close_on_exec_flag (1);
- fhw->set_close_on_exec_flag (1);
- }
-
- fildes[0] = fdr;
- fildes[1] = fdw;
-
- res = 0;
- fhr->create_guard (sa);
- if (wincap.has_unreliable_pipes ())
+ cygheap_fdnew fdw (fdr, false);
+ if (fdw < 0)
+ set_errno (ENMFILE);
+ else if (!CreatePipe (&r, &w, sa, psize))
+ __seterrno ();
+ else
{
- char buf[80];
- int count = pipecount++; /* FIXME: Should this be InterlockedIncrement? */
- __small_sprintf (buf, pipeid_fmt, myself->pid, count);
- fhw->writepipe_exists = CreateEvent (sa, TRUE, FALSE, buf);
- fhr->orig_pid = myself->pid;
- fhr->id = count;
+ fhandler_pipe *fhr = (fhandler_pipe *) cygheap->fdtab.build_fhandler (fdr, FH_PIPER, "/dev/piper");
+ fhandler_pipe *fhw = (fhandler_pipe *) cygheap->fdtab.build_fhandler (fdw, FH_PIPEW, "/dev/pipew");
+
+ int binmode = mode & O_TEXT ? 0 : 1;
+ fhr->init (r, GENERIC_READ, binmode);
+ fhw->init (w, GENERIC_WRITE, binmode);
+ if (mode & O_NOINHERIT)
+ {
+ fhr->set_close_on_exec_flag (1);
+ fhw->set_close_on_exec_flag (1);
+ }
+
+ fildes[0] = fdr;
+ fildes[1] = fdw;
+
+ res = 0;
+ fhr->create_guard (sa);
+ if (wincap.has_unreliable_pipes ())
+ {
+ char buf[80];
+ int count = pipecount++; /* FIXME: Should this be InterlockedIncrement? */
+ __small_sprintf (buf, pipeid_fmt, myself->pid, count);
+ fhw->writepipe_exists = CreateEvent (sa, TRUE, FALSE, buf);
+ fhr->orig_pid = myself->pid;
+ fhr->id = count;
+ }
}
}
- syscall_printf ("%d = make_pipe ([%d, %d], %d, %p)", res, fdr, fdw, psize, mode);
- ReleaseResourceLock(LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "make_pipe");
+ syscall_printf ("%d = make_pipe ([%d, %d], %d, %p)", res, fildes[0],
+ fildes[1], psize, mode);
return res;
}
#include "fhandler.h"
#include "path.h"
#include "dtable.h"
-#include "cygheap.h"
#include "cygerrno.h"
+#include "cygheap.h"
#include "sigproc.h"
extern "C"
int
facl (int fd, int cmd, int nentries, aclent_t *aclbufp)
{
- if (cygheap->fdtab.not_open (fd))
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
{
syscall_printf ("-1 = facl (%d)", fd);
- set_errno (EBADF);
return -1;
}
- const char *path = cygheap->fdtab[fd]->get_name ();
+ const char *path = cfd->get_name ();
if (path == NULL)
{
syscall_printf ("-1 = facl (%d) (no name)", fd);
#include <stdlib.h>
#include <grp.h>
#include <pwd.h>
+#include <errno.h>
#include "sync.h"
#include "sigproc.h"
#include "pinfo.h"
#include "fhandler.h"
#include "path.h"
#include "dtable.h"
+#include "cygerrno.h"
#include "cygheap.h"
#include "heap.h"
#include "shared_info.h"
fhandler_base *fh;
for (int i = 0; i < (int) cygheap->fdtab.size; i++)
if ((fh = cygheap->fdtab[i]) != NULL &&
- (fh->get_device() == FH_TTYS || fh->get_device() == FH_PTYM))
+ (fh->get_device () == FH_TTYS || fh->get_device () == FH_PTYM))
{
res = TRUE;
break;
dup (int fd)
{
int res;
- SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "dup");
+ cygheap_fdnew newfd;
- res = dup2 (fd, cygheap->fdtab.find_unused_handle ());
-
- ReleaseResourceLock(LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "dup");
+ if (newfd < 0)
+ res = -1;
+ else
+ res = dup2 (fd, newfd);
return res;
}
_read (int fd, void *ptr, size_t len)
{
int res;
- fhandler_base *fh;
extern int sigcatchers;
int e = get_errno ();
{
sigframe thisframe (mainthread);
- if (cygheap->fdtab.not_open (fd))
- {
- set_errno (EBADF);
- return -1;
- }
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ return -1;
- // set_sig_errno (0);
- fh = cygheap->fdtab[fd];
- DWORD wait = fh->is_nonblocking () ? 0 : INFINITE;
+ DWORD wait = cfd->is_nonblocking () ? 0 : INFINITE;
/* Could block, so let user know we at least got here. */
syscall_printf ("read (%d, %p, %d) %sblocking, sigcatchers %d", fd, ptr, len, wait ? "" : "non", sigcatchers);
- if (wait && (/*!sigcatchers || */!fh->is_slow () || fh->get_r_no_interrupt ()))
+ if (wait && (/*!sigcatchers || */!cfd->is_slow () || cfd->get_r_no_interrupt ()))
debug_printf ("non-interruptible read\n");
- else if (!fh->ready_for_read (fd, wait, 0))
+ else if (!cfd->ready_for_read (fd, wait, 0))
{
if (!wait)
set_sig_errno (EAGAIN); /* Don't really need 'set_sig_errno' here, but... */
/* Check to see if this is a background read from a "tty",
sending a SIGTTIN, if appropriate */
- res = fh->bg_check (SIGTTIN);
+ res = cfd->bg_check (SIGTTIN);
if (res > bg_eof)
{
myself->process_state |= PID_TTYIN;
- res = fh->read (ptr, len);
+ res = cfd->read (ptr, len);
myself->process_state &= ~PID_TTYIN;
}
set_errno (e);
}
- syscall_printf ("%d = read (%d<%s>, %p, %d), bin %d, errno %d", res, fd, fh->get_name (),
- ptr, len, fh->get_r_binary (), get_errno ());
+ syscall_printf ("%d = read (%d, %p, %d), errno %d", res, fd, ptr, len,
+ get_errno ());
MALLOC_CHECK;
return res;
}
int res = -1;
sigframe thisframe (mainthread);
- if (cygheap->fdtab.not_open (fd))
- {
- set_errno (EBADF);
- goto done;
- }
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ goto done;
/* Could block, so let user know we at least got here. */
if (fd == 1 || fd == 2)
else
syscall_printf ("write (%d, %p, %d)", fd, ptr, len);
- fhandler_base *fh;
- fh = cygheap->fdtab[fd];
-
- res = fh->bg_check (SIGTTOU);
+ res = cfd->bg_check (SIGTTOU);
if (res > bg_eof)
{
myself->process_state |= PID_TTYOU;
- res = fh->write (ptr, len);
+ res = cfd->write (ptr, len);
myself->process_state &= ~PID_TTYOU;
}
else
syscall_printf ("%d = write (%d, %p, %d)", res, fd, ptr, len);
- return (ssize_t)res;
+ return (ssize_t) res;
}
/*
extern "C" int
_open (const char *unix_path, int flags, ...)
{
- int fd;
int res = -1;
va_list ap;
mode_t mode = 0;
- fhandler_base *fh;
sigframe thisframe (mainthread);
syscall_printf ("open (%s, %p)", unix_path, flags);
if (!check_null_empty_str_errno (unix_path))
{
- SetResourceLock (LOCK_FD_LIST, WRITE_LOCK|READ_LOCK, " open ");
-
/* check for optional mode argument */
va_start (ap, flags);
mode = va_arg (ap, mode_t);
va_end (ap);
- fd = cygheap->fdtab.find_unused_handle ();
+ fhandler_base *fh;
+ cygheap_fdnew fd;
if (fd < 0)
set_errno (ENMFILE);
res = -1; // errno already set
else if (!fh->open (&pc, flags, (mode & 07777) & ~cygheap->umask))
{
- cygheap->fdtab.release (fd);
+ fd.release ();
res = -1;
}
else if ((res = fd) <= 2)
set_std_handle (res);
}
- ReleaseResourceLock (LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," open");
}
syscall_printf ("%d = open (%s, %p)", res, unix_path, flags);
set_errno (EINVAL);
res = -1;
}
- else if (cygheap->fdtab.not_open (fd))
- {
- set_errno (EBADF);
- res = -1;
- }
else
{
- res = cygheap->fdtab[fd]->lseek (pos, dir);
+ cygheap_fdget cfd (fd);
+ if (cfd >= 0)
+ res = cfd->lseek (pos, dir);
+ else
+ res = -1;
}
syscall_printf ("%d = lseek (%d, %d, %d)", res, fd, pos, dir);
syscall_printf ("close (%d)", fd);
MALLOC_CHECK;
- if (cygheap->fdtab.not_open (fd))
- {
- debug_printf ("handle %d not open", fd);
- set_errno (EBADF);
- res = -1;
- }
+ cygheap_fdget cfd (fd, true);
+ if (cfd < 0)
+ res = -1;
else
{
- SetResourceLock (LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," close");
- res = cygheap->fdtab[fd]->close ();
- cygheap->fdtab.release (fd);
- ReleaseResourceLock (LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," close");
+ cfd->close ();
+ cfd.release ();
+ res = 0;
}
syscall_printf ("%d = close (%d)", res, fd);
int res;
sigframe thisframe (mainthread);
- if (cygheap->fdtab.not_open (fd))
- {
- syscall_printf ("0 = isatty (%d)", fd);
- return 0;
- }
-
- res = cygheap->fdtab[fd]->is_tty ();
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ res = 0;
+ else
+ res = cfd->is_tty ();
syscall_printf ("%d = isatty (%d)", res, fd);
return res;
}
fchown (int fd, uid_t uid, gid_t gid)
{
sigframe thisframe (mainthread);
- if (cygheap->fdtab.not_open (fd))
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
{
syscall_printf ("-1 = fchown (%d,...)", fd);
- set_errno (EBADF);
return -1;
}
- const char *path = cygheap->fdtab[fd]->get_name ();
+ const char *path = cfd->get_name ();
if (path == NULL)
{
fchmod (int fd, mode_t mode)
{
sigframe thisframe (mainthread);
- if (cygheap->fdtab.not_open (fd))
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
{
syscall_printf ("-1 = fchmod (%d, 0%o)", fd, mode);
- set_errno (EBADF);
return -1;
}
- const char *path = cygheap->fdtab[fd]->get_name ();
+ const char *path = cfd->get_name ();
if (path == NULL)
{
extern "C" int
_fstat (int fd, struct stat *buf)
{
- int r;
+ int res;
sigframe thisframe (mainthread);
- if (cygheap->fdtab.not_open (fd))
- {
- syscall_printf ("-1 = fstat (%d, %p)", fd, buf);
- set_errno (EBADF);
- r = -1;
- }
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ res = -1;
else
{
memset (buf, 0, sizeof (struct stat));
- r = cygheap->fdtab[fd]->fstat (buf, NULL);
- syscall_printf ("%d = fstat (%d, %x)", r, fd, buf);
+ res = cfd->fstat (buf, NULL);
}
- return r;
+ syscall_printf ("%d = fstat (%d, %p)", res, fd, buf);
+ return res;
}
/* fsync: P96 6.6.1.1 */
fsync (int fd)
{
sigframe thisframe (mainthread);
- if (cygheap->fdtab.not_open (fd))
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
{
syscall_printf ("-1 = fsync (%d)", fd);
- set_errno (EBADF);
return -1;
}
- HANDLE h = cygheap->fdtab[fd]->get_handle ();
-
- if (FlushFileBuffers (h) == 0)
+ if (FlushFileBuffers (cfd->get_handle ()) == 0)
{
__seterrno ();
return -1;
extern "C" long int
fpathconf (int fd, int v)
{
- if (cygheap->fdtab.not_open (fd))
- {
- set_errno (EBADF);
- return -1;
- }
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ return -1;
switch (v)
{
case _PC_LINK_MAX:
case _PC_NO_TRUNC:
return -1;
case _PC_VDISABLE:
- if (isatty (fd))
+ if (cfd->is_tty ())
return -1;
else
{
}
case _PC_POSIX_PERMISSIONS:
case _PC_POSIX_SECURITY:
- {
- fhandler_base *fh = cygheap->fdtab[fd];
- if (fh->get_device () == FH_DISK)
- return check_posix_perm (fh->get_win32_name (), v);
- set_errno (EINVAL);
- return -1;
- }
+ if (cfd->get_device () == FH_DISK)
+ return check_posix_perm (cfd->get_win32_name (), v);
+ set_errno (EINVAL);
+ return -1;
default:
set_errno (EINVAL);
return -1;
extern "C" char *
ttyname (int fd)
{
- if (cygheap->fdtab.not_open (fd) || !cygheap->fdtab[fd]->is_tty ())
+ cygheap_fdget cfd (fd);
+ if (cfd < 0 || !cfd->is_tty ())
{
return 0;
}
- return (char *) (cygheap->fdtab[fd]->ttyname ());
+ return (char *) (cfd->ttyname ());
}
extern "C" char *
return 0; /* we do it for old apps, due to getc/putc macros */
}
- if (cygheap->fdtab.not_open (fd))
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
{
syscall_printf (" _cifs: fd not open\n");
return 0;
}
- fhandler_base *p = cygheap->fdtab[fd];
-
- if (p->get_device () != FH_DISK)
+ if (cfd->get_device () != FH_DISK)
{
syscall_printf (" _cifs: fd not disk file\n");
return 0;
}
- if (p->get_w_binary () || p->get_r_binary ())
+ if (cfd->get_w_binary () || cfd->get_r_binary ())
{
syscall_printf (" _cifs: get_*_binary\n");
return 0;
extern "C" int
getmode (int fd)
{
- if (cygheap->fdtab.not_open (fd))
- {
- set_errno (EBADF);
- return -1;
- }
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ return -1;
- return cygheap->fdtab[fd]->get_flags () & (O_BINARY | O_TEXT);
+ return cfd->get_flags () & (O_BINARY | O_TEXT);
}
/* Set a file descriptor into text or binary mode, returning the
extern "C" int
setmode (int fd, int mode)
{
- if (cygheap->fdtab.not_open (fd))
- {
- set_errno (EBADF);
- return -1;
- }
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ return -1;
if (mode != O_BINARY && mode != O_TEXT && mode != 0)
{
set_errno (EINVAL);
return -1;
}
- fhandler_base *p = cygheap->fdtab[fd];
-
/* Note that we have no way to indicate the case that writes are
binary but not reads, or vice-versa. These cases can arise when
using the tty or console interface. People using those
interfaces should not use setmode. */
int res;
- if (p->get_w_binary () && p->get_r_binary ())
+ if (cfd->get_w_binary () && cfd->get_r_binary ())
res = O_BINARY;
- else if (p->get_w_binset () && p->get_r_binset ())
+ else if (cfd->get_w_binset () && cfd->get_r_binset ())
res = O_TEXT; /* Specifically set O_TEXT */
else
res = 0;
if (!mode)
- p->reset_to_open_binmode ();
+ cfd->reset_to_open_binmode ();
else if (mode & O_BINARY)
{
- p->set_w_binary (1);
- p->set_r_binary (1);
+ cfd->set_w_binary (1);
+ cfd->set_r_binary (1);
}
else
{
- p->set_w_binary (0);
- p->set_r_binary (0);
+ cfd->set_w_binary (0);
+ cfd->set_r_binary (0);
}
if (_cygwin_istext_for_stdio (fd))
setmode_file = fd;
_fwalk (_REENT, setmode_helper);
- syscall_printf ("setmode (%d<%s>, %s) returns %s\n", fd, p->get_name (),
+ syscall_printf ("setmode (%d<%s>, %s) returns %s\n", fd, cfd->get_name (),
mode & O_TEXT ? "text" : "binary",
res & O_TEXT ? "text" : "binary");
return res;
int res = -1;
if (length < 0)
- {
- set_errno (EINVAL);
- }
- else if (cygheap->fdtab.not_open (fd))
- {
- set_errno (EBADF);
- }
+ set_errno (EINVAL);
else
{
- HANDLE h = cygheap->fdtab[fd]->get_handle ();
- off_t prev_loc;
-
- if (h)
+ cygheap_fdget cfd (fd);
+ if (cfd >= 0)
{
- /* remember curr file pointer location */
- prev_loc = cygheap->fdtab[fd]->lseek (0, SEEK_CUR);
+ HANDLE h = cygheap->fdtab[fd]->get_handle ();
- cygheap->fdtab[fd]->lseek (length, SEEK_SET);
- if (!SetEndOfFile (h))
+ if (cfd->get_handle ())
{
- __seterrno ();
- }
- else
- res = 0;
+ /* remember curr file pointer location */
+ off_t prev_loc = cfd->lseek (0, SEEK_CUR);
- /* restore original file pointer location */
- cygheap->fdtab[fd]->lseek (prev_loc, 0);
+ cfd->lseek (length, SEEK_SET);
+ if (!SetEndOfFile (h))
+ __seterrno ();
+ else
+ res = 0;
+
+ /* restore original file pointer location */
+ cfd->lseek (prev_loc, 0);
+ }
}
}
- syscall_printf ("%d = ftruncate (%d, %d)", res, fd, length);
+ syscall_printf ("%d = ftruncate (%d, %d)", res, fd, length);
return res;
}
extern "C" long
get_osfhandle (int fd)
{
- long res = -1;
+ long res;
- if (cygheap->fdtab.not_open (fd))
- set_errno (EBADF);
+ cygheap_fdget cfd (fd);
+ if (cfd >= 0)
+ res = (long) cfd->get_handle ();
else
- res = (long) cygheap->fdtab[fd]->get_handle ();
+ res = -1;
syscall_printf ("%d = get_osfhandle (%d)", res, fd);
return res;
fstatfs (int fd, struct statfs *sfs)
{
sigframe thisframe (mainthread);
- if (cygheap->fdtab.not_open (fd))
- {
- set_errno (EBADF);
- return -1;
- }
- fhandler_disk_file *f = (fhandler_disk_file *) cygheap->fdtab[fd];
- return statfs (f->get_name (), sfs);
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ return -1;
+ return statfs (cfd->get_name (), sfs);
}
/* setpgid: POSIX 4.3.3.1 */
ptsname (int fd)
{
sigframe thisframe (mainthread);
- if (cygheap->fdtab.not_open (fd))
- {
- set_errno (EBADF);
- return 0;
- }
- return (char *) (cygheap->fdtab[fd]->ptsname ());
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ return 0;
+ return (char *) (cfd->ptsname ());
}
/* FIXME: what is this? */
#include "fhandler.h"
#include "path.h"
#include "dtable.h"
-#include "cygheap.h"
#include "cygerrno.h"
+#include "cygheap.h"
#include "ntdll.h"
/* sysconf: POSIX 4.8.1.1 */
#include <syslog.h>
#include <stdarg.h>
#include <unistd.h>
+#include <errno.h>
#include "security.h"
#include "fhandler.h"
#include "path.h"
{
int res = -1;
- if (cygheap->fdtab.not_open (fd))
- {
- set_errno (EBADF);
- goto out;
- }
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ goto out;
- fhandler_base *fh;
- fh = cygheap->fdtab[fd];
-
- if (!fh->is_tty ())
+ if (!cfd->is_tty ())
set_errno (ENOTTY);
- else
- {
- if ((res = fh->bg_check (-SIGTTOU)) > bg_eof)
- res = fh->tcsendbreak (duration);
- }
+ else if ((res = cfd->bg_check (-SIGTTOU)) > bg_eof)
+ res = cfd->tcsendbreak (duration);
out:
syscall_printf ("%d = tcsendbreak (%d, %d)", res, fd, duration);
termios_printf ("tcdrain");
- if (cygheap->fdtab.not_open (fd))
- {
- set_errno (EBADF);
- goto out;
- }
-
- fhandler_base *fh;
- fh = cygheap->fdtab[fd];
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ goto out;
- if (!fh->is_tty ())
+ if (!cfd->is_tty ())
set_errno (ENOTTY);
- else
- {
- if ((res = fh->bg_check (-SIGTTOU)) > bg_eof)
- res = fh->tcdrain ();
- }
+ else if ((res = cfd->bg_check (-SIGTTOU)) > bg_eof)
+ res = cfd->tcdrain ();
out:
syscall_printf ("%d = tcdrain (%d)", res, fd);
{
int res = -1;
- if (cygheap->fdtab.not_open (fd))
- {
- set_errno (EBADF);
- goto out;
- }
-
- fhandler_base *fh;
- fh = cygheap->fdtab[fd];
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ goto out;
- if (!fh->is_tty ())
+ if (!cfd->is_tty ())
set_errno (ENOTTY);
- else
- {
- if ((res = fh->bg_check (-SIGTTOU)) > bg_eof)
- res = fh->tcflush (queue);
- }
+ else if ((res = cfd->bg_check (-SIGTTOU)) > bg_eof)
+ res = cfd->tcflush (queue);
out:
termios_printf ("%d = tcflush (%d, %d)", res, fd, queue);
{
int res = -1;
- if (cygheap->fdtab.not_open (fd))
- {
- set_errno (EBADF);
- goto out;
- }
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ goto out;
- fhandler_base *fh;
- fh = cygheap->fdtab[fd];
-
- if (!fh->is_tty ())
+ if (!cfd->is_tty ())
set_errno (ENOTTY);
- else
- {
- if ((res = fh->bg_check (-SIGTTOU)) > bg_eof)
- res = fh->tcflow (action);
- }
+ else if ((res = cfd->bg_check (-SIGTTOU)) > bg_eof)
+ res = cfd->tcflow (action);
out:
syscall_printf ("%d = tcflow (%d, %d)", res, fd, action);
{
int res = -1;
- if (cygheap->fdtab.not_open (fd))
- {
- set_errno (EBADF);
- goto out;
- }
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ goto out;
t = __tonew_termios (t);
- fhandler_base *fh;
- fh = cygheap->fdtab[fd];
-
- if (!fh->is_tty ())
+ if (!cfd->is_tty ())
set_errno (ENOTTY);
- else
- {
- if ((res = fh->bg_check (-SIGTTOU)) > bg_eof)
- res = fh->tcsetattr (a, t);
- }
+ else if ((res = cfd->bg_check (-SIGTTOU)) > bg_eof)
+ res = cfd->tcsetattr (a, t);
out:
termios_printf ("iflag %x, oflag %x, cflag %x, lflag %x, VMIN %d, VTIME %d",
int res = -1;
struct termios *t = __makenew_termios (in_t);
- if (cygheap->fdtab.not_open (fd))
- set_errno (EBADF);
- else if (!cygheap->fdtab[fd]->is_tty ())
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ /* saw an error */;
+ else if (!cfd->is_tty ())
set_errno (ENOTTY);
- else
- {
- if ((res = cygheap->fdtab[fd]->tcgetattr (t)) == 0)
- (void) __toapp_termios (in_t, t);
- }
+ else if ((res = cfd->tcgetattr (t)) == 0)
+ (void) __toapp_termios (in_t, t);
if (res)
termios_printf ("%d = tcgetattr (%d, %p)", res, fd, in_t);
{
int res = -1;
- if (cygheap->fdtab.not_open (fd))
- set_errno (EBADF);
- else if (!cygheap->fdtab[fd]->is_tty ())
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ /* saw an error */;
+ else if (!cfd->is_tty ())
set_errno (ENOTTY);
else
- res = cygheap->fdtab[fd]->tcgetpgrp ();
+ res = cfd->tcgetpgrp ();
termios_printf ("%d = tcgetpgrp (%d)", res, fd);
return res;
{
int res = -1;
- if (cygheap->fdtab.not_open (fd))
- set_errno (EBADF);
- else if (!cygheap->fdtab[fd]->is_tty ())
+ cygheap_fdget cfd (fd);
+ if (cfd < 0)
+ /* saw an error */;
+ else if (!cfd->is_tty ())
set_errno (ENOTTY);
else
- res = cygheap->fdtab[fd]->tcsetpgrp (pgid);
+ res = cfd->tcsetpgrp (pgid);
termios_printf ("%d = tcsetpgrp (%d, %x)", res, fd, pgid);
return res;
#include <limits.h>
#include <stdlib.h>
#include <lm.h>
+#include <errno.h>
#include <sys/cygwin.h>
#include "sync.h"
#include "sigproc.h"
#include "fhandler.h"
#include "path.h"
#include "dtable.h"
+#include "cygerrno.h"
#include "cygheap.h"
#include "registry.h"