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"
|
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
|
|
|
|
2007-11-28 01:04:19 +08:00
|
|
|
/* The prefix_len is the length of the path prefix ncluding trailing "/"
|
|
|
|
(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;
|
|
|
|
}
|
2009-06-12 17:50:05 +08:00
|
|
|
/* Name must not be empty, or just be a single slash, or start with more
|
|
|
|
than one slash. Same for backslash.
|
|
|
|
Apart from handling backslash like slash, the naming rules are identical
|
|
|
|
to Linux, including the names and requirements for subdirectories, if
|
|
|
|
the name contains further slashes. */
|
|
|
|
if (!name || (strchr ("/\\", name[0])
|
|
|
|
&& (!name[1] || strchr ("/\\", name[1]))))
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
|
|
|
debug_printf ("Invalid %s name '%s'", ipc_names[type].description, name);
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return false;
|
|
|
|
}
|
2009-06-12 17:50:05 +08:00
|
|
|
/* Skip leading (back-)slash. */
|
|
|
|
if (strchr ("/\\", name[0]))
|
|
|
|
++name;
|
2007-11-28 01:04:19 +08:00
|
|
|
if (len > PATH_MAX - ipc_names[type].prefix_len)
|
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." : "",
|
2009-06-12 17:50:05 +08:00
|
|
|
name);
|
2007-02-14 18:06:46 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ipc_mutex_init (HANDLE *pmtx, const char *name)
|
|
|
|
{
|
2010-10-07 04:20:01 +08:00
|
|
|
WCHAR buf[MAX_PATH];
|
|
|
|
UNICODE_STRING uname;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
__small_swprintf (buf, L"mqueue/mtx_%s", name);
|
|
|
|
RtlInitUnicodeString (&uname, buf);
|
|
|
|
InitializeObjectAttributes (&attr, &uname,
|
|
|
|
OBJ_INHERIT | OBJ_OPENIF | OBJ_CASE_INSENSITIVE,
|
|
|
|
get_shared_parent_dir (),
|
|
|
|
everyone_sd (CYG_MUTANT_ACCESS));
|
|
|
|
status = NtCreateMutant (pmtx, CYG_MUTANT_ACCESS, &attr, FALSE);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("NtCreateMutant: %y", status);
|
2010-10-07 04:20:01 +08:00
|
|
|
return geterrno_from_win_error (RtlNtStatusToDosError (status));
|
|
|
|
}
|
|
|
|
return 0;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-02-27 01:00:11 +08:00
|
|
|
ipc_mutex_lock (HANDLE mtx, bool eintr)
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2015-02-27 01:00:11 +08:00
|
|
|
switch (cygwait (mtx, cw_infinite, cw_cancel | cw_cancel_self
|
|
|
|
| (eintr ? cw_sig_eintr : cw_sig_restart)))
|
2007-07-08 00:43:03 +08:00
|
|
|
{
|
2007-02-14 18:06:46 +08:00
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
case WAIT_ABANDONED_0:
|
|
|
|
return 0;
|
2012-07-22 06:58:20 +08:00
|
|
|
case WAIT_SIGNALED:
|
2007-02-14 18:06:46 +08:00
|
|
|
set_errno (EINTR);
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
break;
|
2007-07-08 00:43:03 +08:00
|
|
|
}
|
2007-02-14 18:06:46 +08:00
|
|
|
return geterrno_from_win_error ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
ipc_mutex_unlock (HANDLE mtx)
|
|
|
|
{
|
|
|
|
return ReleaseMutex (mtx) ? 0 : geterrno_from_win_error ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
ipc_mutex_close (HANDLE mtx)
|
|
|
|
{
|
|
|
|
return CloseHandle (mtx) ? 0 : geterrno_from_win_error ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-10-07 04:20:01 +08:00
|
|
|
ipc_cond_init (HANDLE *pevt, const char *name, char sr)
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2010-10-07 04:20:01 +08:00
|
|
|
WCHAR buf[MAX_PATH];
|
|
|
|
UNICODE_STRING uname;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
__small_swprintf (buf, L"mqueue/evt_%s%c", name, sr);
|
|
|
|
RtlInitUnicodeString (&uname, buf);
|
|
|
|
InitializeObjectAttributes (&attr, &uname,
|
|
|
|
OBJ_INHERIT | OBJ_OPENIF | OBJ_CASE_INSENSITIVE,
|
|
|
|
get_shared_parent_dir (),
|
|
|
|
everyone_sd (CYG_EVENT_ACCESS));
|
|
|
|
status = NtCreateEvent (pevt, CYG_EVENT_ACCESS, &attr,
|
|
|
|
NotificationEvent, FALSE);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("NtCreateEvent: %y", status);
|
2010-10-07 04:20:01 +08:00
|
|
|
return geterrno_from_win_error (RtlNtStatusToDosError (status));
|
|
|
|
}
|
|
|
|
return 0;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ipc_cond_timedwait (HANDLE evt, HANDLE mtx, const struct timespec *abstime)
|
|
|
|
{
|
2012-07-22 06:58:20 +08:00
|
|
|
HANDLE w4[4] = { evt, };
|
2011-04-28 20:13:41 +08:00
|
|
|
DWORD cnt = 2;
|
2011-04-28 22:44:24 +08:00
|
|
|
DWORD timer_idx = 0;
|
2011-04-28 20:13:41 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
2015-11-27 21:39:11 +08:00
|
|
|
wait_signal_arrived here (w4[1]);
|
2011-05-02 01:42:41 +08:00
|
|
|
if ((w4[cnt] = pthread::get_cancel_event ()) != NULL)
|
|
|
|
++cnt;
|
2011-04-28 20:13:41 +08:00
|
|
|
if (abstime)
|
|
|
|
{
|
|
|
|
if (abstime->tv_sec < 0
|
|
|
|
|| abstime->tv_nsec < 0
|
2018-02-07 20:07:44 +08:00
|
|
|
|| abstime->tv_nsec >= NSPERSEC)
|
2011-04-28 20:13:41 +08:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/* If a timeout is set, we create a waitable timer to wait for.
|
|
|
|
This is the easiest way to handle the absolute timeout value, given
|
|
|
|
that NtSetTimer also takes absolute times and given the double
|
|
|
|
dependency on evt *and* mtx, which requires to call WFMO twice. */
|
|
|
|
NTSTATUS status;
|
|
|
|
LARGE_INTEGER duetime;
|
|
|
|
|
2011-04-28 22:44:24 +08:00
|
|
|
timer_idx = cnt++;
|
|
|
|
status = NtCreateTimer (&w4[timer_idx], TIMER_ALL_ACCESS, NULL,
|
2011-04-28 20:13:41 +08:00
|
|
|
NotificationTimer);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
return geterrno_from_nt_status (status);
|
2013-06-14 17:09:41 +08:00
|
|
|
timespec_to_filetime (abstime, &duetime);
|
2011-04-28 22:44:24 +08:00
|
|
|
status = NtSetTimer (w4[timer_idx], &duetime, NULL, NULL, FALSE, 0, NULL);
|
2011-04-28 20:13:41 +08:00
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
2011-04-28 22:44:24 +08:00
|
|
|
NtClose (w4[timer_idx]);
|
2011-04-28 20:13:41 +08:00
|
|
|
return geterrno_from_nt_status (status);
|
|
|
|
}
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
2010-10-07 04:20:01 +08:00
|
|
|
ResetEvent (evt);
|
2011-04-28 20:13:41 +08:00
|
|
|
if ((ret = ipc_mutex_unlock (mtx)) != 0)
|
|
|
|
return ret;
|
|
|
|
/* Everything's set up, so now wait for the event to be signalled. */
|
|
|
|
restart1:
|
|
|
|
switch (WaitForMultipleObjects (cnt, w4, FALSE, INFINITE))
|
2007-07-08 00:43:03 +08:00
|
|
|
{
|
2007-02-14 18:06:46 +08:00
|
|
|
case WAIT_OBJECT_0:
|
2011-04-28 20:13:41 +08:00
|
|
|
break;
|
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
if (_my_tls.call_signal_handler ())
|
|
|
|
goto restart1;
|
|
|
|
ret = EINTR;
|
|
|
|
break;
|
|
|
|
case WAIT_OBJECT_0 + 2:
|
2011-04-28 22:44:24 +08:00
|
|
|
if (timer_idx != 2)
|
2011-06-06 13:02:13 +08:00
|
|
|
pthread::static_cancel_self ();
|
2011-04-28 22:44:24 +08:00
|
|
|
/*FALLTHRU*/
|
|
|
|
case WAIT_OBJECT_0 + 3:
|
2011-04-28 20:13:41 +08:00
|
|
|
ret = ETIMEDOUT;
|
|
|
|
break;
|
2007-02-14 18:06:46 +08:00
|
|
|
default:
|
2011-04-28 20:13:41 +08:00
|
|
|
ret = geterrno_from_win_error ();
|
2007-02-14 18:06:46 +08:00
|
|
|
break;
|
2007-07-08 00:43:03 +08:00
|
|
|
}
|
2011-04-28 20:13:41 +08:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
/* At this point we need to lock the mutex. The wait is practically
|
|
|
|
the same as before, just that we now wait on the mutex instead of the
|
|
|
|
event. */
|
|
|
|
restart2:
|
|
|
|
w4[0] = mtx;
|
|
|
|
switch (WaitForMultipleObjects (cnt, w4, FALSE, INFINITE))
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
case WAIT_ABANDONED_0:
|
|
|
|
break;
|
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
if (_my_tls.call_signal_handler ())
|
|
|
|
goto restart2;
|
|
|
|
ret = EINTR;
|
|
|
|
break;
|
|
|
|
case WAIT_OBJECT_0 + 2:
|
2011-04-28 22:44:24 +08:00
|
|
|
if (timer_idx != 2)
|
|
|
|
pthread_testcancel ();
|
|
|
|
/*FALLTHRU*/
|
|
|
|
case WAIT_OBJECT_0 + 3:
|
2011-04-28 20:13:41 +08:00
|
|
|
ret = ETIMEDOUT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = geterrno_from_win_error ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-04-28 22:44:24 +08:00
|
|
|
if (timer_idx)
|
2011-04-28 20:13:41 +08:00
|
|
|
{
|
|
|
|
if (ret != ETIMEDOUT)
|
2011-04-28 22:44:24 +08:00
|
|
|
NtCancelTimer (w4[timer_idx], NULL);
|
|
|
|
NtClose (w4[timer_idx]);
|
2011-04-28 20:13:41 +08:00
|
|
|
}
|
|
|
|
return ret;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
|
|
|
|
2011-04-28 20:13:41 +08:00
|
|
|
static inline void
|
2007-02-14 18:06:46 +08:00
|
|
|
ipc_cond_signal (HANDLE evt)
|
|
|
|
{
|
2011-04-28 20:13:41 +08:00
|
|
|
SetEvent (evt);
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
|
|
|
|
2011-04-28 20:13:41 +08:00
|
|
|
static inline void
|
2007-02-14 18:06:46 +08:00
|
|
|
ipc_cond_close (HANDLE evt)
|
|
|
|
{
|
2011-04-28 20:13:41 +08:00
|
|
|
CloseHandle (evt);
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
|
|
|
|
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;
|
2008-04-01 18:22:33 +08:00
|
|
|
return fcntl64 (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;
|
2008-04-01 18:22:33 +08:00
|
|
|
return fcntl64 (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. */
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
/* The mq_attr structure is defined using long datatypes per POSIX.
|
|
|
|
For interoperability reasons between 32 and 64 bit processes, we have
|
|
|
|
to make sure to use a unified structure layout in the message queue file.
|
|
|
|
That's what the mq_fattr is, the in-file representation of the mq_attr
|
|
|
|
struct. */
|
|
|
|
#pragma pack (push, 4)
|
|
|
|
struct mq_fattr
|
|
|
|
{
|
|
|
|
uint32_t mq_flags;
|
|
|
|
uint32_t mq_maxmsg;
|
|
|
|
uint32_t mq_msgsize;
|
|
|
|
uint32_t mq_curmsgs;
|
|
|
|
};
|
|
|
|
|
2007-02-14 18:06:46 +08:00
|
|
|
struct mq_hdr
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
struct mq_fattr mqh_attr; /* the queue's attributes */
|
|
|
|
int32_t mqh_head; /* index of first message */
|
|
|
|
int32_t mqh_free; /* index of first free message */
|
|
|
|
int32_t mqh_nwait; /* #threads blocked in mq_receive() */
|
2007-02-15 19:28:46 +08:00
|
|
|
pid_t mqh_pid; /* nonzero PID if mqh_event set */
|
2007-02-20 23:48:04 +08:00
|
|
|
char mqh_uname[36]; /* unique name used to identify synchronization
|
2007-07-08 00:43:03 +08:00
|
|
|
objects connected to this queue */
|
2013-04-23 17:44:36 +08:00
|
|
|
union {
|
|
|
|
struct sigevent mqh_event; /* for mq_notify() */
|
|
|
|
/* Make sure sigevent takes the same space on 32 and 64 bit systems.
|
|
|
|
Other than that, it doesn't need to be compatible since only
|
|
|
|
one process can be notified at a time. */
|
|
|
|
uint64_t mqh_placeholder[8];
|
|
|
|
};
|
|
|
|
uint32_t mqh_magic; /* Expect MQI_MAGIC here, otherwise it's
|
|
|
|
an old-style message queue. */
|
2007-02-14 18:06:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct msg_hdr
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
int32_t msg_next; /* index of next on linked list */
|
|
|
|
int32_t msg_len; /* actual length */
|
2007-02-15 19:28:46 +08:00
|
|
|
unsigned int msg_prio; /* priority */
|
2007-02-14 18:06:46 +08:00
|
|
|
};
|
2013-04-23 17:44:36 +08:00
|
|
|
#pragma pack (pop)
|
2007-02-14 18:06:46 +08:00
|
|
|
|
|
|
|
struct mq_info
|
|
|
|
{
|
2007-02-15 19:28:46 +08:00
|
|
|
struct mq_hdr *mqi_hdr; /* start of mmap'ed region */
|
2013-04-23 17:44:36 +08:00
|
|
|
uint32_t mqi_magic; /* magic number if open */
|
2007-02-15 19:28:46 +08:00
|
|
|
int mqi_flags; /* flags for this process */
|
|
|
|
HANDLE mqi_lock; /* mutex lock */
|
2010-10-07 04:20:01 +08:00
|
|
|
HANDLE mqi_waitsend; /* and condition variable for full queue */
|
|
|
|
HANDLE mqi_waitrecv; /* and condition variable for empty queue */
|
2007-02-14 18:06:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define MQI_MAGIC 0x98765432UL
|
|
|
|
|
|
|
|
#define MSGSIZE(i) roundup((i), sizeof(long))
|
|
|
|
|
|
|
|
#define MAX_TRIES 10 /* for waiting for initialization */
|
|
|
|
|
|
|
|
struct mq_attr defattr = { 0, 10, 8192, 0 }; /* Linux defaults. */
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
extern "C" off_t lseek64 (int, off_t, int);
|
|
|
|
extern "C" void *mmap64 (void *, size_t, int, int, int, off_t);
|
2007-02-14 18:06:46 +08:00
|
|
|
|
|
|
|
extern "C" mqd_t
|
|
|
|
mq_open (const char *name, int oflag, ...)
|
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
int i, fd = -1, nonblock, created = 0;
|
2007-02-14 18:06:46 +08:00
|
|
|
long msgsize, index;
|
2013-04-23 17:44:36 +08:00
|
|
|
off_t filesize = 0;
|
2007-02-14 18:06:46 +08:00
|
|
|
va_list ap;
|
|
|
|
mode_t mode;
|
2014-08-22 17:21:33 +08:00
|
|
|
int8_t *mptr = (int8_t *) MAP_FAILED;
|
2013-04-23 17:44:36 +08:00
|
|
|
struct stat statbuff;
|
2007-02-14 18:06:46 +08:00
|
|
|
struct mq_hdr *mqhdr;
|
|
|
|
struct msg_hdr *msghdr;
|
|
|
|
struct mq_attr *attr;
|
2014-08-22 17:21:33 +08:00
|
|
|
struct mq_info *mqinfo = NULL;
|
2007-02-20 23:48:04 +08:00
|
|
|
LUID luid;
|
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
|
|
|
|
{
|
|
|
|
oflag &= (O_CREAT | O_EXCL | O_NONBLOCK);
|
|
|
|
nonblock = oflag & O_NONBLOCK;
|
|
|
|
oflag &= ~O_NONBLOCK;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
again:
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* Open and specify O_EXCL and user-execute */
|
|
|
|
fd = open (mqname, 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 (mqd_t) -1;
|
|
|
|
}
|
|
|
|
created = 1;
|
|
|
|
/* First one to create the file initializes it */
|
|
|
|
if (attr == NULL)
|
|
|
|
attr = &defattr;
|
|
|
|
/* Check minimum and maximum values. The max values are pretty much
|
|
|
|
arbitrary, taken from the linux mq_overview man page. However,
|
|
|
|
these max values make sure that the internal mq_fattr structure
|
|
|
|
can use 32 bit types. */
|
|
|
|
else if (attr->mq_maxmsg <= 0 || attr->mq_maxmsg > 32768
|
|
|
|
|| attr->mq_msgsize <= 0 || attr->mq_msgsize > 1048576)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
/* Calculate and set the file size */
|
|
|
|
msgsize = MSGSIZE (attr->mq_msgsize);
|
|
|
|
filesize = sizeof (struct mq_hdr)
|
|
|
|
+ (attr->mq_maxmsg * (sizeof (struct msg_hdr) + msgsize));
|
|
|
|
if (lseek64 (fd, filesize - 1, SEEK_SET) == -1)
|
|
|
|
__leave;
|
|
|
|
if (write (fd, "", 1) == -1)
|
|
|
|
__leave;
|
|
|
|
|
|
|
|
/* Memory map the file */
|
|
|
|
mptr = (int8_t *) mmap64 (NULL, (size_t) filesize,
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED, fd, 0);
|
|
|
|
if (mptr == (int8_t *) MAP_FAILED)
|
|
|
|
__leave;
|
|
|
|
|
|
|
|
/* Allocate one mq_info{} for the queue */
|
|
|
|
if (!(mqinfo = (struct mq_info *)
|
|
|
|
calloc (1, sizeof (struct mq_info))))
|
|
|
|
__leave;
|
|
|
|
mqinfo->mqi_hdr = mqhdr = (struct mq_hdr *) mptr;
|
|
|
|
mqinfo->mqi_magic = MQI_MAGIC;
|
|
|
|
mqinfo->mqi_flags = nonblock;
|
|
|
|
|
|
|
|
/* Initialize header at beginning of file */
|
|
|
|
/* Create free list with all messages on it */
|
|
|
|
mqhdr->mqh_attr.mq_flags = 0;
|
|
|
|
mqhdr->mqh_attr.mq_maxmsg = attr->mq_maxmsg;
|
|
|
|
mqhdr->mqh_attr.mq_msgsize = attr->mq_msgsize;
|
|
|
|
mqhdr->mqh_attr.mq_curmsgs = 0;
|
|
|
|
mqhdr->mqh_nwait = 0;
|
|
|
|
mqhdr->mqh_pid = 0;
|
|
|
|
NtAllocateLocallyUniqueId (&luid);
|
|
|
|
__small_sprintf (mqhdr->mqh_uname, "%016X%08x%08x",
|
|
|
|
hash_path_name (0,mqname),
|
|
|
|
luid.HighPart, luid.LowPart);
|
|
|
|
mqhdr->mqh_head = 0;
|
|
|
|
mqhdr->mqh_magic = MQI_MAGIC;
|
|
|
|
index = sizeof (struct mq_hdr);
|
|
|
|
mqhdr->mqh_free = index;
|
|
|
|
for (i = 0; i < attr->mq_maxmsg - 1; i++)
|
|
|
|
{
|
|
|
|
msghdr = (struct msg_hdr *) &mptr[index];
|
|
|
|
index += sizeof (struct msg_hdr) + msgsize;
|
|
|
|
msghdr->msg_next = index;
|
|
|
|
}
|
|
|
|
msghdr = (struct msg_hdr *) &mptr[index];
|
|
|
|
msghdr->msg_next = 0; /* end of free list */
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
/* Initialize mutex & condition variables */
|
|
|
|
i = ipc_mutex_init (&mqinfo->mqi_lock, mqhdr->mqh_uname);
|
|
|
|
if (i != 0)
|
|
|
|
{
|
|
|
|
set_errno (i);
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
i = ipc_cond_init (&mqinfo->mqi_waitsend, mqhdr->mqh_uname, 'S');
|
|
|
|
if (i != 0)
|
|
|
|
{
|
|
|
|
set_errno (i);
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
i = ipc_cond_init (&mqinfo->mqi_waitrecv, mqhdr->mqh_uname, 'R');
|
|
|
|
if (i != 0)
|
|
|
|
{
|
|
|
|
set_errno (i);
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
/* Initialization complete, turn off user-execute bit */
|
|
|
|
if (fchmod (fd, mode) == -1)
|
|
|
|
__leave;
|
|
|
|
close (fd);
|
|
|
|
return ((mqd_t) mqinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
exists:
|
|
|
|
/* Open the file then memory map */
|
|
|
|
if ((fd = open (mqname, O_RDWR | O_CLOEXEC)) < 0)
|
2007-07-08 00:43:03 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (errno == ENOENT && (oflag & O_CREAT))
|
|
|
|
goto again;
|
|
|
|
__leave;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
/* Make certain initialization is complete */
|
|
|
|
for (i = 0; i < MAX_TRIES; i++)
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (stat64 (mqname, &statbuff) == -1)
|
|
|
|
{
|
|
|
|
if (errno == ENOENT && (oflag & O_CREAT))
|
|
|
|
{
|
|
|
|
close (fd);
|
|
|
|
fd = -1;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
if ((statbuff.st_mode & S_IXUSR) == 0)
|
|
|
|
break;
|
|
|
|
sleep (1);
|
|
|
|
}
|
|
|
|
if (i == MAX_TRIES)
|
|
|
|
{
|
|
|
|
set_errno (ETIMEDOUT);
|
|
|
|
__leave;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
filesize = statbuff.st_size;
|
2007-02-14 18:06:46 +08:00
|
|
|
mptr = (int8_t *) mmap64 (NULL, (size_t) filesize, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED, fd, 0);
|
|
|
|
if (mptr == (int8_t *) MAP_FAILED)
|
2014-08-22 17:21:33 +08:00
|
|
|
__leave;
|
|
|
|
close (fd);
|
|
|
|
fd = -1;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
/* Allocate one mq_info{} for each open */
|
2010-10-07 04:20:01 +08:00
|
|
|
if (!(mqinfo = (struct mq_info *) calloc (1, sizeof (struct mq_info))))
|
2014-08-22 17:21:33 +08:00
|
|
|
__leave;
|
2007-02-14 18:06:46 +08:00
|
|
|
mqinfo->mqi_hdr = mqhdr = (struct mq_hdr *) mptr;
|
2014-08-22 17:21:33 +08:00
|
|
|
if (mqhdr->mqh_magic != MQI_MAGIC)
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
system_printf (
|
|
|
|
"Old message queue \"%s\" detected!\n"
|
|
|
|
"This file is not usable as message queue anymore due to changes in the "
|
|
|
|
"internal file layout. Please remove the file and try again.", mqname);
|
|
|
|
set_errno (EACCES);
|
|
|
|
__leave;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
mqinfo->mqi_magic = MQI_MAGIC;
|
|
|
|
mqinfo->mqi_flags = nonblock;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
/* Initialize mutex & condition variable */
|
2007-02-15 19:28:46 +08:00
|
|
|
i = ipc_mutex_init (&mqinfo->mqi_lock, mqhdr->mqh_uname);
|
2007-02-14 18:06:46 +08:00
|
|
|
if (i != 0)
|
2014-08-22 17:21:33 +08:00
|
|
|
{
|
|
|
|
set_errno (i);
|
|
|
|
__leave;
|
|
|
|
}
|
2010-10-07 04:20:01 +08:00
|
|
|
i = ipc_cond_init (&mqinfo->mqi_waitsend, mqhdr->mqh_uname, 'S');
|
|
|
|
if (i != 0)
|
2014-08-22 17:21:33 +08:00
|
|
|
{
|
|
|
|
set_errno (i);
|
|
|
|
__leave;
|
|
|
|
}
|
2010-10-07 04:20:01 +08:00
|
|
|
i = ipc_cond_init (&mqinfo->mqi_waitrecv, mqhdr->mqh_uname, 'R');
|
2007-02-14 18:06:46 +08:00
|
|
|
if (i != 0)
|
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
set_errno (i);
|
|
|
|
__leave;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
return (mqd_t) mqinfo;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
__except (EFAULT) {}
|
|
|
|
__endtry
|
2007-02-14 18:06:46 +08:00
|
|
|
/* Don't let following function calls change errno */
|
|
|
|
save_errno save;
|
|
|
|
if (created)
|
|
|
|
unlink (mqname);
|
|
|
|
if (mptr != (int8_t *) MAP_FAILED)
|
|
|
|
munmap((void *) mptr, (size_t) filesize);
|
|
|
|
if (mqinfo)
|
2010-10-07 04:20:01 +08:00
|
|
|
{
|
|
|
|
if (mqinfo->mqi_lock)
|
2011-06-06 13:02:13 +08:00
|
|
|
ipc_mutex_close (mqinfo->mqi_lock);
|
2010-10-07 04:20:01 +08:00
|
|
|
if (mqinfo->mqi_waitsend)
|
|
|
|
ipc_cond_close (mqinfo->mqi_waitsend);
|
|
|
|
if (mqinfo->mqi_waitrecv)
|
|
|
|
ipc_cond_close (mqinfo->mqi_waitrecv);
|
|
|
|
free (mqinfo);
|
|
|
|
}
|
2009-09-24 17:25:35 +08:00
|
|
|
if (fd >= 0)
|
|
|
|
close (fd);
|
2007-02-14 18:06:46 +08:00
|
|
|
return (mqd_t) -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
mq_getattr (mqd_t mqd, struct mq_attr *mqstat)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
struct mq_hdr *mqhdr;
|
2013-04-23 17:44:36 +08:00
|
|
|
struct mq_fattr *attr;
|
2007-02-14 18:06:46 +08:00
|
|
|
struct mq_info *mqinfo;
|
2007-07-08 00:43:03 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
mqinfo = (struct mq_info *) mqd;
|
|
|
|
if (mqinfo->mqi_magic != MQI_MAGIC)
|
|
|
|
{
|
|
|
|
set_errno (EBADF);
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
mqhdr = mqinfo->mqi_hdr;
|
|
|
|
attr = &mqhdr->mqh_attr;
|
2015-02-27 01:00:11 +08:00
|
|
|
if ((n = ipc_mutex_lock (mqinfo->mqi_lock, false)) != 0)
|
2014-08-22 17:21:33 +08:00
|
|
|
{
|
|
|
|
errno = n;
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
mqstat->mq_flags = mqinfo->mqi_flags; /* per-open */
|
|
|
|
mqstat->mq_maxmsg = attr->mq_maxmsg; /* remaining three per-queue */
|
|
|
|
mqstat->mq_msgsize = attr->mq_msgsize;
|
|
|
|
mqstat->mq_curmsgs = attr->mq_curmsgs;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
ipc_mutex_unlock (mqinfo->mqi_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
__except (EBADF) {}
|
|
|
|
__endtry
|
|
|
|
return -1;
|
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)
|
|
|
|
{
|
|
|
|
int n;
|
2007-07-08 00:43:03 +08:00
|
|
|
struct mq_hdr *mqhdr;
|
2013-04-23 17:44:36 +08:00
|
|
|
struct mq_fattr *attr;
|
2007-02-14 18:06:46 +08:00
|
|
|
struct mq_info *mqinfo;
|
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
mqinfo = (struct mq_info *) mqd;
|
|
|
|
if (mqinfo->mqi_magic != MQI_MAGIC)
|
|
|
|
{
|
|
|
|
set_errno (EBADF);
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
mqhdr = mqinfo->mqi_hdr;
|
|
|
|
attr = &mqhdr->mqh_attr;
|
2015-02-27 01:00:11 +08:00
|
|
|
if ((n = ipc_mutex_lock (mqinfo->mqi_lock, false)) != 0)
|
2014-08-22 17:21:33 +08:00
|
|
|
{
|
|
|
|
errno = n;
|
|
|
|
__leave;
|
|
|
|
}
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
if (omqstat != NULL)
|
|
|
|
{
|
|
|
|
omqstat->mq_flags = mqinfo->mqi_flags; /* previous attributes */
|
|
|
|
omqstat->mq_maxmsg = attr->mq_maxmsg;
|
|
|
|
omqstat->mq_msgsize = attr->mq_msgsize;
|
|
|
|
omqstat->mq_curmsgs = attr->mq_curmsgs; /* and current status */
|
|
|
|
}
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
if (mqstat->mq_flags & O_NONBLOCK)
|
|
|
|
mqinfo->mqi_flags |= O_NONBLOCK;
|
|
|
|
else
|
|
|
|
mqinfo->mqi_flags &= ~O_NONBLOCK;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
ipc_mutex_unlock (mqinfo->mqi_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
__except (EBADF) {}
|
|
|
|
__endtry
|
|
|
|
return -1;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
mq_notify (mqd_t mqd, const struct sigevent *notification)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
pid_t pid;
|
|
|
|
struct mq_hdr *mqhdr;
|
|
|
|
struct mq_info *mqinfo;
|
2007-07-08 00:43:03 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
mqinfo = (struct mq_info *) mqd;
|
|
|
|
if (mqinfo->mqi_magic != MQI_MAGIC)
|
|
|
|
{
|
|
|
|
set_errno (EBADF);
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
mqhdr = mqinfo->mqi_hdr;
|
2015-02-27 01:00:11 +08:00
|
|
|
if ((n = ipc_mutex_lock (mqinfo->mqi_lock, false)) != 0)
|
2014-08-22 17:21:33 +08:00
|
|
|
{
|
|
|
|
errno = n;
|
|
|
|
__leave;
|
|
|
|
}
|
2007-07-08 00:43:03 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
pid = getpid ();
|
|
|
|
if (!notification)
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (mqhdr->mqh_pid == pid)
|
|
|
|
mqhdr->mqh_pid = 0; /* unregister calling process */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (mqhdr->mqh_pid != 0)
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (kill (mqhdr->mqh_pid, 0) != -1 || errno != ESRCH)
|
|
|
|
{
|
|
|
|
set_errno (EBUSY);
|
|
|
|
ipc_mutex_unlock (mqinfo->mqi_lock);
|
|
|
|
__leave;
|
|
|
|
}
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
mqhdr->mqh_pid = pid;
|
|
|
|
mqhdr->mqh_event = *notification;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
ipc_mutex_unlock (mqinfo->mqi_lock);
|
|
|
|
return 0;
|
2007-07-08 00:43:03 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
__except (EBADF) {}
|
|
|
|
__endtry
|
|
|
|
return -1;
|
2007-07-08 00:43:03 +08:00
|
|
|
}
|
2007-02-14 18:06:46 +08:00
|
|
|
|
|
|
|
static int
|
|
|
|
_mq_send (mqd_t mqd, const char *ptr, size_t len, unsigned int prio,
|
|
|
|
const struct timespec *abstime)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
long index, freeindex;
|
|
|
|
int8_t *mptr;
|
|
|
|
struct sigevent *sigev;
|
|
|
|
struct mq_hdr *mqhdr;
|
2013-04-23 17:44:36 +08:00
|
|
|
struct mq_fattr *attr;
|
2007-02-14 18:06:46 +08:00
|
|
|
struct msg_hdr *msghdr, *nmsghdr, *pmsghdr;
|
2014-08-22 17:21:33 +08:00
|
|
|
struct mq_info *mqinfo = NULL;
|
|
|
|
bool ipc_mutex_locked = false;
|
|
|
|
int ret = -1;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2011-04-28 20:36:27 +08:00
|
|
|
pthread_testcancel ();
|
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
mqinfo = (struct mq_info *) mqd;
|
|
|
|
if (mqinfo->mqi_magic != MQI_MAGIC)
|
|
|
|
{
|
|
|
|
set_errno (EBADF);
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
if (prio > MQ_PRIO_MAX)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
__leave;
|
|
|
|
}
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
mqhdr = mqinfo->mqi_hdr; /* struct pointer */
|
|
|
|
mptr = (int8_t *) mqhdr; /* byte pointer */
|
|
|
|
attr = &mqhdr->mqh_attr;
|
2015-02-27 01:00:11 +08:00
|
|
|
if ((n = ipc_mutex_lock (mqinfo->mqi_lock, true)) != 0)
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
errno = n;
|
|
|
|
__leave;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
ipc_mutex_locked = true;
|
|
|
|
if (len > (size_t) attr->mq_msgsize)
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
set_errno (EMSGSIZE);
|
|
|
|
__leave;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
if (attr->mq_curmsgs == 0)
|
2011-03-03 23:48:36 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (mqhdr->mqh_pid != 0 && mqhdr->mqh_nwait == 0)
|
2011-03-03 23:48:36 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
sigev = &mqhdr->mqh_event;
|
|
|
|
if (sigev->sigev_notify == SIGEV_SIGNAL)
|
|
|
|
sigqueue (mqhdr->mqh_pid, sigev->sigev_signo,
|
|
|
|
sigev->sigev_value);
|
|
|
|
mqhdr->mqh_pid = 0; /* unregister */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (attr->mq_curmsgs >= attr->mq_maxmsg)
|
|
|
|
{
|
|
|
|
/* Queue is full */
|
|
|
|
if (mqinfo->mqi_flags & O_NONBLOCK)
|
|
|
|
{
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
/* Wait for room for one message on the queue */
|
|
|
|
while (attr->mq_curmsgs >= attr->mq_maxmsg)
|
|
|
|
{
|
|
|
|
int ret = ipc_cond_timedwait (mqinfo->mqi_waitsend,
|
|
|
|
mqinfo->mqi_lock, abstime);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
set_errno (ret);
|
|
|
|
__leave;
|
|
|
|
}
|
2011-03-03 23:48:36 +08:00
|
|
|
}
|
|
|
|
}
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
/* nmsghdr will point to new message */
|
|
|
|
if ((freeindex = mqhdr->mqh_free) == 0)
|
|
|
|
api_fatal ("mq_send: curmsgs = %ld; free = 0", attr->mq_curmsgs);
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
nmsghdr = (struct msg_hdr *) &mptr[freeindex];
|
|
|
|
nmsghdr->msg_prio = prio;
|
|
|
|
nmsghdr->msg_len = len;
|
|
|
|
memcpy (nmsghdr + 1, ptr, len); /* copy message from caller */
|
|
|
|
mqhdr->mqh_free = nmsghdr->msg_next; /* new freelist head */
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
/* Find right place for message in linked list */
|
|
|
|
index = mqhdr->mqh_head;
|
|
|
|
pmsghdr = (struct msg_hdr *) &(mqhdr->mqh_head);
|
|
|
|
while (index)
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
msghdr = (struct msg_hdr *) &mptr[index];
|
|
|
|
if (prio > msghdr->msg_prio)
|
|
|
|
{
|
|
|
|
nmsghdr->msg_next = index;
|
|
|
|
pmsghdr->msg_next = freeindex;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
index = msghdr->msg_next;
|
|
|
|
pmsghdr = msghdr;
|
|
|
|
}
|
|
|
|
if (index == 0)
|
|
|
|
{
|
|
|
|
/* Queue was empty or new goes at end of list */
|
2007-02-14 18:06:46 +08:00
|
|
|
pmsghdr->msg_next = freeindex;
|
2014-08-22 17:21:33 +08:00
|
|
|
nmsghdr->msg_next = 0;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
/* Wake up anyone blocked in mq_receive waiting for a message */
|
|
|
|
if (attr->mq_curmsgs == 0)
|
|
|
|
ipc_cond_signal (mqinfo->mqi_waitrecv);
|
|
|
|
attr->mq_curmsgs++;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
ipc_mutex_unlock (mqinfo->mqi_lock);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
__except (EBADF) {}
|
|
|
|
__endtry
|
|
|
|
if (ipc_mutex_locked)
|
|
|
|
ipc_mutex_unlock (mqinfo->mqi_lock);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return _mq_send (mqd, ptr, len, prio, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
mq_timedsend (mqd_t mqd, const char *ptr, size_t len, unsigned int prio,
|
|
|
|
const struct timespec *abstime)
|
|
|
|
{
|
|
|
|
return _mq_send (mqd, ptr, len, prio, abstime);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
_mq_receive (mqd_t mqd, char *ptr, size_t maxlen, unsigned int *priop,
|
|
|
|
const struct timespec *abstime)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
long index;
|
|
|
|
int8_t *mptr;
|
2014-08-22 17:21:33 +08:00
|
|
|
ssize_t len = -1;
|
2007-07-08 00:43:03 +08:00
|
|
|
struct mq_hdr *mqhdr;
|
2013-04-23 17:44:36 +08:00
|
|
|
struct mq_fattr *attr;
|
2007-02-14 18:06:46 +08:00
|
|
|
struct msg_hdr *msghdr;
|
2014-08-22 17:21:33 +08:00
|
|
|
struct mq_info *mqinfo = (struct mq_info *) mqd;
|
|
|
|
bool ipc_mutex_locked = false;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2011-04-28 20:36:27 +08:00
|
|
|
pthread_testcancel ();
|
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (mqinfo->mqi_magic != MQI_MAGIC)
|
|
|
|
{
|
|
|
|
set_errno (EBADF);
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
mqhdr = mqinfo->mqi_hdr; /* struct pointer */
|
|
|
|
mptr = (int8_t *) mqhdr; /* byte pointer */
|
|
|
|
attr = &mqhdr->mqh_attr;
|
2015-02-27 01:00:11 +08:00
|
|
|
if ((n = ipc_mutex_lock (mqinfo->mqi_lock, true)) != 0)
|
2014-08-22 17:21:33 +08:00
|
|
|
{
|
|
|
|
errno = n;
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
ipc_mutex_locked = true;
|
|
|
|
if (maxlen < (size_t) attr->mq_msgsize)
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
set_errno (EMSGSIZE);
|
|
|
|
__leave;
|
2007-02-14 18:06:46 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
if (attr->mq_curmsgs == 0) /* queue is empty */
|
2011-03-03 23:48:36 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (mqinfo->mqi_flags & O_NONBLOCK)
|
2011-03-03 23:48:36 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
set_errno (EAGAIN);
|
|
|
|
__leave;
|
2011-03-03 23:48:36 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
/* Wait for a message to be placed onto queue */
|
|
|
|
mqhdr->mqh_nwait++;
|
|
|
|
while (attr->mq_curmsgs == 0)
|
|
|
|
{
|
|
|
|
int ret = ipc_cond_timedwait (mqinfo->mqi_waitrecv,
|
|
|
|
mqinfo->mqi_lock, abstime);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
set_errno (ret);
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mqhdr->mqh_nwait--;
|
2011-03-03 23:48:36 +08:00
|
|
|
}
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
if ((index = mqhdr->mqh_head) == 0)
|
|
|
|
api_fatal ("mq_receive: curmsgs = %ld; head = 0", attr->mq_curmsgs);
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
msghdr = (struct msg_hdr *) &mptr[index];
|
|
|
|
mqhdr->mqh_head = msghdr->msg_next; /* new head of list */
|
|
|
|
len = msghdr->msg_len;
|
|
|
|
memcpy(ptr, msghdr + 1, len); /* copy the message itself */
|
|
|
|
if (priop != NULL)
|
|
|
|
*priop = msghdr->msg_prio;
|
|
|
|
|
|
|
|
/* Just-read message goes to front of free list */
|
|
|
|
msghdr->msg_next = mqhdr->mqh_free;
|
|
|
|
mqhdr->mqh_free = index;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
/* Wake up anyone blocked in mq_send waiting for room */
|
|
|
|
if (attr->mq_curmsgs == attr->mq_maxmsg)
|
|
|
|
ipc_cond_signal (mqinfo->mqi_waitsend);
|
|
|
|
attr->mq_curmsgs--;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
ipc_mutex_unlock (mqinfo->mqi_lock);
|
|
|
|
}
|
|
|
|
__except (EBADF) {}
|
|
|
|
__endtry
|
|
|
|
if (ipc_mutex_locked)
|
|
|
|
ipc_mutex_unlock (mqinfo->mqi_lock);
|
2007-02-14 18:06:46 +08:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" ssize_t
|
|
|
|
mq_receive (mqd_t mqd, char *ptr, size_t maxlen, unsigned int *priop)
|
|
|
|
{
|
|
|
|
return _mq_receive (mqd, ptr, maxlen, priop, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" ssize_t
|
|
|
|
mq_timedreceive (mqd_t mqd, char *ptr, size_t maxlen, unsigned int *priop,
|
|
|
|
const struct timespec *abstime)
|
|
|
|
{
|
|
|
|
return _mq_receive (mqd, ptr, maxlen, priop, abstime);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
mq_close (mqd_t mqd)
|
|
|
|
{
|
|
|
|
long msgsize, filesize;
|
|
|
|
struct mq_hdr *mqhdr;
|
2013-04-23 17:44:36 +08:00
|
|
|
struct mq_fattr *attr;
|
2007-02-14 18:06:46 +08:00
|
|
|
struct mq_info *mqinfo;
|
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2007-02-14 18:06:46 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
mqinfo = (struct mq_info *) mqd;
|
|
|
|
if (mqinfo->mqi_magic != MQI_MAGIC)
|
|
|
|
{
|
|
|
|
set_errno (EBADF);
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
mqhdr = mqinfo->mqi_hdr;
|
|
|
|
attr = &mqhdr->mqh_attr;
|
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
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
msgsize = MSGSIZE (attr->mq_msgsize);
|
|
|
|
filesize = sizeof (struct mq_hdr)
|
|
|
|
+ (attr->mq_maxmsg * (sizeof (struct msg_hdr) + msgsize));
|
|
|
|
if (munmap (mqinfo->mqi_hdr, filesize) == -1)
|
|
|
|
__leave;
|
2007-02-14 18:06:46 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
mqinfo->mqi_magic = 0; /* just in case */
|
|
|
|
ipc_cond_close (mqinfo->mqi_waitsend);
|
|
|
|
ipc_cond_close (mqinfo->mqi_waitrecv);
|
|
|
|
ipc_mutex_close (mqinfo->mqi_lock);
|
|
|
|
free (mqinfo);
|
|
|
|
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. */
|
|
|
|
|
|
|
|
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++)
|
|
|
|
{
|
|
|
|
if (stat64 (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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2013-04-23 17:44:36 +08:00
|
|
|
&& lseek64 (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;
|
|
|
|
}
|