De-K&R-ify source, remove register keywords.
[dragonfly.git] / sys / sys / caps.h
1 /*
2  * SYS/CAPS.H
3  *
4  *      Implements an architecture independant Capability Service API
5  * 
6  * $DragonFly: src/sys/sys/caps.h,v 1.3 2004/01/18 12:50:15 dillon Exp $
7  */
8
9 #ifndef _SYS_CAPS_H_
10 #define _SYS_CAPS_H_
11
12 #ifndef _SYS_TYPES_H_
13 #include <sys/types.h>
14 #endif
15 #ifndef _SYS_MSGPORT_H_
16 #include <sys/msgport.h>
17 #endif
18
19 typedef enum caps_msg_state { 
20         CAPMS_REQUEST, 
21         CAPMS_REQUEST_RETRY,    /* internal / FUTURE */
22         CAPMS_REPLY, 
23         CAPMS_REPLY_RETRY,      /* internal / FUGURE */
24         CAPMS_DISPOSE
25 } caps_msg_state_t;
26
27 typedef struct caps_msgid {
28         off_t                   c_id;
29         caps_msg_state_t        c_state;
30         int                     c_reserved01;
31 } *caps_msgid_t;
32
33 typedef enum caps_type { 
34         CAPT_UNKNOWN, CAPT_CLIENT, CAPT_SERVICE, CAPT_REMOTE 
35 } caps_type_t;
36
37 /*
38  * Note: upper 16 bits reserved for kernel use
39  */
40 #define CAPF_UFLAGS     0xFFFF
41 #define CAPF_USER       0x0001
42 #define CAPF_GROUP      0x0002
43 #define CAPF_WORLD      0x0004
44 #define CAPF_EXCL       0x0008
45 #define CAPF_ANYCLIENT  (CAPF_USER|CAPF_GROUP|CAPF_WORLD)
46 #define CAPF_WCRED      0x0010  /* waiting for cred */
47 /* FUTURE: CAPF_ASYNC - support async services */
48 /* FUTURE: CAPF_NOGROUPS - don't bother filling in the groups[] array */
49 /* FUTURE: CAPF_TERM - send termination request to existing service */
50 /* FUTURE: CAPF_TAKE - take over existing service's connections */
51 /* FUTURE: CAPF_DISPOSE_IMM - need immediate dispose wakeups */
52
53 /*
54  * Abort codes
55  */
56 #define CAPS_ABORT_NOTIMPL      0       /* abort not implemented, no action */
57 #define CAPS_ABORT_RETURNED     1       /* already returned, no action */
58 #define CAPS_ABORT_BEFORESERVER 2       /* caught before the server got it */
59 #define CAPS_ABORT_ATSERVER     3       /* server had retrieved message */
60
61 #define CAPF_ABORT_HARD         0x0001  /* rip out from under server (3) */
62
63 #define CAPS_MAXGROUPS  16
64 #define CAPS_MAXNAMELEN 64
65 #define CAPS_MAXINPROG  128
66
67 struct thread;
68
69 typedef struct caps_port {
70         struct lwkt_port        cp_lport;
71         int                     cp_portid;      /* caps port id */
72         int                     cp_upcallid;    /* upcall id */
73 } *caps_port_t;
74
75 typedef struct caps_cred {
76         pid_t                   pid;
77         uid_t                   uid;
78         uid_t                   euid;
79         gid_t                   gid;
80         int                     ngroups;
81         int                     cacheid;
82         gid_t                   groups[CAPS_MAXGROUPS];
83 } *caps_cred_t;
84
85 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
86
87 struct caps_kmsg;
88
89 TAILQ_HEAD(caps_kmsg_queue, caps_kmsg);
90
91 /*
92  * caps_kinfo - Holds a client or service registration
93  *
94  * ci_msgpendq: holds the kernel copy of the message after it has been
95  *              sent to the local port.  The message is matched up against
96  *              replies and automatically replied if the owner closes its 
97  *              connection.
98  */
99 typedef struct caps_kinfo {
100         struct lwkt_port        ci_lport;       /* embedded local port */
101         struct caps_kinfo       *ci_tdnext;     /* per-process list */
102         struct caps_kinfo       *ci_hnext;      /* registration hash table */
103         struct thread           *ci_td;         /* owner */
104         struct caps_kmsg_queue  ci_msgpendq;    /* pending reply (just rcvd) */
105         struct caps_kmsg_queue  ci_msguserq;    /* pending reply (user holds) */
106         struct caps_kinfo       *ci_rcaps;      /* connected to remote */
107         int                     ci_cmsgcount;   /* client in-progress msgs */
108         int                     ci_id;
109         int                     ci_flags;
110         int                     ci_refs;
111         int                     ci_mrefs;       /* message (vmspace) refs */
112         caps_type_t             ci_type;
113         uid_t                   ci_uid;
114         gid_t                   ci_gid;
115         int                     ci_namelen;
116         char                    ci_name[4];     /* variable length */
117         /* ci_name must be last element */
118 } *caps_kinfo_t;
119
120 /* note: user flags are held in the low 16 bits */
121 #define CAPKF_TDLIST    0x00010000
122 #define CAPKF_HLIST     0x00020000
123 #define CAPKF_FLUSH     0x00040000
124 #define CAPKF_RCAPS     0x00080000
125 #define CAPKF_CLOSED    0x00100000
126 #define CAPKF_MWAIT     0x00200000
127
128 /*
129  * Kernel caps message.  The kernel keepps track of messagse received,
130  * undergoing processing by the service, and returned.  User-supplied data
131  * is copied on reception rather then transmission.
132  */
133 typedef struct caps_kmsg {
134         TAILQ_ENTRY(caps_kmsg)  km_node;
135         caps_kinfo_t            km_mcaps;       /* message sender */
136         void                    *km_umsg;       /* mcaps vmspace */
137         int                     km_umsg_size;   /* mcaps vmspace */
138         struct caps_cred        km_ccr;         /* caps cred for msg */
139         struct caps_msgid       km_msgid;
140         int                     km_flags;
141 } *caps_kmsg_t;
142
143 #define km_state        km_msgid.c_state
144
145 #define CAPKMF_ONUSERQ          0x0001
146 #define CAPKMF_ONPENDQ          0x0002
147 #define CAPKMF_REPLY            0x0004
148 #define CAPKMF_CDONE            0x0008
149 #define CAPKMF_PEEKED           0x0010
150 #define CAPKMF_ABORTED          0x0020
151
152 #endif
153
154 #ifdef _KERNEL
155
156 /*
157  * kernel support
158  */
159 void caps_exit(struct thread *td);
160 void caps_fork(struct proc *p1, struct proc *p2);
161
162 #else
163
164 /*
165  * Userland API (libcaps)
166  */
167 caps_port_t caps_service(const char *name, uid_t uid, gid_t gid, 
168                             mode_t modes, int flags);
169 caps_port_t caps_client(const char *name, uid_t uid, gid_t gid, int flags);
170
171 /*
172  * Syscall API
173  */
174 int caps_sys_service(const char *name, uid_t uid, gid_t gid, int upcid, int flags);
175 int caps_sys_client(const char *name, uid_t uid, gid_t gid, int upcid, int flags);
176 off_t caps_sys_put(int portid, void *msg, int msgsize);
177 int caps_sys_reply(int portid, void *msg, int msgsize, off_t msgcid);
178 int caps_sys_get(int portid, void *msg, int maxsize, caps_msgid_t msgid, caps_cred_t ccr);
179 int caps_sys_wait(int portid, void *msg, int maxsize, caps_msgid_t msgid, caps_cred_t ccr);
180 int caps_sys_abort(int portid, off_t msgcid, int flags);
181
182 #endif
183
184 #endif
185