newlib/winsup/cygwin/fhandler_procsysvipc.cc

348 lines
9.1 KiB
C++
Raw Normal View History

/* fhandler_procsysvipc.cc: fhandler for /proc/sysvipc virtual filesystem
Copyright 2011 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>
#include <stdio.h>
#include <sys/cygwin.h>
#include "cygerrno.h"
#include "cygserver.h"
#include "security.h"
#include "path.h"
#include "fhandler.h"
#include "fhandler_virtual.h"
#include "pinfo.h"
#include "shared_info.h"
#include "dtable.h"
#include "cygheap.h"
#include "ntdll.h"
#include "cygtls.h"
#include "pwdgrp.h"
#include "tls_pbuf.h"
#include <sys/param.h>
#include <ctype.h>
#define _COMPILING_NEWLIB
#include <dirent.h>
#define _KERNEL
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
static _off64_t format_procsysvipc_msg (void *, char *&);
static _off64_t format_procsysvipc_sem (void *, char *&);
static _off64_t format_procsysvipc_shm (void *, char *&);
static const virt_tab_t procsysvipc_tab[] =
{
{ _VN ("."), FH_PROCSYSVIPC, virt_directory, NULL },
{ _VN (".."), FH_PROCSYSVIPC, virt_directory, NULL },
{ _VN ("msg"), FH_PROCSYSVIPC, virt_file, format_procsysvipc_msg },
{ _VN ("sem"), FH_PROCSYSVIPC, virt_file, format_procsysvipc_sem },
{ _VN ("shm"), FH_PROCSYSVIPC, virt_file, format_procsysvipc_shm },
* autoload.cc: Call _api_fatal in asm. * child_info.h: Redefine CURR_CHILD_INFO_MAGIC. (child_info_fork::abort): Rename from handle_failure. Change arguments. * cygtls.h (_local_storage::ttybuf): New field. * dcrt0.cc (vapi_fatal): Split api_fatal. Add "in forked process" to message when appropriate. (api_fatal): Use vapi_fatal. * devices.h: Make multiple inclusion safe. (fh_devices): Add FH_CONS* stuff. Reorder slightly. (device): Eliminate anonymous union. Add more ways to access minor/major. (device::setunit): Accommodate no-longer-anonymous union. (device::is_fs): Ditto. (device::is_fs_special): Ditto. (device::major): New function. (device::minor): Ditto. (device::is_device): New function. (device::not_device): Ditto. (device::operator int): New operator. (device::operator fh_devices): Ditto. (device::operator bool): Ditto. (device::operator DWORD): Ditto. (device::operator =): Ditto. (isproc_dev): New function. (isprocsys_dev): Ditto. (iscons_dev): Ditto. (istty_slave_dev): Ditto. * devices.in: Add new "/dev/cons*" strings. Accommodate no-longer-anonymous union throughout. (BRACK): Use more precise method for initialization. * devices.cc: Regenerate. * dtable.cc (dtable::stdio_init): Use get_cttyp instead of get_tty. (dtable::find_archetype): Use new DWORD operator in device to test archetypes. (dtable::init_std_file_from_handle): Use different method to initialize 'dev'. Adapt to different ctty handling and accommodate /dev/cons*. (fh_alloc): Accommodate no-longer-anonymous union. Adapt to new /dev/cons*. (build_fh_pc): Make debugging output more useful. * exceptions.cc (ctrl_c_handler): Use get_cttyp instead of get_tty. * external.cc (fillout_pinfo): Accommodate new cons* stuff. * fhandler.cc (fhandler_base::read): Eliminate is_slow() test. * fhandler.h (fhandler_base::*): Adapt to changes in device.h. (fhandler_*::is_slow): Delete. ( fhandler_proc::get_proc_fhandler): Return fh_devices type. * fhandler_console.cc (open_shared_console): New function. (console_unit): New class. (console_unit::console_unit): New constructor. (enum_windows): New function. Declare as friend to console_unit. (fhandler_console::set_unit): New function. (fhandler_console::get_tty_stuff): Call set_unit to set the unit number and determine if initialization is needed. Eliminate flags parameter. (tty_list::get_cttyp): Rename (sorta) from get_tty. Return pointer to correct tty_min. (fhandler_console::open): Adapt to elimination of argument to get_tty_stuff. (fhandler_console::output_tcsetattr): Properly detect error condition. (fhandler_console::fixup_after_fork_exec): Adapt to get_tty_stuff() setting tc automatically. * fhandler_proc.cc: Use FH_BAD rather than 0 throughout where using fh_devices enum. (fhandler_proc::get_proc_fhandler): Return fh_devices. Adapt to devices.h changes. * fhandler_process.cc: Adapt to devices.h changes. Use FH_BAD rather than 0 throughout where using fh_devices enum. * fhandler_procnet.cc: Ditto. * fhandler_procsys.cc: Ditto. * fhandler_procsysvipc.cc: Ditto. * fhandler_tape.cc (fhandler_dev_tape::fhandler_dev_tape): Ditto. * fhandler_termios.cc (handler_termios::bg_check): Use tc->ttyname() rather than assuming that we can construct a tty. * fhandler_tty.cc (fhandler_tty_master::fhandler_tty_master): Just return get_minor() of dev. (fhandler_pty_master::process_slave_output): Add slightly more debugging info. (fhandler_tty_slave::fhandler_tty_slave): Change name from ntty to unit. (fhandler_pty_master::open): Ditto. (fhandler_tty_slave::ioctl): Adapt to change which causes ctty to represent a complete device. (fhandler_tty_master::init_console): Add debugging for failure path. (fhandler_pty_master::setup): Use get_unit() to retrieve unit number rather than relying on raw ntty. (fhandler_pty_master::setup): Ditto. * fhandler_virtual.h (virt_tab_t): Redefine fhandler as fh_devices. * fork.cc: Remove obsolete vfork stuff. (frok::child): Don't assume that a ctty == 0 is valid. * mount.cc (mount_info::conv_to_win32_path): Adapt to device struct changes. (mount_info::conv_to_win32_path): Ditto. * path.cc (path_conv::check): Retrive major/minor numbers via a method rather than accessing them directly from device. Rely on dev operators to set/retrieve device information as required by device struct change. * path.h (isproc_dev): Move to devices.h. (isprocsys_dev): Ditto. (isvirtual_dev): Ditto. (path_conv:{isdevice,isfifo,isspecial,iscygdrive,issocket,get_devn,get_unitn}): Use device methods to access/manipulate devices. * pinfo.cc (pinfo::exit): Don't assume that ctty == 0 is valid. Use iscons_dev to determine if a device is a console. (_pinfo::_ctty): Use device::parse to generate tty/cons name. (_pinfo::set_ctty): Don't assume that ctty == 0 is valid. Remove redundant info from debugging. * shared.cc (offsets): Remove console offset. * shared_info.h (shared_locations): Ditto. * syscalls.cc (umask): Use device methods to manipulate device information. (ctermid): Use device::parse to generate term device name. * tlsoffsets.h: Regenerate. * tty.cc (ttyslot): Return minor number of ctty since ctty now represents a full device. (tty::create_master): Set ctty to a complete device. (tty_list::attach): Rework to detect new /dev/cons* stuff. (tty_list::terminate): Adapt to changes to ctty. (tty_list::init): Adapt to change to setntty - pass in device major number. (tty::exists): Use get_unit() to retrive tty unit number. (tty::open_mutex): Ditto. (tty::open_inuse): Ditto. (tty::create_inuse): Ditto. (tty::get_event): Ditto. (tty_min::ttyname): Define new function. * tty.h (tty_min::ntty): Redefine as fh_devices. (tty::exists): Use get_unit() to retrive tty unit number. (tty::open_mutex): Ditto. (tty::open_inuse): Ditto. (tty::create_inuse): Ditto. (tty::get_event): Ditto. (tty_min::ttyname): Declare new function. (tty::getntty): Declare as const. (tty_list::operator []): Assure that only minor part of argument is used. * dll_init.cc (dll_list::alloc): Detect mismatch of data segments early issuing an explicit error message if necessary. * heap.cc (heap_init): Adapt to changes from fork->handle_failure to fork->abort. * pinfo.h (EXITCODE_FORK_FAILED): New enum. (from Ryan Johnson) * sigproc.cc (child_info_fork::abort): Rename from handle_failure. Change arguments to allow passing in a printf-like message. * winsup.h (api_fatal): Delete macro definition. (api_fatal): Redefine from __api_fatal. (vapi_fatal): Declare new function. * include/sys/strace.h (strace_vprintf): Define new macro. * ntdll.h (_SYSTEM_INFORMATION_CLASS): Add SystemHandleInformation.
2011-05-28 20:17:09 +02:00
{ NULL, 0, FH_BAD, virt_none, NULL }
};
static const int PROCSYSVIPC_LINK_COUNT =
(sizeof (procsysvipc_tab) / sizeof (virt_tab_t)) - 1;
/* Returns 0 if path doesn't exist, >0 if path is a directory,
* -1 if path is a file.
*/
virtual_ftype_t
fhandler_procsysvipc::exists ()
{
const char *path = get_name ();
debug_printf ("exists (%s)", path);
path += proc_len + 1;
while (*path != 0 && !isdirsep (*path))
path++;
if (*path == 0)
return virt_rootdir;
virt_tab_t *entry = virt_tab_search (path + 1, true, procsysvipc_tab,
PROCSYSVIPC_LINK_COUNT);
cygserver_init();
if (entry)
{
if (entry->type == virt_file)
{
if (cygserver_running != CYGSERVER_OK)
return virt_none;
}
fileid = entry - procsysvipc_tab;
return entry->type;
}
return virt_none;
}
fhandler_procsysvipc::fhandler_procsysvipc ():
fhandler_proc ()
{
}
int
fhandler_procsysvipc::fstat (struct __stat64 *buf)
{
fhandler_base::fstat (buf);
buf->st_mode &= ~_IFMT & NO_W;
int file_type = exists ();
switch (file_type)
{
case virt_none:
set_errno (ENOENT);
return -1;
case virt_directory:
case virt_rootdir:
buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
buf->st_nlink = 2;
return 0;
case virt_file:
default:
buf->st_mode |= S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
return 0;
}
}
int
fhandler_procsysvipc::readdir (DIR *dir, dirent *de)
{
int res = ENMFILE;
if (dir->__d_position >= PROCSYSVIPC_LINK_COUNT)
goto out;
{
cygserver_init();
if (cygserver_running != CYGSERVER_OK)
goto out;
}
strcpy (de->d_name, procsysvipc_tab[dir->__d_position++].name);
dir->__flags |= dirent_saw_dot | dirent_saw_dot_dot;
res = 0;
out:
syscall_printf ("%d = readdir (%p, %p) (%s)", res, dir, de, de->d_name);
return res;
}
int
fhandler_procsysvipc::open (int flags, mode_t mode)
{
int res = fhandler_virtual::open (flags, mode);
if (!res)
goto out;
nohandle (true);
const char *path;
path = get_name () + proc_len + 1;
pid = atoi (path);
while (*path != 0 && !isdirsep (*path))
path++;
if (*path == 0)
{
if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
{
set_errno (EEXIST);
res = 0;
goto out;
}
else if (flags & O_WRONLY)
{
set_errno (EISDIR);
res = 0;
goto out;
}
else
{
flags |= O_DIROPEN;
goto success;
}
}
virt_tab_t *entry;
entry = virt_tab_search (path + 1, true, procsysvipc_tab, PROCSYSVIPC_LINK_COUNT);
if (!entry)
{
set_errno ((flags & O_CREAT) ? EROFS : ENOENT);
res = 0;
goto out;
}
if (flags & O_WRONLY)
{
set_errno (EROFS);
res = 0;
goto out;
}
fileid = entry - procsysvipc_tab;
if (!fill_filebuf ())
{
res = 0;
goto out;
}
if (flags & O_APPEND)
position = filesize;
else
position = 0;
success:
res = 1;
set_flags ((flags & ~O_TEXT) | O_BINARY);
set_open_status ();
out:
syscall_printf ("%d = fhandler_proc::open (%p, %d)", res, flags, mode);
return res;
}
bool
fhandler_procsysvipc::fill_filebuf ()
{
if (procsysvipc_tab[fileid].format_func)
{
filesize = procsysvipc_tab[fileid].format_func (NULL, filebuf);
return true;
}
return false;
}
static _off64_t
format_procsysvipc_msg (void *, char *&destbuf)
{
tmp_pathbuf tp;
char *buf = tp.c_get ();
char *bufptr = buf;
struct msginfo msginfo;
struct msqid_ds *xmsqids;
size_t xmsqids_len;
msgctl (0, IPC_INFO, (struct msqid_ds *) &msginfo);
xmsqids_len = sizeof (struct msqid_ds) * msginfo.msgmni;
xmsqids = (struct msqid_ds *) malloc (xmsqids_len);
msgctl (msginfo.msgmni, IPC_INFO, (struct msqid_ds *) xmsqids);
bufptr += __small_sprintf (bufptr,
" key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n");
for (int i = 0; i < msginfo.msgmni; i++) {
if (xmsqids[i].msg_qbytes != 0) {
bufptr += sprintf (bufptr,
"%10llu %10u %5o %11lu %10lu %5d %5d %5lu %5lu %5lu %5lu %10ld %10ld %10ld\n",
xmsqids[i].msg_perm.key,
IXSEQ_TO_IPCID(i, xmsqids[i].msg_perm),
xmsqids[i].msg_perm.mode,
xmsqids[i].msg_cbytes,
xmsqids[i].msg_qnum,
xmsqids[i].msg_lspid,
xmsqids[i].msg_lrpid,
xmsqids[i].msg_perm.uid,
xmsqids[i].msg_perm.gid,
xmsqids[i].msg_perm.cuid,
xmsqids[i].msg_perm.cgid,
xmsqids[i].msg_stime,
xmsqids[i].msg_rtime,
xmsqids[i].msg_ctime);
}
}
destbuf = (char *) crealloc_abort (destbuf, bufptr - buf);
memcpy (destbuf, buf, bufptr - buf);
return bufptr - buf;
}
static _off64_t
format_procsysvipc_sem (void *, char *&destbuf)
{
tmp_pathbuf tp;
char *buf = tp.c_get ();
char *bufptr = buf;
union semun semun;
struct seminfo seminfo;
struct semid_ds *xsemids;
size_t xsemids_len;
semun.buf = (struct semid_ds *) &seminfo;
semctl (0, 0, IPC_INFO, semun);
xsemids_len = sizeof (struct semid_ds) * seminfo.semmni;
xsemids = (struct semid_ds *) malloc (xsemids_len);
semun.buf = xsemids;
semctl (seminfo.semmni, 0, IPC_INFO, semun);
bufptr += __small_sprintf (bufptr,
" key semid perms nsems uid gid cuid cgid otime ctime\n");
for (int i = 0; i < seminfo.semmni; i++) {
if ((xsemids[i].sem_perm.mode & SEM_ALLOC) != 0) {
bufptr += sprintf (bufptr,
"%10llu %10u %5o %10d %5lu %5lu %5lu %5lu %10ld %10ld\n",
xsemids[i].sem_perm.key,
IXSEQ_TO_IPCID(i, xsemids[i].sem_perm),
xsemids[i].sem_perm.mode,
xsemids[i].sem_nsems,
xsemids[i].sem_perm.uid,
xsemids[i].sem_perm.gid,
xsemids[i].sem_perm.cuid,
xsemids[i].sem_perm.cgid,
xsemids[i].sem_otime,
xsemids[i].sem_ctime);
}
}
destbuf = (char *) crealloc_abort (destbuf, bufptr - buf);
memcpy (destbuf, buf, bufptr - buf);
return bufptr - buf;
}
static _off64_t
format_procsysvipc_shm (void *, char *&destbuf)
{
tmp_pathbuf tp;
char *buf = tp.c_get ();
char *bufptr = buf;
struct shminfo shminfo;
struct shmid_ds *xshmids;
size_t xshmids_len;
shmctl (0, IPC_INFO, (struct shmid_ds *) &shminfo);
xshmids_len = sizeof (struct shmid_ds) * shminfo.shmmni;
xshmids = (struct shmid_ds *) malloc (xshmids_len);
shmctl (shminfo.shmmni, IPC_INFO, (struct shmid_ds *) xshmids);
bufptr += __small_sprintf (bufptr,
" key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n");
for (int i = 0; i < shminfo.shmmni; i++) {
if (xshmids[i].shm_perm.mode & 0x0800) {
bufptr += sprintf (bufptr,
"%10llu %10u %5o %10u %5d %5d %6u %5lu %5lu %5lu %5lu %10ld %10ld %10ld\n",
xshmids[i].shm_perm.key,
IXSEQ_TO_IPCID(i, xshmids[i].shm_perm),
xshmids[i].shm_perm.mode,
xshmids[i].shm_segsz,
xshmids[i].shm_cpid,
xshmids[i].shm_lpid,
xshmids[i].shm_nattch,
xshmids[i].shm_perm.uid,
xshmids[i].shm_perm.gid,
xshmids[i].shm_perm.cuid,
xshmids[i].shm_perm.cgid,
xshmids[i].shm_atime,
xshmids[i].shm_dtime,
xshmids[i].shm_ctime);
}
}
destbuf = (char *) crealloc_abort (destbuf, bufptr - buf);
memcpy (destbuf, buf, bufptr - buf);
return bufptr - buf;
}