xref: /illumos-gate/usr/src/cmd/lp/include/msgs.h (revision 6dde88b51419b99fe0aab8e56184c693945826b8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 # include	<sys/types.h>
30 # include	<poll.h>
31 # include	<stdarg.h>
32 # include	<stropts.h>
33 
34 #if	!defined(_LP_MSGS_H)
35 # define	_LP_MSGS_H
36 
37 /*
38  * THE DISPATCH TABLE DEPENDS ON EACH R_... MESSAGE FOLLOWING
39  * IMMEDIATELY AFTER ITS CORRESPONDING S_... COUNTERPART.
40  * I.E R_... MESSAGE FOR A S_... MESSAGE IS (S_... + 1)
41  */
42 # define	R_BAD_MESSAGE			0
43 /* # define	S_NEW_QUEUE			1	DEFUNCT */
44 /* # define	R_NEW_QUEUE			2	DEFUNCT */
45 # define	S_ALLOC_FILES			3
46 # define	R_ALLOC_FILES			4
47 # define	S_PRINT_REQUEST			5
48 # define	R_PRINT_REQUEST			6
49 # define	S_START_CHANGE_REQUEST		7
50 # define	R_START_CHANGE_REQUEST		8
51 # define	S_END_CHANGE_REQUEST		9
52 # define	R_END_CHANGE_REQUEST		10
53 # define	S_CANCEL_REQUEST		11
54 # define	R_CANCEL_REQUEST		12
55 /* # define	S_INQUIRE_REQUEST		13	DEFUNCT */
56 /* # define	R_INQUIRE_REQUEST		14	DEFUNCT */
57 # define	S_LOAD_PRINTER			15
58 # define	R_LOAD_PRINTER			16
59 # define	S_UNLOAD_PRINTER		17
60 # define	R_UNLOAD_PRINTER		18
61 # define	S_INQUIRE_PRINTER_STATUS	19
62 # define	R_INQUIRE_PRINTER_STATUS	20
63 # define	S_LOAD_CLASS			21
64 # define	R_LOAD_CLASS			22
65 # define	S_UNLOAD_CLASS			23
66 # define	R_UNLOAD_CLASS			24
67 # define	S_INQUIRE_CLASS			25
68 # define	R_INQUIRE_CLASS			26
69 # define	S_MOUNT				27
70 # define	R_MOUNT				28
71 # define	S_UNMOUNT			29
72 # define	R_UNMOUNT			30
73 # define	S_MOVE_REQUEST			31
74 # define	R_MOVE_REQUEST			32
75 # define	S_MOVE_DEST			33
76 # define	R_MOVE_DEST			34
77 # define	S_ACCEPT_DEST			35
78 # define	R_ACCEPT_DEST			36
79 # define	S_REJECT_DEST			37
80 # define	R_REJECT_DEST			38
81 # define	S_ENABLE_DEST			39
82 # define	R_ENABLE_DEST			40
83 # define	S_DISABLE_DEST			41
84 # define	R_DISABLE_DEST			42
85 # define	S_LOAD_FILTER_TABLE		43
86 # define	R_LOAD_FILTER_TABLE		44
87 # define	S_UNLOAD_FILTER_TABLE		45
88 # define	R_UNLOAD_FILTER_TABLE		46
89 # define	S_LOAD_PRINTWHEEL		47
90 # define	R_LOAD_PRINTWHEEL		48
91 # define	S_UNLOAD_PRINTWHEEL		49
92 # define	R_UNLOAD_PRINTWHEEL		50
93 # define	S_LOAD_USER_FILE		51
94 # define	R_LOAD_USER_FILE		52
95 # define	S_UNLOAD_USER_FILE		53
96 # define	R_UNLOAD_USER_FILE		54
97 # define	S_LOAD_FORM			55
98 # define	R_LOAD_FORM			56
99 # define	S_UNLOAD_FORM			57
100 # define	R_UNLOAD_FORM			58
101 /* # define	S_GETSTATUS			59	DEFUNCT */
102 /* # define	R_GETSTATUS			60	DEFUNCT */
103 # define	S_QUIET_ALERT			61
104 # define	R_QUIET_ALERT			62
105 # define	S_SEND_FAULT			63
106 # define	R_SEND_FAULT			64
107 # define	S_SHUTDOWN			65
108 # define	R_SHUTDOWN			66
109 # define	S_GOODBYE			67
110 # define	S_CHILD_DONE			68
111 
112 /*
113 **	These are for use by the scheduler only
114 */
115 # define	I_GET_TYPE			69
116 # define	I_QUEUE_CHK			70
117 /* # define	R_CONNECT			71	DEFUNCT */
118 
119 /* # define	S_GET_STATUS			72	DEFUNCT */
120 /* # define	R_GET_STATUS			73	DEFUNCT */
121 # define	S_INQUIRE_REQUEST_RANK		74
122 # define	R_INQUIRE_REQUEST_RANK		75
123 # define	S_CANCEL			76
124 # define	R_CANCEL			77
125 /* # define	S_NEW_CHILD			78	DEFUNCT */
126 /* # define	R_NEW_CHILD			79	DEFUNCT */
127 /* # define	S_SEND_JOB			80	DEFUNCT */
128 /* # define	R_SEND_JOB			81	DEFUNCT */
129 /* # define	S_JOB_COMPLETED			82	DEFUNCT */
130 /* # define	R_JOB_COMPLETED			83	DEFUNCT */
131 /* # define	S_INQUIRE_REMOTE_PRINTER	84	DEFUNCT */
132 /* # define	R_INQUIRE_REMOTE_PRINTER	20	DEFUNCT */
133 /* # define	S_CHILD_SYNC			85	DEFUNCT */
134 /* # define	S_LOAD_SYSTEM			86	DEFUNCT */
135 /* # define	R_LOAD_SYSTEM			87	DEFUNCT */
136 /* # define	S_UNLOAD_SYSTEM			88	DEFUNCT */
137 /* # define	R_UNLOAD_SYSTEM			89	DEFUNCT */
138 /* new messages */
139 # define	S_CLEAR_FAULT			90
140 # define	R_CLEAR_FAULT			91
141 # define	S_MOUNT_TRAY			92
142 # define	R_MOUNT_TRAY			93
143 # define	S_UNMOUNT_TRAY			94
144 # define	R_UNMOUNT_TRAY			95
145 # define	S_MAX_TRAYS			96
146 # define	R_MAX_TRAYS			97
147 # define	S_PAPER_CHANGED			98
148 # define	R_PAPER_CHANGED			99
149 # define	S_PAPER_ALLOWED			100
150 # define	R_PAPER_ALLOWED			101
151 # define	S_PASS_PEER_CONNECTION		102
152 # define	R_PASS_PEER_CONNECTION		103
153 /*
154 **	Last available message
155 */
156 # define	LAST_MESSAGE			104
157 
158 /*
159 **      These are the possible status codes returned by the scheduler
160 */
161 # define	MOK		 0
162 # define	MOKMORE		 1
163 # define	MOKREMOTE	 2
164 # define	MMORERR		 3
165 # define	MNODEST		 4
166 # define	MERRDEST	 5
167 # define	MDENYDEST	 6
168 # define	MNOMEDIA	 7
169 # define	MDENYMEDIA	 8
170 # define	MNOFILTER	 9
171 # define	MNOINFO		10
172 # define	MNOMEM		11
173 # define	MNOMOUNT	12
174 # define	MNOOPEN		13
175 # define	MNOPERM		14
176 # define	MNOSTART	15
177 # define	MUNKNOWN	16
178 # define	M2LATE		17
179 # define	MNOSPACE	18
180 # define	MBUSY		19
181 # define	MTRANSMITERR	20
182 # define	MNOMORE		21
183 # define	MGONEREMOTE	22
184 # define	MNOTRAY		23
185 
186 /*
187 ** Offsets and lengths of the various elements of the message header.
188 **
189 **	Macro		Data Type	Size	Comment
190 **
191 **	HEAD_RESYNC	2 bytes		(2)	*
192 **	HEAD_AUTHCODE	short + long	(6)	*
193 **
194 **	HEAD_SIZE	4 bytes		(4)	\
195 **	HEAD_TYPE	4 bytes		(4)	 > message propper
196 **	HEAD_DATA	n bytes		(n)	/
197 **
198 **	TAIL_CHKSUM	4 bytes		(4)	*
199 **	TAIL_ENDSYNC	2 bytes		(2)	*
200 **
201 **	Items marked with an asterisk are only used with the 3.2
202 **	Spooler protocol.
203 */
204 
205 /*
206 **	3.2 Protocol Header Information:
207 **		2-byte message introduction
208 **		6-byte client authorization data
209 */
210 #define	HEAD_RESYNC		(0)
211 #define HEAD_RESYNC_LEN		2
212 #define HEAD_AUTHCODE		(HEAD_RESYNC + HEAD_RESYNC_LEN)
213 #define HEAD_AUTHCODE_LEN		(sizeof(short) + sizeof(long))
214 
215 /*
216 **	3.2 Protocol Message Information:
217 **		4-byte message size
218 **		4-byte message type
219 **		n-byte message data
220 */
221 #define HEAD_SIZE		(HEAD_AUTHCODE + HEAD_AUTHCODE_LEN)
222 #define HEAD_SIZE_LEN			4
223 #define HEAD_TYPE		(HEAD_SIZE + HEAD_SIZE_LEN)
224 #define HEAD_TYPE_LEN			4
225 #define HEAD_DATA		(HEAD_TYPE + HEAD_TYPE_LEN)
226 
227 /*
228 **	3.2 Protocol Size of non-data header information
229 */
230 #define HEAD_LEN		HEAD_DATA
231 
232 /*
233 **	Equivalents for 4.0 protocol
234 */
235 #define MESG_SIZE		(0)
236 #define MESG_SIZE_LEN			4
237 #define MESG_TYPE		(MESG_SIZE + MESG_SIZE_LEN)
238 #define MESG_TYPE_LEN			4
239 #define MESG_DATA		(MESG_TYPE + MESG_TYPE_LEN)
240 
241 #define MESG_LEN		MESG_DATA
242 
243 /*
244 **	3.2 Protocol Trailer Information:
245 **		4-byte message check sum
246 **		2-byte message closing identifier
247 **
248 **	"N" is the decoded value of buffer[HEAD_SIZE].  This must
249 **	be provided because messages are variable length.
250 */
251 #define	TAIL_ENDSYNC_LEN		2
252 #define	TAIL_ENDSYNC(N)		(N - TAIL_ENDSYNC_LEN)
253 #define TAIL_CHKSUM_LEN			4
254 #define TAIL_CHKSUM(N)		(TAIL_ENDSYNC(N) - TAIL_CHKSUM_LEN)
255 
256 /*
257 **	3.2 Protocol Size of non-data trailer information
258 */
259 #define	TAIL_LEN		(TAIL_CHKSUM_LEN + TAIL_ENDSYNC_LEN)
260 
261 /*
262 **	3.2 Protocol Size of all non-data information
263 **	(This is also the minimum size for 3.2 protocol messages)
264 */
265 #define	CONTROL_LEN		(HEAD_LEN + TAIL_LEN)
266 
267 /*
268 **	Size of excess data induced by 3.2 Protocol.
269 **	(This is also the size differance between 3.2 & 4.0 protocols)
270 */
271 #define	EXCESS_3_2_LEN		(HEAD_SIZE + TAIL_LEN)
272 /**
273  ** Checksum:
274  **/
275 #define CALC_CHKSUM(B,SZ,RC) \
276 if (SZ >= CONTROL_LEN) \
277 { \
278     register unsigned char	*p = (unsigned char *)B, \
279 			    *pend = p + SZ - TAIL_LEN; \
280     RC = 0; \
281     while (p < pend) \
282 	RC += *p++;  /* let it overflow */ \
283 } \
284 else \
285     return ((errno = EINVAL, -1))
286 
287 /*
288 **      Largest size permitted for any given message
289 */
290 # define	MSGMAX		2048
291 
292 /*
293 **      Possible values of the type field of S_QUIET_ALERT
294 */
295 # define	QA_FORM		1
296 # define	QA_PRINTER	2
297 # define	QA_PRINTWHEEL	3
298 
299 typedef	struct	strbuf	strbuf_t;	/*  STREAMS buffer */
300 
301 typedef	struct mque
302 {
303     struct mque	  *next;
304     struct strbuf *dat;
305 } MQUE;
306 
307 /*
308 **	Definition of a message descriptor
309 */
310 typedef struct
311 {
312     short	type;			/* type of connection */
313     int		readfd;			/* STREAM fd to read from */
314     int		writefd;		/* STREAM fd to write to */
315     int		wait;			/* number of systems waiting for */
316     char	*file;			/* pipe name if type==MD_FIFO */
317     short	state;			/* Current state of client */
318     short	admin;			/* Non zero if admin  */
319     short	event;			/* Event returned from poll */
320     MQUE *	mque;			/* backlogged message ptr */
321     uid_t	uid;			/* Clients UID */
322     gid_t	gid;			/* Clients GID */
323     char *	slabel;			/* Clients SLABEL */
324     void	(**on_discon)();	/* Clean up functions */
325 } MESG;
326 
327 # define	MDSIZE	(sizeof(MESG))
328 
329 /*
330 **	Possible values of MESG.state
331 */
332 # define	MDS_IDLE	0
333 
334 # define	MDS_32PROTO	320
335 # define	MDS_32CONNECT	321
336 
337 /*
338 **	Possible values of MESG.type
339 */
340 # define	MD_UNKNOWN	0	/* We don't know just yet */
341 # define	MD_STREAM	1	/* 4.0 STREAMS pipe protocol */
342 # define	MD_BOUND	2	/* 4.0 STREAMS fd protocol */
343 # define	MD_SYS_FIFO	3	/* 3.2 named-pipe protocol */
344 # define	MD_USR_FIFO	4	/* 3.2 named-pipe protocol */
345 # define	MD_MASTER	5	/* MD_STREAM used by lpsched */
346 # define	MD_CHILD	6	/* MD_STREAM to a child process */
347 
348 /*
349 **	Definition for a FIFO buffer (used
350 **	in read_fifo.
351 */
352 typedef struct
353 {
354 	int	full;
355 	char	save [MSGMAX],
356 		*psave,
357 		*psave_end;
358 } fifobuffer_t;
359 
360 /*
361 **      Definitions for the rest of the world and lint
362 */
363 /*
364 **	Server functions in order of usage
365 */
366 MESG		* mcreate ( char * );
367 int		mlisteninit ( MESG * );
368 MESG		* mlisten ( void );
369 int		mlistenadd ( MESG *, short );
370 int		mon_discon ( MESG *, void (*)());
371 MESG		* mlistenreset ( void );
372 int		mdestroy ( MESG * );
373 
374 /*
375 **	Client functions in order of typical usage
376 */
377 MESG		* mconnect ( char *, int, int );
378 int		mgetm ( MESG *, int, ... );
379 int		mwrite ( MESG *, char * );
380 int		mputm ( MESG *, int, ... );
381 int		mread ( MESG *, char *, int );
382 short		msize ( char * );
383 short		mpeek ( MESG * );
384 int		mdisconnect ( MESG * );
385 
386 /*
387 **	This may be called to deallocate internal buffers allocated
388 **	by mgetm and mputm.  Probably not useful except right before
389 **	a fork().
390 */
391 void		__mbfree ( void );
392 
393 /*
394 **	Client functions for pre-4.0 compatability
395 */
396 int		mclose ( void );
397 int		mneeds ( void );
398 int		mopen ( void );
399 int		mrecv ( char *, int );
400 int		msend ( char * );
401 
402 int		Putmsg (MESG *, strbuf_t *, strbuf_t *, int);
403 int		Getmsg (MESG *, strbuf_t *, strbuf_t *, int *);
404 int		read3_2 (MESG * md, char *msgbuf, int size);
405 int		write3_2 (MESG *, char *, int);
406 int		read_fifo (int, char *, unsigned int);
407 int		write_fifo (int, char *, unsigned int);
408 int		ResetFifoBuffer (int);
409 fifobuffer_t	*GetFifoBuffer (int);
410 
411 /*
412 **	General purpose message manipulating functions
413 */
414 char		* htos ( char *, unsigned short );
415 char		* ltos ( char *, unsigned long );
416 unsigned long	stol ( char * );
417 unsigned short	stoh ( char * );
418 int		_getmessage ( char *, short, va_list );
419 int		_putmessage ( char *, short, va_list );
420 int		getmessage ( char *, short, ... );
421 int		putmessage ( char *, short, ... );
422 
423 /*
424 **	This will yield the type of a message
425 */
426 # define	mtype(buffer)	(getmessage(buffer, I_GET_TYPE))
427 
428 /*
429 **	This will yeild the size of a message
430 */
431 # define	msize(buffer)	(stoh(buffer))
432 
433 /*
434 **	Pass this for the request-id argument of S_CANCEL
435 **	to obtain the effect of the 3.2 S_CANCEL_REQUEST.
436 */
437 # define	CURRENT_REQ	"current"
438 
439 #endif	/* !defined (_LP_MSGS_H) */
440