* Makefile.in (DLL_OFILES): Add fhandler_mailslot.o.

* devices.h (FH_KMSG): Define new device.
	* devices.in: Add "/dev/kmsg" entry.
	* devices.cc: Regenerate.
	* dtable.cc (build_fh_pc): Handle case FH_KMSG.
	* fhandler.h (class fhandler_mailslot): New class.
	(class select_stuff): Add device_specific_mailslot pointer.
	* fhandler_mailslot.cc: New file.
	* select.cc (peek_mailslot): New function.
	(verify_mailslot): Ditto.
	(struct mailslotinf): New stuct to handle select on mailslots.
	(thread_mailslot): New function.
	(start_thread_mailslot): Ditto.
	(mailslot_cleanup): Ditto.
	(fhandler_mailslot::select_read): New method.
	* syslog.cc (klog_guard): New muto.
	(dev_kmsg): Local mailslot for kernel message device.
	(vklog): New function.
	(klog): Ditto.
	* winsup.h (vklog): Declare.
	(klog): Ditto.
	* include/sys/syslog.h: Define _PATH_KLOG.
This commit is contained in:
Corinna Vinschen 2005-05-10 20:56:07 +00:00
parent a586e5b6ae
commit 13505ca8fc
12 changed files with 1179 additions and 803 deletions

View File

@ -1,3 +1,28 @@
2005-05-10 Corinna Vinschen <corinna@vinschen.de>
* Makefile.in (DLL_OFILES): Add fhandler_mailslot.o.
* devices.h (FH_KMSG): Define new device.
* devices.in: Add "/dev/kmsg" entry.
* devices.cc: Regenerate.
* dtable.cc (build_fh_pc): Handle case FH_KMSG.
* fhandler.h (class fhandler_mailslot): New class.
(class select_stuff): Add device_specific_mailslot pointer.
* fhandler_mailslot.cc: New file.
* select.cc (peek_mailslot): New function.
(verify_mailslot): Ditto.
(struct mailslotinf): New stuct to handle select on mailslots.
(thread_mailslot): New function.
(start_thread_mailslot): Ditto.
(mailslot_cleanup): Ditto.
(fhandler_mailslot::select_read): New method.
* syslog.cc (klog_guard): New muto.
(dev_kmsg): Local mailslot for kernel message device.
(vklog): New function.
(klog): Ditto.
* winsup.h (vklog): Declare.
(klog): Ditto.
* include/sys/syslog.h: Define _PATH_KLOG.
2005-05-10 Christopher Faylor <cgf@timesys.com>
* dcrt0.cc (dll_crt0_1): Call cygwin_exit to ensure that destructors

View File

@ -123,10 +123,10 @@ DLL_OFILES:=assert.o autoload.o bsdlib.o ctype.o cxx.o cygheap.o cygthread.o \
cygtls.o dcrt0.o debug.o delqueue.o devices.o dir.o dlfcn.o dll_init.o \
dtable.o environ.o errno.o exceptions.o exec.o external.o fcntl.o \
fhandler.o fhandler_clipboard.o fhandler_console.o fhandler_disk_file.o \
fhandler_dsp.o fhandler_fifo.o fhandler_floppy.o fhandler_mem.o \
fhandler_netdrive.o fhandler_nodevice.o fhandler_proc.o fhandler_process.o \
fhandler_random.o fhandler_raw.o fhandler_registry.o fhandler_serial.o \
fhandler_socket.o fhandler_tape.o fhandler_termios.o \
fhandler_dsp.o fhandler_fifo.o fhandler_floppy.o fhandler_mailslot.o \
fhandler_mem.o fhandler_netdrive.o fhandler_nodevice.o fhandler_proc.o \
fhandler_process.o fhandler_random.o fhandler_raw.o fhandler_registry.o \
fhandler_serial.o fhandler_socket.o fhandler_tape.o fhandler_termios.o \
fhandler_tty.o fhandler_virtual.o fhandler_windows.o fhandler_zero.o \
flock.o fnmatch.o fork.o getopt.o glob.o grp.o heap.o hookapi.o \
init.o ioctl.o ipc.o iruserok.o localtime.o lsearch.o malloc_wrapper.o \

File diff suppressed because it is too large Load Diff

View File

@ -101,6 +101,7 @@ enum fh_devices
FH_FULL = FHDEV (1, 7),
FH_RANDOM = FHDEV (1, 8),
FH_URANDOM = FHDEV (1, 9),
FH_KMSG = FHDEV (1, 11),
FH_OSS_DSP = FHDEV (14, 3),
DEV_CYGDRIVE_MAJOR = 98,

View File

@ -81,6 +81,7 @@ const device dev_bad_storage =
"/dev/sr%(0-15)d", BRACK(FHDEV(DEV_CDROM_MAJOR, {$1})), "\\Device\\CdRom{$1}"
"/dev/sd%{a-z}s", BRACK(FH_SD{uc $1}), "\\Device\\Harddisk{ord($1) - ord('a')}\\Partition0"
"/dev/sd%{a-z}s%(1-15)d", BRACK(FH_SD{uc $1} | {$2}), "\\Device\\Harddisk{ord($1) - ord('a')}\\Partition{$2 % 16}"
"/dev/kmsg", BRACK(FH_KMSG), "\\\\.\\mailslot\\cygwin\\dev\\kmsg"
%other {return NULL;}
%%
#undef BRACK

View File

@ -465,6 +465,9 @@ build_fh_pc (path_conv& pc)
fh = cnew (fhandler_tty_slave) ();
break;
}
case FH_KMSG:
fh = cnew (fhandler_mailslot) ();
break;
}
}

View File

@ -360,6 +360,17 @@ class fhandler_base
friend class fhandler_fifo;
};
class fhandler_mailslot : public fhandler_base
{
public:
fhandler_mailslot ();
int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
int open (int flags, mode_t mode = 0);
int write (const void *ptr, size_t len);
int ioctl (unsigned int cmd, void *);
select_record *select_read (select_record *s);
};
class fhandler_socket: public fhandler_base
{
private:
@ -1274,6 +1285,7 @@ typedef union
char __dev_tape[sizeof (fhandler_dev_tape)];
char __dev_zero[sizeof (fhandler_dev_zero)];
char __disk_file[sizeof (fhandler_disk_file)];
char __mailslot[sizeof (fhandler_mailslot)];
char __pipe[sizeof (fhandler_pipe)];
char __proc[sizeof (fhandler_proc)];
char __process[sizeof (fhandler_process)];
@ -1327,6 +1339,7 @@ class select_stuff
void *device_specific_pipe;
void *device_specific_socket;
void *device_specific_serial;
void *device_specific_mailslot;
int test_and_set (int i, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds);
@ -1336,7 +1349,8 @@ class select_stuff
select_stuff (): always_ready (0), windows_used (0), start (0),
device_specific_pipe (0),
device_specific_socket (0),
device_specific_serial (0) {}
device_specific_serial (0),
device_specific_mailslot (0) {}
};
int __stdcall set_console_state_for_spawn ();

View File

@ -0,0 +1,160 @@
/* fhandler_mailslot.cc. See fhandler.h for a description of the fhandler classes.
Copyright 2005 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 <unistd.h>
#include <sys/termios.h>
#include <ntdef.h>
#include "cygerrno.h"
#include "perprocess.h"
#include "security.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "ntdll.h"
/**********************************************************************/
/* fhandler_mailslot */
fhandler_mailslot::fhandler_mailslot ()
: fhandler_base ()
{
}
int __stdcall
fhandler_mailslot::fstat (struct __stat64 *buf)
{
debug_printf ("here");
fhandler_base::fstat (buf);
if (is_auto_device ())
{
buf->st_mode = S_IFCHR | S_IRUSR | S_IWUSR;
buf->st_uid = geteuid32 ();
buf->st_gid = getegid32 ();
buf->st_nlink = 1;
buf->st_blksize = S_BLKSIZE;
time_as_timestruc_t (&buf->st_ctim);
buf->st_atim = buf->st_mtim = buf->st_ctim;
}
return 0;
}
int
fhandler_mailslot::open (int flags, mode_t mode)
{
int res = 0;
HANDLE x;
switch (flags & O_ACCMODE)
{
case O_RDONLY: /* Server */
x = CreateMailslot (get_win32_name (),
0, /* Any message size */
(flags & O_NONBLOCK) ? 0 : MAILSLOT_WAIT_FOREVER,
&sec_none);
if (x == INVALID_HANDLE_VALUE)
{
/* FIXME: It's not possible to open the read side of an existing
mailslot using CreateFile. You'll get a handle, but using it
in ReadFile returns ERROR_INVALID_PARAMETER. On the other
hand, CreateMailslot returns with ERROR_ALREADY_EXISTS if the
mailslot has been created already.
So this is an exclusive open for now. *Duplicating* read side
handles works, though, so it might be an option to duplicate
the handle from the first process to the current process for
opening the mailslot. */
#if 0
if (GetLastError () != ERROR_ALREADY_EXISTS)
{
__seterrno ();
break;
}
x = CreateFile (get_win32_name (), GENERIC_READ, wincap.shared (),
&sec_none, OPEN_EXISTING, 0, 0);
#endif
if (x == INVALID_HANDLE_VALUE)
{
__seterrno ();
break;
}
}
set_io_handle (x);
set_flags (flags, O_BINARY);
res = 1;
set_open_status ();
break;
case O_WRONLY: /* Client */
/* The client is the DLL exclusively. Don't allow opening from
application code. */
extern fhandler_mailslot *dev_kmsg;
if (this != dev_kmsg)
{
set_errno (EPERM); /* As on Linux. */
break;
}
x = CreateFile (get_win32_name (), GENERIC_WRITE, wincap.shared (),
&sec_none, OPEN_EXISTING, 0, 0);
if (x == INVALID_HANDLE_VALUE)
{
__seterrno ();
break;
}
set_io_handle (x);
set_flags (flags, O_BINARY);
res = 1;
set_open_status ();
break;
default:
set_errno (EINVAL);
break;
}
return res;
}
int
fhandler_mailslot::write (const void *ptr, size_t len)
{
/* Check for 425/426 byte weirdness */
if (len == 425 || len == 426)
{
char buf[427];
buf[425] = buf[426] = '\0';
memcpy (buf, ptr, len);
return raw_write (buf, 427);
}
return raw_write (ptr, len);
}
int
fhandler_mailslot::ioctl (unsigned int cmd, void *buf)
{
int res = -1;
switch (cmd)
{
case FIONBIO:
{
DWORD timeout = buf ? 0 : MAILSLOT_WAIT_FOREVER;
if (!SetMailslotInfo (get_handle (), timeout))
{
debug_printf ("SetMailslotInfo (%u): %E", timeout);
break;
}
}
/*FALLTHRU*/
default:
res = fhandler_base::ioctl (cmd, buf);
break;
}
return res;
}

View File

@ -15,6 +15,7 @@ details. */
#include <stdarg.h>
#define _PATH_LOG "/dev/log"
#define _PATH_KLOG "/dev/kmsg"
#define LOG_EMERG 0
#define LOG_ALERT 1

View File

@ -1591,3 +1591,123 @@ fhandler_windows::select_except (select_record *s)
s->windows_handle = true;
return s;
}
static int
peek_mailslot (select_record *me, bool)
{
HANDLE h;
set_handle_or_return_if_not_open (h, me);
if (me->read_selected && me->read_ready)
return 1;
DWORD msgcnt = 0;
if (!GetMailslotInfo (h, NULL, NULL, &msgcnt, NULL))
{
select_printf ("mailslot %d(%p) error %E", me->fd, h);
return 1;
}
if (msgcnt > 0)
{
me->read_ready = true;
select_printf ("mailslot %d(%p) ready", me->fd, h);
return 1;
}
select_printf ("mailslot %d(%p) not ready", me->fd, h);
return 0;
}
static int
verify_mailslot (select_record *me, fd_set *rfds, fd_set *wfds,
fd_set *efds)
{
return peek_mailslot (me, true);
}
static int start_thread_mailslot (select_record *me, select_stuff *stuff);
struct mailslotinf
{
cygthread *thread;
bool stop_thread_mailslot;
select_record *start;
};
static DWORD WINAPI
thread_mailslot (void *arg)
{
mailslotinf *mi = (mailslotinf *) arg;
bool gotone = false;
for (;;)
{
select_record *s = mi->start;
while ((s = s->next))
if (s->startup == start_thread_mailslot)
{
if (peek_mailslot (s, true))
gotone = true;
if (mi->stop_thread_mailslot)
{
select_printf ("stopping");
goto out;
}
}
/* Paranoid check */
if (mi->stop_thread_mailslot)
{
select_printf ("stopping from outer loop");
break;
}
if (gotone)
break;
Sleep (10);
}
out:
return 0;
}
static int
start_thread_mailslot (select_record *me, select_stuff *stuff)
{
if (stuff->device_specific_mailslot)
{
me->h = *((mailslotinf *) stuff->device_specific_mailslot)->thread;
return 1;
}
mailslotinf *mi = new mailslotinf;
mi->start = &stuff->start;
mi->stop_thread_mailslot = false;
mi->thread = new cygthread (thread_mailslot, (LPVOID) mi, "select_mailslot");
me->h = *mi->thread;
if (!me->h)
return 0;
stuff->device_specific_mailslot = (void *) mi;
return 1;
}
static void
mailslot_cleanup (select_record *, select_stuff *stuff)
{
mailslotinf *mi = (mailslotinf *) stuff->device_specific_mailslot;
if (mi && mi->thread)
{
mi->stop_thread_mailslot = true;
mi->thread->detach ();
delete mi;
stuff->device_specific_mailslot = NULL;
}
}
select_record *
fhandler_mailslot::select_read (select_record *s)
{
if (!s)
s = new select_record;
s->startup = start_thread_mailslot;
s->peek = peek_mailslot;
s->verify = verify_mailslot;
s->cleanup = mailslot_cleanup;
s->read_selected = true;
s->read_ready = false;
return s;
}

View File

@ -461,6 +461,38 @@ syslog (int priority, const char *message, ...)
va_end (ap);
}
static NO_COPY muto klog_guard;
fhandler_mailslot *dev_kmsg;
extern "C" void
vklog (int priority, const char *message, va_list ap)
{
/* TODO: kernel messages are under our control entirely and they should
be quick. No playing with /dev/null, but a fixed upper size for now. */
char buf[2060]; /* 2048 + a prority */
__small_sprintf (buf, "<%d>", priority);
__small_vsprintf (buf + strlen (buf), message, ap);
klog_guard.init ("klog_guard")->acquire ();
if (!dev_kmsg)
dev_kmsg = (fhandler_mailslot *) build_fh_name ("/dev/kmsg");
if (dev_kmsg && !dev_kmsg->get_handle ())
dev_kmsg->open (O_WRONLY, 0);
if (dev_kmsg && dev_kmsg->get_handle ())
dev_kmsg->write (buf, strlen (buf) + 1);
else
vsyslog (priority, message, ap);
klog_guard.release ();
}
extern "C" void
klog (int priority, const char *message, ...)
{
va_list ap;
va_start (ap, message);
vklog (priority, message, ap);
va_end (ap);
}
extern "C" void
closelog (void)
{

View File

@ -296,6 +296,9 @@ extern "C" int __small_sprintf (char *dst, const char *fmt, ...) /*__attribute__
extern "C" int __small_vsprintf (char *dst, const char *fmt, va_list ap) /*__attribute__ ((regparm (3)))*/;
extern void multiple_cygwin_problem (const char *, unsigned, unsigned);
extern "C" void vklog (int priority, const char *message, va_list ap);
extern "C" void klog (int priority, const char *message, ...);
int symlink_worker (const char *, const char *, bool, bool)
__attribute__ ((regparm (3)));