features.h: support POSIX.1-2024
[newlib-cygwin.git] / winsup / cygserver / sem.cc
blob2c02264787eb4522e204bb3ef4cad3f28c0c6392
1 /* sem.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_sem.h"
26 client_request_sem::client_request_sem ()
27 : client_request (CYGSERVER_REQUEST_SEM,
28 &_parameters, sizeof (_parameters))
32 void
33 client_request_sem::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_semaphores == TUN_FALSE)
46 syscall_printf ("Semaphore support not started");
47 error_code (ENOSYS);
48 _parameters.out.ret = -1;
49 msglen (sizeof (_parameters.out));
50 return;
52 process *const client = cache->process (_parameters.in.ipcblk.cygpid,
53 _parameters.in.ipcblk.winpid);
54 if (!client)
56 error_code (EAGAIN);
57 msglen (0);
58 return;
60 if (!conn->impersonate_client ())
62 client->release ();
63 error_code (EACCES);
64 msglen (0);
65 return;
67 if (!adjust_identity_info (&_parameters.in.ipcblk))
69 client->release ();
70 conn->revert_to_self ();
71 error_code (EACCES);
72 msglen (0);
73 return;
75 /* Early revert_to_self since IPC code runs in kernel mode. */
76 conn->revert_to_self ();
77 /* sysv_sem.cc takes care of itself. */
78 client->release ();
79 thread td (client, &_parameters.in.ipcblk, true);
80 int res;
81 switch (_parameters.in.semop)
83 case SEMOP_semctl:
84 res = semctl (&td, &_parameters.in.ctlargs);
85 break;
86 case SEMOP_semget:
87 res = semget (&td, &_parameters.in.getargs);
88 break;
89 case SEMOP_semop:
90 res = semop (&td, &_parameters.in.opargs);
91 break;
92 default:
93 res = ENOSYS;
94 td.td_retval[0] = -1;
95 break;
97 /* Allocated by the call to adjust_identity_info(). */
98 if (_parameters.in.ipcblk.gidlist)
99 free (_parameters.in.ipcblk.gidlist);
100 error_code (res);
101 _parameters.out.ret = td.td_retval[0];
102 msglen (sizeof (_parameters.out));
104 #endif /* __OUTSIDE_CYGWIN__ */