2011-04-02 03:48:19 +08:00
|
|
|
/* fhandler_procsysvipc.cc: fhandler for /proc/sysvipc virtual filesystem
|
|
|
|
|
|
|
|
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 "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>
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
static off_t format_procsysvipc_msg (void *, char *&);
|
|
|
|
static off_t format_procsysvipc_sem (void *, char *&);
|
|
|
|
static off_t format_procsysvipc_shm (void *, char *&);
|
2011-04-02 03:48:19 +08:00
|
|
|
|
|
|
|
static const virt_tab_t procsysvipc_tab[] =
|
|
|
|
{
|
2011-06-06 13:02:13 +08:00
|
|
|
{ _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 },
|
2011-06-13 04:15:26 +08:00
|
|
|
{ NULL, 0, FH_NADA, virt_none, NULL }
|
2011-04-02 03:48:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-05-20 22:49:02 +08:00
|
|
|
cygserver_init ();
|
2011-04-02 03:48:19 +08:00
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
if (entry->type == virt_file)
|
2011-06-06 13:02:13 +08:00
|
|
|
{
|
|
|
|
if (cygserver_running != CYGSERVER_OK)
|
|
|
|
return virt_none;
|
|
|
|
}
|
2011-04-02 03:48:19 +08:00
|
|
|
fileid = entry - procsysvipc_tab;
|
|
|
|
return entry->type;
|
|
|
|
}
|
|
|
|
return virt_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_procsysvipc::fhandler_procsysvipc ():
|
|
|
|
fhandler_proc ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-21 12:34:52 +08:00
|
|
|
int __reg2
|
2013-04-23 17:44:36 +08:00
|
|
|
fhandler_procsysvipc::fstat (struct stat *buf)
|
2011-04-02 03:48:19 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
{
|
2014-05-20 22:49:02 +08:00
|
|
|
cygserver_init ();
|
2011-04-02 03:48:19 +08:00
|
|
|
if (cygserver_running != CYGSERVER_OK)
|
|
|
|
goto out;
|
|
|
|
}
|
2014-10-09 21:24:37 +08:00
|
|
|
strcpy (de->d_name, procsysvipc_tab[dir->__d_position].name);
|
|
|
|
de->d_type = virt_ftype_to_dtype (procsysvipc_tab[dir->__d_position].type);
|
|
|
|
dir->__d_position++;
|
2011-04-02 03:48:19 +08:00
|
|
|
dir->__flags |= dirent_saw_dot | dirent_saw_dot_dot;
|
|
|
|
res = 0;
|
|
|
|
out:
|
2011-12-04 05:43:27 +08:00
|
|
|
syscall_printf ("%d = readdir(%p, %p) (%s)", res, dir, de, de->d_name);
|
2011-04-02 03:48:19 +08:00
|
|
|
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:
|
2013-04-23 17:44:36 +08:00
|
|
|
syscall_printf ("%d = fhandler_proc::open(%p, 0%o)", res, flags, mode);
|
2011-04-02 03:48:19 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-20 22:49:02 +08:00
|
|
|
#define MSG_HEADLINE " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n"
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
static off_t
|
2011-04-02 03:48:19 +08:00
|
|
|
format_procsysvipc_msg (void *, char *&destbuf)
|
|
|
|
{
|
2014-05-20 22:49:02 +08:00
|
|
|
char *buf;
|
2011-04-02 03:48:19 +08:00
|
|
|
struct msginfo msginfo;
|
|
|
|
struct msqid_ds *xmsqids;
|
|
|
|
|
|
|
|
msgctl (0, IPC_INFO, (struct msqid_ds *) &msginfo);
|
2014-05-20 22:49:02 +08:00
|
|
|
/* Don't use tmp_pathbuf. The required buffer sizes can be up to 128K! */
|
|
|
|
xmsqids = (struct msqid_ds *) malloc (sizeof (struct msqid_ds)
|
|
|
|
* msginfo.msgmni);
|
|
|
|
if (!xmsqids)
|
|
|
|
return 0;
|
|
|
|
/* buf size = sizeof headline + 128 bytes per msg queue entry. */
|
|
|
|
buf = (char *) malloc (sizeof (MSG_HEADLINE) + msginfo.msgmni * 128);
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
free (xmsqids);
|
|
|
|
return 0;
|
2011-04-02 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2014-05-20 22:49:02 +08:00
|
|
|
char *bufptr = stpcpy (buf, MSG_HEADLINE);
|
|
|
|
msgctl (msginfo.msgmni, IPC_INFO, (struct msqid_ds *) xmsqids);
|
|
|
|
for (int i = 0; i < msginfo.msgmni; i++)
|
|
|
|
{
|
|
|
|
if (xmsqids[i].msg_qbytes != 0)
|
|
|
|
{
|
|
|
|
bufptr += sprintf (bufptr,
|
|
|
|
"%10llu %10u %5o %11u %10u %5d %5d %5u %5u %5u %5u "
|
|
|
|
"%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,
|
|
|
|
(unsigned) xmsqids[i].msg_perm.uid,
|
|
|
|
(unsigned) xmsqids[i].msg_perm.gid,
|
|
|
|
(unsigned) xmsqids[i].msg_perm.cuid,
|
|
|
|
(unsigned) xmsqids[i].msg_perm.cgid,
|
|
|
|
xmsqids[i].msg_stime,
|
|
|
|
xmsqids[i].msg_rtime,
|
|
|
|
xmsqids[i].msg_ctime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
off_t size = bufptr - buf;
|
|
|
|
destbuf = (char *) crealloc_abort (destbuf, size);
|
|
|
|
memcpy (destbuf, buf, size);
|
|
|
|
free (buf);
|
|
|
|
free (xmsqids);
|
|
|
|
return size;
|
2011-04-02 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2014-05-20 22:49:02 +08:00
|
|
|
#undef MSG_HEADLINE
|
|
|
|
|
|
|
|
#define SEM_HEADLINE " key semid perms nsems uid gid cuid cgid otime ctime\n"
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
static off_t
|
2011-04-02 03:48:19 +08:00
|
|
|
format_procsysvipc_sem (void *, char *&destbuf)
|
|
|
|
{
|
2014-05-20 22:49:02 +08:00
|
|
|
char *buf;
|
2011-04-02 03:48:19 +08:00
|
|
|
union semun semun;
|
|
|
|
struct seminfo seminfo;
|
|
|
|
struct semid_ds *xsemids;
|
|
|
|
|
|
|
|
semun.buf = (struct semid_ds *) &seminfo;
|
|
|
|
semctl (0, 0, IPC_INFO, semun);
|
2014-05-20 22:49:02 +08:00
|
|
|
/* Don't use tmp_pathbuf. The required buffer sizes can be up to 96K! */
|
|
|
|
xsemids = (struct semid_ds *) malloc (sizeof (struct semid_ds)
|
|
|
|
* seminfo.semmni);
|
|
|
|
if (!xsemids)
|
|
|
|
return 0;
|
|
|
|
/* buf size = sizeof headline + 96 bytes per semaphore entry. */
|
|
|
|
buf = (char *) malloc (sizeof (SEM_HEADLINE) + seminfo.semmni * 96);
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
free (xsemids);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *bufptr = stpcpy (buf, SEM_HEADLINE);
|
2011-04-02 03:48:19 +08:00
|
|
|
semun.buf = xsemids;
|
|
|
|
semctl (seminfo.semmni, 0, IPC_INFO, semun);
|
2014-05-20 22:49:02 +08:00
|
|
|
for (int i = 0; i < seminfo.semmni; i++)
|
|
|
|
{
|
|
|
|
if ((xsemids[i].sem_perm.mode & SEM_ALLOC) != 0)
|
|
|
|
{
|
|
|
|
bufptr += sprintf (bufptr,
|
|
|
|
"%10llu %10u %5o %10d %5u %5u %5u %5u %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,
|
|
|
|
(unsigned) xsemids[i].sem_perm.uid,
|
|
|
|
(unsigned) xsemids[i].sem_perm.gid,
|
|
|
|
(unsigned) xsemids[i].sem_perm.cuid,
|
|
|
|
(unsigned) xsemids[i].sem_perm.cgid,
|
|
|
|
xsemids[i].sem_otime,
|
|
|
|
xsemids[i].sem_ctime);
|
|
|
|
}
|
2011-04-02 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2014-05-20 22:49:02 +08:00
|
|
|
off_t size = bufptr - buf;
|
|
|
|
destbuf = (char *) crealloc_abort (destbuf, size);
|
|
|
|
memcpy (destbuf, buf, size);
|
|
|
|
free (buf);
|
|
|
|
free (xsemids);
|
|
|
|
return size;
|
2011-04-02 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2014-05-20 22:49:02 +08:00
|
|
|
#undef SEM_HEADLINE
|
|
|
|
|
|
|
|
#define SHM_HEADLINE " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n"
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
static off_t
|
2011-04-02 03:48:19 +08:00
|
|
|
format_procsysvipc_shm (void *, char *&destbuf)
|
|
|
|
{
|
2014-05-20 22:49:02 +08:00
|
|
|
char *buf;
|
2011-04-02 03:48:19 +08:00
|
|
|
struct shminfo shminfo;
|
|
|
|
struct shmid_ds *xshmids;
|
|
|
|
|
|
|
|
shmctl (0, IPC_INFO, (struct shmid_ds *) &shminfo);
|
2014-05-20 22:49:02 +08:00
|
|
|
/* Don't use tmp_pathbuf. The required buffer sizes can be up to 120K! */
|
|
|
|
xshmids = (struct shmid_ds *) malloc (sizeof (struct shmid_ds)
|
|
|
|
* shminfo.shmmni);
|
|
|
|
if (!xshmids)
|
|
|
|
return 0;
|
|
|
|
/* buf size = sizeof headline + 120 bytes per shmem entry. */
|
|
|
|
buf = (char *) malloc (sizeof (SHM_HEADLINE) + shminfo.shmmni * 120);
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
free (xshmids);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-04-02 03:48:19 +08:00
|
|
|
|
2014-05-20 22:49:02 +08:00
|
|
|
char *bufptr = stpcpy (buf, SHM_HEADLINE);
|
|
|
|
shmctl (shminfo.shmmni, IPC_INFO, (struct shmid_ds *) xshmids);
|
|
|
|
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 %5u %5u %5u %5u "
|
|
|
|
"%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,
|
|
|
|
(unsigned) xshmids[i].shm_perm.uid,
|
|
|
|
(unsigned) xshmids[i].shm_perm.gid,
|
|
|
|
(unsigned) xshmids[i].shm_perm.cuid,
|
|
|
|
(unsigned) xshmids[i].shm_perm.cgid,
|
|
|
|
xshmids[i].shm_atime,
|
|
|
|
xshmids[i].shm_dtime,
|
|
|
|
xshmids[i].shm_ctime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
off_t size = bufptr - buf;
|
|
|
|
destbuf = (char *) crealloc_abort (destbuf, size);
|
|
|
|
memcpy (destbuf, buf, size);
|
|
|
|
free (buf);
|
|
|
|
free (xshmids);
|
|
|
|
return size;
|
2011-04-02 03:48:19 +08:00
|
|
|
}
|
2014-05-20 22:49:02 +08:00
|
|
|
|
|
|
|
#undef SHM_HEADLINE
|