2007-02-14 18:06:46 +08:00
|
|
|
/* posix_ipc.cc: POSIX IPC API for Cygwin.
|
|
|
|
|
|
|
|
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"
|
2010-10-07 04:20:01 +08:00
|
|
|
#include "shared_info.h"
|
2007-02-20 23:48:04 +08:00
|
|
|
#include "thread.h"
|
2007-02-14 18:06:46 +08:00
|
|
|
#include "path.h"
|
|
|
|
#include "cygtls.h"
|
2007-03-30 00:37:36 +08:00
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2007-02-14 18:06:46 +08:00
|
|
|
#include "sigproc.h"
|
2008-04-21 20:46:58 +08:00
|
|
|
#include "ntdll.h"
|
2021-05-25 22:49:04 +08:00
|
|
|
#include "tls_pbuf.h"
|
2021-05-21 18:01:56 +08:00
|
|
|
#include <io.h>
|
2007-02-14 18:06:46 +08:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <mqueue.h>
|
2007-02-20 23:48:04 +08:00
|
|
|
#include <semaphore.h>
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2021-05-21 21:18:38 +08:00
|
|
|
/* The prefix_len is the length of the path prefix including trailing "/"
|
2007-11-28 01:04:19 +08:00
|
|
|
(or "/sem." for semaphores) as well as the trailing NUL. */
|
2008-09-11 13:43:11 +08:00
|
|
|
static struct
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
|
|
|
const char *prefix;
|
2007-11-28 01:04:19 +08:00
|
|
|
const size_t prefix_len;
|
2007-02-14 18:06:46 +08:00
|
|
|
const char *description;
|
|
|
|
} ipc_names[] = {
|
2007-11-28 01:04:19 +08:00
|
|
|
{ "/dev/shm", 10, "POSIX shared memory object" },
|
|
|
|
{ "/dev/mqueue", 13, "POSIX message queue" },
|
|
|
|
{ "/dev/shm", 14, "POSIX semaphore" }
|
2007-02-14 18:06:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
enum ipc_type_t
|
|
|
|
{
|
|
|
|
shmem,
|
|
|
|
mqueue,
|
2007-02-20 23:48:04 +08:00
|
|
|
semaphore
|
2007-02-14 18:06:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
2007-11-28 01:04:19 +08:00
|
|
|
check_path (char *res_name, ipc_type_t type, const char *name, size_t len)
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2009-06-12 17:50:05 +08:00
|
|
|
/* Note that we require the existance of the appropriate /dev subdirectories
|
2007-02-14 18:06:46 +08:00
|
|
|
for POSIX IPC object support, similar to Linux (which supports the
|
|
|
|
directories, but doesn't require to mount them). We don't create
|
|
|
|
these directory here, that's the task of the installer. But we check
|
|
|
|
for existance and give ample warning. */
|
|
|
|
path_conv path (ipc_names[type].prefix, PC_SYM_NOFOLLOW);
|
|
|
|
if (path.error || !path.exists () || !path.isdir ())
|
|
|
|
{
|
|
|
|
small_printf (
|
|
|
|
"Warning: '%s' does not exists or is not a directory.\n\n"
|
|
|
|
"%ss require the existance of this directory.\n"
|
|
|
|
"Create the directory '%s' and set the permissions to 01777.\n"
|
|
|
|
"For instance on the command line: mkdir -m 01777 %s\n",
|
|
|
|
ipc_names[type].prefix, ipc_names[type].description,
|
|
|
|
ipc_names[type].prefix, ipc_names[type].prefix);
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-21 21:21:29 +08:00
|
|
|
/* Apart from handling backslash like slash, the naming rules are identical
|
2009-06-12 17:50:05 +08:00
|
|
|
to Linux, including the names and requirements for subdirectories, if
|
|
|
|
the name contains further slashes. */
|
2021-05-21 21:21:29 +08:00
|
|
|
/* Name must not be empty and has to start with a slash (or backslash) */
|
|
|
|
if (!name || !strchr ("/\\", name[0]))
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
|
|
|
debug_printf ("Invalid %s name '%s'", ipc_names[type].description, name);
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-21 21:21:29 +08:00
|
|
|
/* Name must not consist of just a single slash (or backslash) */
|
|
|
|
if (!name[1])
|
|
|
|
{
|
|
|
|
debug_printf ("Invalid %s name '%s'", ipc_names[type].description, name);
|
|
|
|
set_errno (ENOENT);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* Name must not contain slashes after the leading one */
|
|
|
|
if (strpbrk (name + 1, "/\\"))
|
|
|
|
{
|
|
|
|
debug_printf ("Invalid %s name '%s'", ipc_names[type].description, name);
|
|
|
|
set_errno (EACCES);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* Length must be less than or equal to NAME_MAX, or NAME_MAX - 4 in
|
|
|
|
case of semaphores, due to the leading "sem." prefix */
|
|
|
|
if (len > NAME_MAX - (type == semaphore ? strlen ("sem.") : 0))
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
|
|
|
debug_printf ("%s name '%s' too long", ipc_names[type].description, name);
|
|
|
|
set_errno (ENAMETOOLONG);
|
|
|
|
return false;
|
|
|
|
}
|
2007-02-20 23:48:04 +08:00
|
|
|
__small_sprintf (res_name, "%s/%s%s", ipc_names[type].prefix,
|
|
|
|
type == semaphore ? "sem." : "",
|
2021-05-21 21:21:29 +08:00
|
|
|
name + 1);
|
2007-02-14 18:06:46 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-02-20 23:48:04 +08:00
|
|
|
class ipc_flock
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
struct flock fl;
|
2007-02-20 23:48:04 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
ipc_flock () { memset (&fl, 0, sizeof fl); }
|
|
|
|
|
|
|
|
int lock (int fd, size_t size)
|
|
|
|
{
|
|
|
|
fl.l_type = F_WRLCK;
|
|
|
|
fl.l_whence = SEEK_SET;
|
|
|
|
fl.l_start = 0;
|
|
|
|
fl.l_len = size;
|
Cygwin: simplify some function names
Remove "32" or "64" from each of the following names: acl32,
aclcheck32, aclfrommode32, aclfrompbits32, aclfromtext32, aclsort32,
acltomode32, acltopbits32, acltotext32, facl32, fchown32, fcntl64,
fstat64, _fstat64, _fstat64_r, ftruncate64, getgid32, getgrent32,
getgrgid32, getgrnam32, getgroups32, getpwuid32, getpwuid_r32,
getuid32, getuid32, initgroups32, lseek64, lstat64, mknod32, mmap64,
setegid32, seteuid32, setgid32, setgroups32, setregid32, setreuid32,
setuid32, stat64, _stat64_r, truncate64.
Remove prototypes and macro definitions of these names.
Remove "#ifndef __INSIDE_CYGWIN__" from some headers so that the new
names will be available when compiling Cygwin.
Remove aliases that are no longer needed.
Include <unistd.h> in fhandler_clipboard.cc for the declarations of
geteuid and getegid.
2022-05-23 03:18:48 +08:00
|
|
|
return fcntl (fd, F_SETLKW, &fl);
|
2007-02-20 23:48:04 +08:00
|
|
|
}
|
|
|
|
int unlock (int fd)
|
|
|
|
{
|
|
|
|
if (!fl.l_len)
|
|
|
|
return 0;
|
|
|
|
fl.l_type = F_UNLCK;
|
Cygwin: simplify some function names
Remove "32" or "64" from each of the following names: acl32,
aclcheck32, aclfrommode32, aclfrompbits32, aclfromtext32, aclsort32,
acltomode32, acltopbits32, acltotext32, facl32, fchown32, fcntl64,
fstat64, _fstat64, _fstat64_r, ftruncate64, getgid32, getgrent32,
getgrgid32, getgrnam32, getgroups32, getpwuid32, getpwuid_r32,
getuid32, getuid32, initgroups32, lseek64, lstat64, mknod32, mmap64,
setegid32, seteuid32, setgid32, setgroups32, setregid32, setreuid32,
setuid32, stat64, _stat64_r, truncate64.
Remove prototypes and macro definitions of these names.
Remove "#ifndef __INSIDE_CYGWIN__" from some headers so that the new
names will be available when compiling Cygwin.
Remove aliases that are no longer needed.
Include <unistd.h> in fhandler_clipboard.cc for the declarations of
geteuid and getegid.
2022-05-23 03:18:48 +08:00
|
|
|
return fcntl (fd, F_SETLKW, &fl);
|
2007-02-20 23:48:04 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-02-14 18:06:46 +08:00
|
|
|
/* POSIX shared memory object implementation. */
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
shm_open (const char *name, int oflag, mode_t mode)
|
|
|
|
{
|
2007-11-28 01:04:19 +08:00
|
|
|
size_t len = strlen (name);
|
|
|
|
char shmname[ipc_names[shmem].prefix_len + len];
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2007-11-28 01:04:19 +08:00
|
|
|
if (!check_path (shmname, shmem, name, len))
|
2007-02-14 18:06:46 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Check for valid flags. */
|
|
|
|
if (((oflag & O_ACCMODE) != O_RDONLY && (oflag & O_ACCMODE) != O_RDWR)
|
|
|
|
|| (oflag & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)))
|
|
|
|
{
|
|
|
|
debug_printf ("Invalid oflag 0%o", oflag);
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-01-15 02:46:02 +08:00
|
|
|
return open (shmname, oflag | O_CLOEXEC, mode & 0777);
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
shm_unlink (const char *name)
|
|
|
|
{
|
2007-11-28 01:04:19 +08:00
|
|
|
size_t len = strlen (name);
|
|
|
|
char shmname[ipc_names[shmem].prefix_len + len];
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2007-11-28 01:04:19 +08:00
|
|
|
if (!check_path (shmname, shmem, name, len))
|
2007-02-14 18:06:46 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return unlink (shmname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The POSIX message queue implementation is based on W. Richard STEVENS
|
|
|
|
implementation, just tweaked for Cygwin. The main change is
|
|
|
|
the usage of Windows mutexes and events instead of using the pthread
|
|
|
|
synchronization objects. The pathname is massaged so that the
|
|
|
|
files are created under /dev/mqueue. mq_timedsend and mq_timedreceive
|
|
|
|
are implemented additionally. */
|
|
|
|
|
|
|
|
extern "C" mqd_t
|
|
|
|
mq_open (const char *name, int oflag, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2021-05-25 22:49:04 +08:00
|
|
|
mode_t mode = 0;
|
2021-05-21 21:25:37 +08:00
|
|
|
fhandler_mqueue *fh = NULL;
|
2021-05-25 22:49:04 +08:00
|
|
|
struct mq_attr *attr = NULL;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2007-11-28 01:04:19 +08:00
|
|
|
size_t len = strlen (name);
|
|
|
|
char mqname[ipc_names[mqueue].prefix_len + len];
|
|
|
|
|
|
|
|
if (!check_path (mqname, mqueue, name, len))
|
2007-02-14 18:06:46 +08:00
|
|
|
return (mqd_t) -1;
|
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
|
|
|
{
|
|
|
|
if (oflag & O_CREAT)
|
|
|
|
{
|
|
|
|
va_start (ap, oflag); /* init ap to final named argument */
|
|
|
|
mode = va_arg (ap, mode_t) & ~S_IXUSR;
|
|
|
|
attr = va_arg (ap, struct mq_attr *);
|
|
|
|
va_end (ap);
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
|
|
|
|
2021-05-21 18:01:56 +08:00
|
|
|
/* Create file descriptor for mqueue */
|
2021-05-25 22:49:04 +08:00
|
|
|
cygheap_fdnew fd;
|
2021-05-21 18:01:56 +08:00
|
|
|
|
2021-05-25 22:49:04 +08:00
|
|
|
if (fd < 0)
|
2021-05-21 18:01:56 +08:00
|
|
|
__leave;
|
2021-05-25 22:49:04 +08:00
|
|
|
fh = (fhandler_mqueue *) build_fh_name (mqname,
|
|
|
|
PC_OPEN | PC_POSIX
|
|
|
|
| PC_SYM_NOFOLLOW | PC_NULLEMPTY,
|
|
|
|
NULL);
|
2021-05-21 18:01:56 +08:00
|
|
|
if (!fh)
|
|
|
|
__leave;
|
|
|
|
|
2021-05-25 22:49:04 +08:00
|
|
|
if (fh->mq_open (oflag, mode, attr))
|
|
|
|
{
|
|
|
|
fd = fh;
|
|
|
|
return (mqd_t) fd;
|
|
|
|
}
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
__except (EFAULT) {}
|
|
|
|
__endtry
|
2021-05-21 21:26:51 +08:00
|
|
|
if (fh)
|
2021-05-25 22:49:04 +08:00
|
|
|
delete fh;
|
2007-02-14 18:06:46 +08:00
|
|
|
return (mqd_t) -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
mq_getattr (mqd_t mqd, struct mq_attr *mqstat)
|
|
|
|
{
|
2021-05-26 02:15:16 +08:00
|
|
|
int ret = -1;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2021-05-26 02:15:16 +08:00
|
|
|
cygheap_fdget fd ((int) mqd, true);
|
|
|
|
fhandler_mqueue *fh = fd->is_mqueue ();
|
|
|
|
if (!fh)
|
|
|
|
set_errno (EBADF);
|
|
|
|
else
|
|
|
|
ret = fh->mq_getattr (mqstat);
|
|
|
|
return ret;
|
2007-07-08 00:43:03 +08:00
|
|
|
}
|
2007-02-14 18:06:46 +08:00
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
mq_setattr (mqd_t mqd, const struct mq_attr *mqstat, struct mq_attr *omqstat)
|
|
|
|
{
|
2021-05-26 02:15:16 +08:00
|
|
|
int ret = -1;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2021-05-26 02:15:16 +08:00
|
|
|
cygheap_fdget fd ((int) mqd, true);
|
|
|
|
fhandler_mqueue *fh = fd->is_mqueue ();
|
|
|
|
if (!fh)
|
|
|
|
set_errno (EBADF);
|
|
|
|
else
|
|
|
|
ret = fh->mq_setattr (mqstat, omqstat);
|
|
|
|
return ret;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
mq_notify (mqd_t mqd, const struct sigevent *notification)
|
|
|
|
{
|
2021-05-26 02:15:16 +08:00
|
|
|
int ret = -1;
|
2007-07-08 00:43:03 +08:00
|
|
|
|
2021-05-26 02:15:16 +08:00
|
|
|
cygheap_fdget fd ((int) mqd, true);
|
|
|
|
fhandler_mqueue *fh = fd->is_mqueue ();
|
|
|
|
if (!fh)
|
|
|
|
set_errno (EBADF);
|
|
|
|
else
|
|
|
|
ret = fh->mq_notify (notification);
|
|
|
|
return ret;
|
2007-07-08 00:43:03 +08:00
|
|
|
}
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2021-05-26 02:15:16 +08:00
|
|
|
extern "C" int
|
|
|
|
mq_timedsend (mqd_t mqd, const char *ptr, size_t len, unsigned int prio,
|
|
|
|
const struct timespec *abstime)
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
int ret = -1;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2021-05-26 02:15:16 +08:00
|
|
|
cygheap_fdget fd ((int) mqd, true);
|
|
|
|
fhandler_mqueue *fh = fd->is_mqueue ();
|
|
|
|
if (!fh)
|
|
|
|
set_errno (EBADF);
|
|
|
|
else
|
|
|
|
ret = fh->mq_timedsend (ptr, len, prio, abstime);
|
2014-08-22 17:21:33 +08:00
|
|
|
return ret;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
mq_send (mqd_t mqd, const char *ptr, size_t len, unsigned int prio)
|
|
|
|
{
|
2021-05-26 02:15:16 +08:00
|
|
|
return mq_timedsend (mqd, ptr, len, prio, NULL);
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
|
|
|
|
2021-05-26 02:15:16 +08:00
|
|
|
extern "C" ssize_t
|
|
|
|
mq_timedreceive (mqd_t mqd, char *ptr, size_t maxlen, unsigned int *priop,
|
2007-02-14 18:06:46 +08:00
|
|
|
const struct timespec *abstime)
|
|
|
|
{
|
2021-05-26 02:15:16 +08:00
|
|
|
int ret = -1;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2021-05-26 02:15:16 +08:00
|
|
|
cygheap_fdget fd ((int) mqd, true);
|
|
|
|
fhandler_mqueue *fh = fd->is_mqueue ();
|
|
|
|
if (!fh)
|
|
|
|
set_errno (EBADF);
|
|
|
|
else
|
|
|
|
ret = fh->mq_timedrecv (ptr, maxlen, priop, abstime);
|
|
|
|
return ret;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" ssize_t
|
|
|
|
mq_receive (mqd_t mqd, char *ptr, size_t maxlen, unsigned int *priop)
|
|
|
|
{
|
2021-05-26 02:15:16 +08:00
|
|
|
return mq_timedreceive (mqd, ptr, maxlen, priop, NULL);
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
mq_close (mqd_t mqd)
|
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2021-05-21 18:01:56 +08:00
|
|
|
cygheap_fdget fd ((int) mqd, true);
|
2021-05-25 22:36:09 +08:00
|
|
|
if (!fd->is_mqueue ())
|
2014-08-22 17:21:33 +08:00
|
|
|
{
|
|
|
|
set_errno (EBADF);
|
|
|
|
__leave;
|
|
|
|
}
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
if (mq_notify (mqd, NULL)) /* unregister calling process */
|
|
|
|
__leave;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2021-05-21 18:01:56 +08:00
|
|
|
fd->isclosed (true);
|
|
|
|
fd->close ();
|
|
|
|
fd.release ();
|
2014-08-22 17:21:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
__except (EBADF) {}
|
|
|
|
__endtry
|
|
|
|
return -1;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
mq_unlink (const char *name)
|
|
|
|
{
|
2007-11-28 01:04:19 +08:00
|
|
|
size_t len = strlen (name);
|
|
|
|
char mqname[ipc_names[mqueue].prefix_len + len];
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2007-11-28 01:04:19 +08:00
|
|
|
if (!check_path (mqname, mqueue, name, len))
|
2007-02-14 18:06:46 +08:00
|
|
|
return -1;
|
|
|
|
if (unlink (mqname) == -1)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-20 23:48:04 +08:00
|
|
|
/* POSIX named semaphore implementation. Loosely based on Richard W. STEPHENS
|
|
|
|
implementation as far as sem_open is concerned, but under the hood using
|
|
|
|
the already existing semaphore class in thread.cc. Using a file backed
|
|
|
|
solution allows to implement kernel persistent named semaphores. */
|
|
|
|
|
2021-05-25 22:49:04 +08:00
|
|
|
#define MAX_TRIES 10 /* for waiting for initialization */
|
|
|
|
|
2007-02-20 23:48:04 +08:00
|
|
|
struct sem_finfo
|
|
|
|
{
|
|
|
|
unsigned int value;
|
|
|
|
unsigned long long hash;
|
|
|
|
LUID luid;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" sem_t *
|
|
|
|
sem_open (const char *name, int oflag, ...)
|
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
int i, fd = -1, created = 0;
|
2007-02-20 23:48:04 +08:00
|
|
|
va_list ap;
|
|
|
|
mode_t mode = 0;
|
|
|
|
unsigned int value = 0;
|
2013-04-23 17:44:36 +08:00
|
|
|
struct stat statbuff;
|
2007-02-20 23:48:04 +08:00
|
|
|
sem_t *sem = SEM_FAILED;
|
|
|
|
sem_finfo sf;
|
|
|
|
bool wasopen = false;
|
|
|
|
ipc_flock file;
|
|
|
|
|
2007-11-28 01:04:19 +08:00
|
|
|
size_t len = strlen (name);
|
|
|
|
char semname[ipc_names[semaphore].prefix_len + len];
|
|
|
|
|
|
|
|
if (!check_path (semname, semaphore, name, len))
|
2007-02-20 23:48:04 +08:00
|
|
|
return SEM_FAILED;
|
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2007-02-20 23:48:04 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
oflag &= (O_CREAT | O_EXCL);
|
|
|
|
|
|
|
|
again:
|
|
|
|
if (oflag & O_CREAT)
|
2007-07-08 00:43:03 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
va_start (ap, oflag); /* init ap to final named argument */
|
|
|
|
mode = va_arg (ap, mode_t) & ~S_IXUSR;
|
|
|
|
value = va_arg (ap, unsigned int);
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
/* Open and specify O_EXCL and user-execute */
|
|
|
|
fd = open (semname, oflag | O_EXCL | O_RDWR | O_CLOEXEC,
|
|
|
|
mode | S_IXUSR);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
if (errno == EEXIST && (oflag & O_EXCL) == 0)
|
|
|
|
goto exists; /* already exists, OK */
|
|
|
|
return SEM_FAILED;
|
|
|
|
}
|
|
|
|
created = 1;
|
|
|
|
/* First one to create the file initializes it. */
|
|
|
|
NtAllocateLocallyUniqueId (&sf.luid);
|
|
|
|
sf.value = value;
|
|
|
|
sf.hash = hash_path_name (0, semname);
|
|
|
|
if (write (fd, &sf, sizeof sf) != sizeof sf)
|
|
|
|
__leave;
|
|
|
|
sem = semaphore::open (sf.hash, sf.luid, fd, oflag, mode, value,
|
|
|
|
wasopen);
|
|
|
|
if (sem == SEM_FAILED)
|
|
|
|
__leave;
|
|
|
|
/* Initialization complete, turn off user-execute bit */
|
|
|
|
if (fchmod (fd, mode) == -1)
|
|
|
|
__leave;
|
|
|
|
/* Don't close (fd); */
|
|
|
|
return sem;
|
2007-02-20 23:48:04 +08:00
|
|
|
}
|
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
exists:
|
|
|
|
/* Open the file and fetch the semaphore name. */
|
|
|
|
if ((fd = open (semname, O_RDWR | O_CLOEXEC)) < 0)
|
2007-02-20 23:48:04 +08:00
|
|
|
{
|
|
|
|
if (errno == ENOENT && (oflag & O_CREAT))
|
2014-08-22 17:21:33 +08:00
|
|
|
goto again;
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
/* Make certain initialization is complete */
|
|
|
|
for (i = 0; i < MAX_TRIES; i++)
|
|
|
|
{
|
Cygwin: simplify some function names
Remove "32" or "64" from each of the following names: acl32,
aclcheck32, aclfrommode32, aclfrompbits32, aclfromtext32, aclsort32,
acltomode32, acltopbits32, acltotext32, facl32, fchown32, fcntl64,
fstat64, _fstat64, _fstat64_r, ftruncate64, getgid32, getgrent32,
getgrgid32, getgrnam32, getgroups32, getpwuid32, getpwuid_r32,
getuid32, getuid32, initgroups32, lseek64, lstat64, mknod32, mmap64,
setegid32, seteuid32, setgid32, setgroups32, setregid32, setreuid32,
setuid32, stat64, _stat64_r, truncate64.
Remove prototypes and macro definitions of these names.
Remove "#ifndef __INSIDE_CYGWIN__" from some headers so that the new
names will be available when compiling Cygwin.
Remove aliases that are no longer needed.
Include <unistd.h> in fhandler_clipboard.cc for the declarations of
geteuid and getegid.
2022-05-23 03:18:48 +08:00
|
|
|
if (stat (semname, &statbuff) == -1)
|
2007-02-20 23:48:04 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (errno == ENOENT && (oflag & O_CREAT))
|
|
|
|
{
|
|
|
|
close (fd);
|
|
|
|
fd = -1;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
__leave;
|
2007-02-20 23:48:04 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
if ((statbuff.st_mode & S_IXUSR) == 0)
|
|
|
|
break;
|
|
|
|
sleep (1);
|
2007-02-20 23:48:04 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
if (i == MAX_TRIES)
|
|
|
|
{
|
|
|
|
set_errno (ETIMEDOUT);
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
if (file.lock (fd, sizeof sf))
|
|
|
|
__leave;
|
|
|
|
if (read (fd, &sf, sizeof sf) != sizeof sf)
|
|
|
|
__leave;
|
|
|
|
sem = semaphore::open (sf.hash, sf.luid, fd, oflag, mode, sf.value,
|
|
|
|
wasopen);
|
|
|
|
file.unlock (fd);
|
|
|
|
if (sem == SEM_FAILED)
|
|
|
|
__leave;
|
|
|
|
/* If wasopen is set, the semaphore was already opened and we already have
|
|
|
|
an open file descriptor pointing to the file. This means, we have to
|
|
|
|
close the file descriptor created in this call. It won't be stored
|
|
|
|
anywhere anyway. */
|
|
|
|
if (wasopen)
|
|
|
|
close (fd);
|
|
|
|
return sem;
|
2007-02-20 23:48:04 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
__except (EFAULT) {}
|
|
|
|
__endtry
|
2007-02-20 23:48:04 +08:00
|
|
|
/* Don't let following function calls change errno */
|
|
|
|
save_errno save;
|
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
if (fd >= 0)
|
|
|
|
file.unlock (fd);
|
2007-02-20 23:48:04 +08:00
|
|
|
if (created)
|
|
|
|
unlink (semname);
|
|
|
|
if (sem != SEM_FAILED)
|
|
|
|
semaphore::close (sem);
|
2009-09-24 17:25:35 +08:00
|
|
|
if (fd >= 0)
|
|
|
|
close (fd);
|
2007-02-20 23:48:04 +08:00
|
|
|
return SEM_FAILED;
|
|
|
|
}
|
|
|
|
|
Cygwin: simplify some function names
Remove "32" or "64" from each of the following names: acl32,
aclcheck32, aclfrommode32, aclfrompbits32, aclfromtext32, aclsort32,
acltomode32, acltopbits32, acltotext32, facl32, fchown32, fcntl64,
fstat64, _fstat64, _fstat64_r, ftruncate64, getgid32, getgrent32,
getgrgid32, getgrnam32, getgroups32, getpwuid32, getpwuid_r32,
getuid32, getuid32, initgroups32, lseek64, lstat64, mknod32, mmap64,
setegid32, seteuid32, setgid32, setgroups32, setregid32, setreuid32,
setuid32, stat64, _stat64_r, truncate64.
Remove prototypes and macro definitions of these names.
Remove "#ifndef __INSIDE_CYGWIN__" from some headers so that the new
names will be available when compiling Cygwin.
Remove aliases that are no longer needed.
Include <unistd.h> in fhandler_clipboard.cc for the declarations of
geteuid and getegid.
2022-05-23 03:18:48 +08:00
|
|
|
extern "C" off_t lseek (int, off_t, int);
|
2021-05-25 22:49:04 +08:00
|
|
|
|
2007-02-20 23:48:04 +08:00
|
|
|
int
|
|
|
|
_sem_close (sem_t *sem, bool do_close)
|
|
|
|
{
|
|
|
|
sem_finfo sf;
|
|
|
|
int fd, ret = -1;
|
|
|
|
ipc_flock file;
|
|
|
|
|
|
|
|
if (semaphore::getinternal (sem, &fd, &sf.hash, &sf.luid, &sf.value) == -1)
|
|
|
|
return -1;
|
|
|
|
if (!file.lock (fd, sizeof sf)
|
Cygwin: simplify some function names
Remove "32" or "64" from each of the following names: acl32,
aclcheck32, aclfrommode32, aclfrompbits32, aclfromtext32, aclsort32,
acltomode32, acltopbits32, acltotext32, facl32, fchown32, fcntl64,
fstat64, _fstat64, _fstat64_r, ftruncate64, getgid32, getgrent32,
getgrgid32, getgrnam32, getgroups32, getpwuid32, getpwuid_r32,
getuid32, getuid32, initgroups32, lseek64, lstat64, mknod32, mmap64,
setegid32, seteuid32, setgid32, setgroups32, setregid32, setreuid32,
setuid32, stat64, _stat64_r, truncate64.
Remove prototypes and macro definitions of these names.
Remove "#ifndef __INSIDE_CYGWIN__" from some headers so that the new
names will be available when compiling Cygwin.
Remove aliases that are no longer needed.
Include <unistd.h> in fhandler_clipboard.cc for the declarations of
geteuid and getegid.
2022-05-23 03:18:48 +08:00
|
|
|
&& lseek (fd, 0LL, SEEK_SET) != (off_t) -1
|
2007-02-20 23:48:04 +08:00
|
|
|
&& write (fd, &sf, sizeof sf) == sizeof sf)
|
|
|
|
ret = do_close ? semaphore::close (sem) : 0;
|
|
|
|
|
|
|
|
/* Don't let following function calls change errno */
|
|
|
|
save_errno save;
|
|
|
|
file.unlock (fd);
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
sem_close (sem_t *sem)
|
|
|
|
{
|
|
|
|
return _sem_close (sem, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
sem_unlink (const char *name)
|
|
|
|
{
|
2007-11-28 01:04:19 +08:00
|
|
|
size_t len = strlen (name);
|
|
|
|
char semname[ipc_names[semaphore].prefix_len + len];
|
2007-02-20 23:48:04 +08:00
|
|
|
|
2007-11-28 01:04:19 +08:00
|
|
|
if (!check_path (semname, semaphore, name, len))
|
2007-02-20 23:48:04 +08:00
|
|
|
return -1;
|
|
|
|
if (unlink (semname) == -1)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2022-08-11 00:11:57 +08:00
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
sem_init (sem_t * sem, int pshared, unsigned int value)
|
|
|
|
{
|
|
|
|
return semaphore::init (sem, pshared, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
sem_destroy (sem_t * sem)
|
|
|
|
{
|
|
|
|
return semaphore::destroy (sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
sem_wait (sem_t * sem)
|
|
|
|
{
|
|
|
|
return semaphore::wait (sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
sem_trywait (sem_t * sem)
|
|
|
|
{
|
|
|
|
return semaphore::trywait (sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
sem_clockwait (sem_t * sem, clockid_t clock_id, const struct timespec *abstime)
|
|
|
|
{
|
|
|
|
return semaphore::clockwait (sem, clock_id, abstime);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
sem_timedwait (sem_t * sem, const struct timespec *abstime)
|
|
|
|
{
|
|
|
|
return semaphore::clockwait (sem, CLOCK_REALTIME, abstime);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
sem_post (sem_t *sem)
|
|
|
|
{
|
|
|
|
return semaphore::post (sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
sem_getvalue (sem_t * sem, int *sval)
|
|
|
|
{
|
|
|
|
return semaphore::getvalue (sem, sval);
|
|
|
|
}
|