1 /*        $NetBSD: ipc.h,v 1.38 2024/05/12 10:34:56 rillig Exp $      */
2 
3 /*
4  * Copyright (c) 1988 University of Utah.
5  * Copyright (c) 1990, 1993
6  *        The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * This code is derived from software contributed to Berkeley by
14  * the Systems Programming Group of the University of Utah Computer
15  * Science Department.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *        @(#)ipc.h 8.4 (Berkeley) 2/19/95
42  */
43 
44 /*
45  * SVID compatible ipc.h file
46  */
47 
48 #ifndef _SYS_IPC_H_
49 #define _SYS_IPC_H_
50 
51 #include <sys/featuretest.h>
52 #include <sys/types.h>
53 
54 struct ipc_perm {
55           uid_t               uid;      /* user id */
56           gid_t               gid;      /* group id */
57           uid_t               cuid;     /* creator user id */
58           gid_t               cgid;     /* creator group id */
59           mode_t              mode;     /* r/w permission */
60 
61           /*
62            * These members are private and used only in the internal
63            * implementation of this interface.
64            */
65           unsigned short      _seq;     /* sequence # (to generate unique
66                                            msg/sem/shm id) */
67           key_t               _key;     /* user specified msg/sem/shm key */
68 };
69 
70 #if defined(_NETBSD_SOURCE)
71 /* Warning: 64-bit structure padding is needed here */
72 struct ipc_perm_sysctl {
73           uint64_t  _key;
74           uid_t               uid;
75           gid_t               gid;
76           uid_t               cuid;
77           gid_t               cgid;
78           mode_t              mode;
79           int16_t             _seq;
80           int16_t             pad;
81 };
82 #endif /* _NETBSD_SOURCE */
83 
84 /* Common access type bits, used with ipcperm(). */
85 #define   IPC_R               000400    /* read permission */
86 #define   IPC_W               000200    /* write/alter permission */
87 #define   IPC_M               010000    /* permission to change control info */
88 
89 /* X/Open required constants (same values as system 5) */
90 #define   IPC_CREAT 001000    /* create entry if key does not exist */
91 #define   IPC_EXCL  002000    /* fail if key exists */
92 #define   IPC_NOWAIT          004000    /* error if request must wait */
93 
94 #define   IPC_PRIVATE         (key_t)0 /* private key */
95 
96 #define   IPC_RMID  0         /* remove identifier */
97 #define   IPC_SET             1         /* set options */
98 #define   IPC_STAT  2         /* get options */
99 
100 /*
101  * Macros to convert between ipc ids and array indices or sequence ids.
102  * The first of these is used by ipcs(1), and so is defined outside the
103  * kernel as well.
104  */
105 #if defined(_NETBSD_SOURCE)
106 #define   IXSEQ_TO_IPCID(ix,perm)       (((perm._seq) << 16) | (ix & 0xffff))
107 #endif
108 
109 #ifdef _KERNEL
110 #include <sys/sysctl.h>
111 #define   IPCID_TO_IX(id)               ((id) & 0xffff)
112 #define   IPCID_TO_SEQ(id)    (((id) >> 16) & 0xffff)
113 
114 struct kauth_cred;
115 __BEGIN_DECLS
116 int       ipcperm(struct kauth_cred *, struct ipc_perm *, int);
117 
118 void      sysvipcinit(void);
119 void      sysvipcfini(void);
120 __END_DECLS
121 
122 /*
123  * sysctl helper routine for kern.ipc.sysvipc_info subtree.
124  */
125 
126 #define SYSCTL_FILL_PERM(src, dst) do { \
127           (dst)._key = (src)._key; \
128           (dst).uid = (src).uid; \
129           (dst).gid = (src).gid; \
130           (dst).cuid = (src).cuid; \
131           (dst).cgid = (src).cgid; \
132           (dst).mode = (src).mode; \
133           (dst)._seq = (src)._seq; \
134 } while (0)
135 
136 /*
137  * Set-up the sysctl routine for COMPAT_50
138  */
139 
140 __BEGIN_DECLS
141 void sysvipc50_set_compat_sysctl(int (*)(SYSCTLFN_PROTO));
142 __END_DECLS
143 
144 #else /* _KERNEL */
145 __BEGIN_DECLS
146 key_t     ftok(const char *, int);
147 __END_DECLS
148 #endif
149 #endif /* !_SYS_IPC_H_ */
150