features.h: support POSIX.1-2024
[newlib-cygwin.git] / winsup / cygserver / msg.cc
blobbf7d7b2ffd2a7f4018c21caf7a4de1eceae552b8
1 /* msg.cc: Single unix specification IPC interface for Cygwin.
3 This file is part of Cygwin.
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
7 details. */
9 #ifdef __OUTSIDE_CYGWIN__
10 #include "woutsup.h"
12 #include <errno.h>
13 #include <pthread.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <time.h>
19 #include "cygserver.h"
20 #include "process.h"
21 #include "transport.h"
23 #include "cygserver_ipc.h"
24 #include "cygserver_msg.h"
26 client_request_msg::client_request_msg ()
27 : client_request (CYGSERVER_REQUEST_MSG,
28 &_parameters, sizeof (_parameters))
32 void
33 client_request_msg::serve (transport_layer_base *const conn,
34 process_cache *const cache)
36 if (msglen () != sizeof (_parameters.in))
38 syscall_printf ("bad request body length: expecting %lu bytes, got %lu",
39 sizeof (_parameters), msglen ());
40 error_code (EINVAL);
41 msglen (0);
42 return;
44 if (support_msgqueues == TUN_FALSE)
46 syscall_printf ("Message queue support not started");
47 error_code (ENOSYS);
48 if (_parameters.in.msgop == MSGOP_msgrcv)
49 _parameters.out.rcv = -1;
50 else
51 _parameters.out.ret = -1;
52 msglen (sizeof (_parameters.out));
53 return;
55 process *const client = cache->process (_parameters.in.ipcblk.cygpid,
56 _parameters.in.ipcblk.winpid);
57 if (!client)
59 error_code (EAGAIN);
60 msglen (0);
61 return;
63 if (!conn->impersonate_client ())
65 client->release ();
66 error_code (EACCES);
67 msglen (0);
68 return;
70 if (!adjust_identity_info (&_parameters.in.ipcblk))
72 client->release ();
73 conn->revert_to_self ();
74 error_code (EACCES);
75 msglen (0);
76 return;
78 /* Early revert_to_self since IPC code runs in kernel mode. */
79 conn->revert_to_self ();
80 /* sysv_msg.cc takes care of itself. */
81 client->release ();
82 thread td (client, &_parameters.in.ipcblk, true);
83 int res;
84 msgop_t msgop = _parameters.in.msgop; /* Get's overwritten otherwise. */
85 switch (msgop)
87 case MSGOP_msgctl:
88 res = msgctl (&td, &_parameters.in.ctlargs);
89 break;
90 case MSGOP_msgget:
91 res = msgget (&td, &_parameters.in.getargs);
92 break;
93 case MSGOP_msgrcv:
94 res = msgrcv (&td, &_parameters.in.rcvargs);
95 break;
96 case MSGOP_msgsnd:
97 res = msgsnd (&td, &_parameters.in.sndargs);
98 break;
99 default:
100 res = ENOSYS;
101 td.td_retval[0] = -1;
102 break;
104 /* Allocated by the call to adjust_identity_info(). */
105 if (_parameters.in.ipcblk.gidlist)
106 free (_parameters.in.ipcblk.gidlist);
107 error_code (res);
108 if (msgop == MSGOP_msgrcv)
109 _parameters.out.rcv = td.td_retval[0];
110 else
111 _parameters.out.ret = td.td_retval[0];
112 msglen (sizeof (_parameters.out));
114 #endif /* __OUTSIDE_CYGWIN__ */