]> granicus.if.org Git - strace/blob - stream.c
2005-05-31 Dmitry V. Levin <ldv@altlinux.org>
[strace] / stream.c
1 /*
2  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
3  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  *      $Id$
29  */
30
31 #include "defs.h"
32 #include <sys/syscall.h>
33
34 #ifdef HAVE_POLL_H
35 #include <poll.h>
36 #endif
37 #ifdef HAVE_SYS_POLL_H
38 #include <sys/poll.h>
39 #endif
40 #ifdef HAVE_STROPTS_H
41 #include <stropts.h>
42 #endif
43 #ifdef HAVE_SYS_CONF_H
44 #include <sys/conf.h>
45 #endif
46 #ifdef HAVE_SYS_STREAM_H
47 #include <sys/stream.h>
48 #endif
49 #ifdef HAVE_SYS_TIHDR_H
50 #include <sys/tihdr.h>
51 #endif
52
53 #if defined(HAVE_SYS_STREAM_H) || defined(LINUX) || defined(FREEBSD)
54
55 #ifndef HAVE_STROPTS_H
56 #define RS_HIPRI 1
57 struct strbuf {
58         int     maxlen;                 /* no. of bytes in buffer */
59         int     len;                    /* no. of bytes returned */
60         char    *buf;                   /* pointer to data */
61 };
62 #define MORECTL 1
63 #define MOREDATA 2
64 #endif /* !HAVE_STROPTS_H */
65
66 #ifdef HAVE_SYS_TIUSER_H
67 #include <sys/tiuser.h>
68 #include <sys/sockmod.h>
69 #include <sys/timod.h>
70 #endif /* HAVE_SYS_TIUSER_H */
71
72 #ifndef FREEBSD
73 static const struct xlat msgflags[] = {
74         { RS_HIPRI,     "RS_HIPRI"      },
75         { 0,            NULL            },
76 };
77
78
79 static void
80 printstrbuf(tcp, sbp, getting)
81 struct tcb *tcp;
82 struct strbuf *sbp;
83 int getting;
84 {
85         if (sbp->maxlen == -1 && getting)
86                 tprintf("{maxlen=-1}");
87         else {
88                 tprintf("{");
89                 if (getting)
90                         tprintf("maxlen=%d, ", sbp->maxlen);
91                 tprintf("len=%d, buf=", sbp->len);
92                 printstr(tcp, (unsigned long) sbp->buf, sbp->len);
93                 tprintf("}");
94         }
95 }
96
97 static void
98 printstrbufarg(tcp, arg, getting)
99 struct tcb *tcp;
100 int arg;
101 int getting;
102 {
103         struct strbuf buf;
104
105         if (arg == 0)
106                 tprintf("NULL");
107         else if (umove(tcp, arg, &buf) < 0)
108                 tprintf("{...}");
109         else
110                 printstrbuf(tcp, &buf, getting);
111         tprintf(", ");
112 }
113
114 int
115 sys_putmsg(tcp)
116 struct tcb *tcp;
117 {
118         int i;
119
120         if (entering(tcp)) {
121                 /* fd */
122                 tprintf("%ld, ", tcp->u_arg[0]);
123                 /* control and data */
124                 for (i = 1; i < 3; i++)
125                         printstrbufarg(tcp, tcp->u_arg[i], 0);
126                 /* flags */
127                 printflags(msgflags, tcp->u_arg[3], "RS_???");
128         }
129         return 0;
130 }
131
132 int
133 sys_getmsg(tcp)
134 struct tcb *tcp;
135 {
136         int i, flags;
137
138         if (entering(tcp)) {
139                 /* fd */
140                 tprintf("%lu, ", tcp->u_arg[0]);
141         } else {
142                 if (syserror(tcp)) {
143                         tprintf("%#lx, %#lx, %#lx",
144                                 tcp->u_arg[1], tcp->u_arg[2], tcp->u_arg[3]);
145                         return 0;
146                 }
147                 /* control and data */
148                 for (i = 1; i < 3; i++)
149                         printstrbufarg(tcp, tcp->u_arg[i], 1);
150                 /* pointer to flags */
151                 if (tcp->u_arg[3] == 0)
152                         tprintf("NULL");
153                 else if (umove(tcp, tcp->u_arg[3], &flags) < 0)
154                         tprintf("[?]");
155                 else {
156                         tprintf("[");
157                         printflags(msgflags, flags, "RS_???");
158                         tprintf("]");
159                 }
160                 /* decode return value */
161                 switch (tcp->u_rval) {
162                 case MORECTL:
163                         tcp->auxstr = "MORECTL";
164                         break;
165                 case MORECTL|MOREDATA:
166                         tcp->auxstr = "MORECTL|MOREDATA";
167                         break;
168                 case MOREDATA:
169                         tcp->auxstr = "MORECTL";
170                         break;
171                 default:
172                         tcp->auxstr = NULL;
173                         break;
174                 }
175         }
176         return RVAL_HEX | RVAL_STR;
177 }
178
179 #if defined SYS_putpmsg || defined SYS_getpmsg
180 static const struct xlat pmsgflags[] = {
181 #ifdef MSG_HIPRI
182         { MSG_HIPRI,    "MSG_HIPRI"     },
183 #endif
184 #ifdef MSG_AND
185         { MSG_ANY,      "MSG_ANY"       },
186 #endif
187 #ifdef MSG_BAND
188         { MSG_BAND,     "MSG_BAND"      },
189 #endif
190         { 0,            NULL            },
191 };
192 #endif
193
194 #ifdef SYS_putpmsg
195 int
196 sys_putpmsg(tcp)
197 struct tcb *tcp;
198 {
199         int i;
200
201         if (entering(tcp)) {
202                 /* fd */
203                 tprintf("%ld, ", tcp->u_arg[0]);
204                 /* control and data */
205                 for (i = 1; i < 3; i++)
206                         printstrbufarg(tcp, tcp->u_arg[i], 0);
207                 /* band */
208                 tprintf("%ld, ", tcp->u_arg[3]);
209                 /* flags */
210                 printflags(pmsgflags, tcp->u_arg[4], "MSG_???");
211         }
212         return 0;
213 }
214 #endif /* SYS_putpmsg */
215
216 #ifdef SYS_getpmsg
217 int
218 sys_getpmsg(tcp)
219 struct tcb *tcp;
220 {
221         int i, flags;
222
223         if (entering(tcp)) {
224                 /* fd */
225                 tprintf("%lu, ", tcp->u_arg[0]);
226         } else {
227                 if (syserror(tcp)) {
228                         tprintf("%#lx, %#lx, %#lx, %#lx", tcp->u_arg[1],
229                                 tcp->u_arg[2], tcp->u_arg[3], tcp->u_arg[4]);
230                         return 0;
231                 }
232                 /* control and data */
233                 for (i = 1; i < 3; i++)
234                         printstrbufarg(tcp, tcp->u_arg[i], 1);
235                 /* pointer to band */
236                 printnum(tcp, tcp->u_arg[3], "%d");
237                 tprintf(", ");
238                 /* pointer to flags */
239                 if (tcp->u_arg[4] == 0)
240                         tprintf("NULL");
241                 else if (umove(tcp, tcp->u_arg[4], &flags) < 0)
242                         tprintf("[?]");
243                 else {
244                         tprintf("[");
245                         printflags(pmsgflags, flags, "MSG_???");
246                         tprintf("]");
247                 }
248                 /* decode return value */
249                 switch (tcp->u_rval) {
250                 case MORECTL:
251                         tcp->auxstr = "MORECTL";
252                         break;
253                 case MORECTL|MOREDATA:
254                         tcp->auxstr = "MORECTL|MOREDATA";
255                         break;
256                 case MOREDATA:
257                         tcp->auxstr = "MORECTL";
258                         break;
259                 default:
260                         tcp->auxstr = NULL;
261                         break;
262                 }
263         }
264         return RVAL_HEX | RVAL_STR;
265 }
266 #endif /* SYS_getpmsg */
267
268 #endif /* !FREEBSD */
269
270
271 #ifdef HAVE_SYS_POLL_H
272
273 static const struct xlat pollflags[] = {
274 #ifdef POLLIN
275         { POLLIN,       "POLLIN"        },
276         { POLLPRI,      "POLLPRI"       },
277         { POLLOUT,      "POLLOUT"       },
278 #ifdef POLLRDNORM
279         { POLLRDNORM,   "POLLRDNORM"    },
280 #endif
281 #ifdef POLLWRNORM
282         { POLLWRNORM,   "POLLWRNORM"    },
283 #endif
284 #ifdef POLLRDBAND
285         { POLLRDBAND,   "POLLRDBAND"    },
286 #endif
287 #ifdef POLLWRBAND
288         { POLLWRBAND,   "POLLWRBAND"    },
289 #endif
290         { POLLERR,      "POLLERR"       },
291         { POLLHUP,      "POLLHUP"       },
292         { POLLNVAL,     "POLLNVAL"      },
293 #endif
294         { 0,            NULL            },
295 };
296
297 int
298 sys_poll(tcp)
299 struct tcb *tcp;
300 {
301         struct pollfd *pollp;
302
303         if (exiting(tcp)) {
304                 int i;
305                 int nfds = tcp->u_arg[1];
306
307                 if (nfds <= 0) {
308                         tprintf("%#lx, %d, %ld\n",
309                                 tcp->u_arg[0], nfds, tcp->u_arg[2]);
310                         return 0;
311                 }
312                 pollp = (struct pollfd *) malloc(nfds * sizeof(*pollp));
313                 if (pollp == NULL) {
314                         fprintf(stderr, "sys_poll: no memory\n");
315                         tprintf("%#lx, %d, %ld",
316                                 tcp->u_arg[0], nfds, tcp->u_arg[2]);
317                         return 0;
318                 }
319                 if (umoven(tcp, tcp->u_arg[0],
320                            (nfds * sizeof(*pollp)), (char *) pollp) < 0) {
321                         tprintf("%#lx", tcp->u_arg[0]);
322                 }
323                 else {
324                         tprintf("[");
325                         for (i = 0; i < nfds; i++) {
326                                 if (i)
327                                         tprintf(", ");
328                                 if (pollp[i].fd < 0) {
329                                         tprintf("{fd=%d}", pollp[i].fd);
330                                         continue;
331                                 }
332                                 tprintf("{fd=%d, events=", pollp[i].fd);
333                                 printflags(pollflags, pollp[i].events,
334                                            "POLL???");
335                                 if (!syserror(tcp) && pollp[i].revents) {
336                                         tprintf(", revents=");
337                                         printflags(pollflags, pollp[i].revents,
338                                                    "POLL???");
339                                 }
340                                 tprintf("}");
341                         }
342                         tprintf("]");
343                 }
344                 tprintf(", %d, ", nfds);
345 #ifdef INFTIM
346                 if (tcp->u_arg[2] == INFTIM)
347                         tprintf("INFTIM");
348                 else
349 #endif
350                         tprintf("%ld", tcp->u_arg[2]);
351                 free(pollp);
352         }
353         return 0;
354 }
355
356 #else /* !HAVE_SYS_POLL_H */
357 int
358 sys_poll(tcp)
359 struct tcb *tcp;
360 {
361         return 0;
362 }
363 #endif
364
365 #if !defined(LINUX) && !defined(FREEBSD)
366
367 static const struct xlat stream_flush_options[] = {
368         { FLUSHR,       "FLUSHR"        },
369         { FLUSHW,       "FLUSHW"        },
370         { FLUSHRW,      "FLUSHRW"       },
371 #ifdef FLUSHBAND
372         { FLUSHBAND,    "FLUSHBAND"     },
373 #endif
374         { 0,            NULL            },
375 };
376
377 static const struct xlat stream_setsig_flags[] = {
378         { S_INPUT,      "S_INPUT"       },
379         { S_HIPRI,      "S_HIPRI"       },
380         { S_OUTPUT,     "S_OUTPUT"      },
381         { S_MSG,        "S_MSG"         },
382 #ifdef S_ERROR
383         { S_ERROR,      "S_ERROR"       },
384 #endif
385 #ifdef S_HANGUP
386         { S_HANGUP,     "S_HANGUP"      },
387 #endif
388 #ifdef S_RDNORM
389         { S_RDNORM,     "S_RDNORM"      },
390 #endif
391 #ifdef S_WRNORM
392         { S_WRNORM,     "S_WRNORM"      },
393 #endif
394 #ifdef S_RDBAND
395         { S_RDBAND,     "S_RDBAND"      },
396 #endif
397 #ifdef S_WRBAND
398         { S_WRBAND,     "S_WRBAND"      },
399 #endif
400 #ifdef S_BANDURG
401         { S_BANDURG,    "S_BANDURG"     },
402 #endif
403         { 0,            NULL            },
404 };
405
406 static const struct xlat stream_read_options[] = {
407         { RNORM,        "RNORM"         },
408         { RMSGD,        "RMSGD"         },
409         { RMSGN,        "RMSGN"         },
410         { 0,            NULL            },
411 };
412
413 static const struct xlat stream_read_flags[] = {
414 #ifdef RPROTDAT
415         { RPROTDAT,     "RPROTDAT"      },
416 #endif
417 #ifdef RPROTDIS
418         { RPROTDIS,     "RPROTDIS"      },
419 #endif
420 #ifdef RPROTNORM
421         { RPROTNORM,    "RPROTNORM"     },
422 #endif
423         { 0,            NULL            },
424 };
425
426 #ifndef RMODEMASK
427 #define RMODEMASK (~0)
428 #endif
429
430 #ifdef I_SWROPT
431 static const struct xlat stream_write_flags[] = {
432         { SNDZERO,      "SNDZERO"       },
433         { SNDPIPE,      "SNDPIPE"       },
434         { 0,            NULL            },
435 };
436 #endif /* I_SWROPT */
437
438 #ifdef I_ATMARK
439 static const struct xlat stream_atmark_options[] = {
440         { ANYMARK,      "ANYMARK"       },
441         { LASTMARK,     "LASTMARK"      },
442         { 0,            NULL            },
443 };
444 #endif /* I_ATMARK */
445
446 #ifdef TI_BIND
447 static const struct xlat transport_user_options[] = {
448         { T_CONN_REQ,   "T_CONN_REQ"    },
449         { T_CONN_RES,   "T_CONN_RES"    },
450         { T_DISCON_REQ, "T_DISCON_REQ"  },
451         { T_DATA_REQ,   "T_DATA_REQ"    },
452         { T_EXDATA_REQ, "T_EXDATA_REQ"  },
453         { T_INFO_REQ,   "T_INFO_REQ"    },
454         { T_BIND_REQ,   "T_BIND_REQ"    },
455         { T_UNBIND_REQ, "T_UNBIND_REQ"  },
456         { T_UNITDATA_REQ,"T_UNITDATA_REQ"},
457         { T_OPTMGMT_REQ,"T_OPTMGMT_REQ" },
458         { T_ORDREL_REQ, "T_ORDREL_REQ"  },
459         { 0,            NULL            },
460 };
461
462 static const struct xlat transport_user_flags [] = {
463         { 0,            "0"             },
464         { T_MORE,       "T_MORE"        },
465         { T_EXPEDITED,  "T_EXPEDITED"   },
466         { T_NEGOTIATE,  "T_NEGOTIATE"   },
467         { T_CHECK,      "T_CHECK"       },
468         { T_DEFAULT,    "T_DEFAULT"     },
469         { T_SUCCESS,    "T_SUCCESS"     },
470         { T_FAILURE,    "T_FAILURE"     },
471         { T_CURRENT,    "T_CURRENT"     },
472         { T_PARTSUCCESS,"T_PARTSUCCESS" },
473         { T_READONLY,   "T_READONLY"    },
474         { T_NOTSUPPORT, "T_NOTSUPPORT"  },
475         { 0,            NULL            },
476 };
477
478
479 #ifdef HAVE_STRUCT_T_OPTHDR
480
481 static const struct xlat xti_level [] = {
482         { XTI_GENERIC,  "XTI_GENERIC"   },
483         { 0,            NULL            },
484 };
485
486 static const struct xlat xti_generic [] = {
487         { XTI_DEBUG,    "XTI_DEBUG"     },
488         { XTI_LINGER,   "XTI_LINGER"    },
489         { XTI_RCVBUF,   "XTI_RCVBUF"    },
490         { XTI_RCVLOWAT, "XTI_RCVLOWAT"  },
491         { XTI_SNDBUF,   "XTI_SNDBUF"    },
492         { XTI_SNDLOWAT, "XTI_SNDLOWAT"  },
493         { 0,            NULL            },
494 };
495
496
497
498 void
499 print_xti_optmgmt (tcp, addr, len)
500 struct tcb *tcp;
501 long addr;
502 int len;
503 {
504         int c = 0;
505         struct t_opthdr hdr;
506
507         while (len >= (int) sizeof hdr) {
508                 if (umove(tcp, addr, &hdr) < 0) break;
509                 if (c++) {
510                         tprintf (", ");
511                 }
512                 else if (len > hdr.len + sizeof hdr) {
513                         tprintf ("[");
514                 }
515                 tprintf ("{level=");
516                 printxval (xti_level, hdr.level, "???");
517                 tprintf (", name=");
518                 switch (hdr.level) {
519                     case XTI_GENERIC:
520                         printxval (xti_generic, hdr.name, "XTI_???");
521                         break;
522                     default:
523                         tprintf ("%ld", hdr.name);
524                         break;
525                 }
526                 tprintf (", status=");
527                 printxval (transport_user_flags,  hdr.status, "T_???");
528                 addr += sizeof hdr;
529                 len -= sizeof hdr;
530                 if ((hdr.len -= sizeof hdr) > 0) {
531                         if (hdr.len > len) break;
532                         tprintf (", val=");
533                         if (len == sizeof (int))
534                                 printnum (tcp, addr, "%d");
535                         else
536                                 printstr (tcp, addr, hdr.len);
537                         addr += hdr.len;
538                         len -= hdr.len;
539                 }
540                 tprintf ("}");
541         }
542         if (len > 0) {
543                 if (c++) tprintf (", ");
544                 printstr (tcp, addr, len);
545         }
546         if (c > 1) tprintf ("]");
547 }
548
549 #endif
550
551
552 static void
553 print_optmgmt (tcp, addr, len)
554 struct tcb *tcp;
555 long addr;
556 int len;
557 {
558         /* We don't know how to tell if TLI (socket) or XTI
559            optmgmt is being used yet, assume TLI. */
560 #if defined (HAVE_STRUCT_OPTHDR)
561         print_sock_optmgmt (tcp, addr, len);
562 #elif defined (HAVE_STRUCT_T_OPTHDR)
563         print_xti_optmgmt (tcp, addr, len);
564 #else
565         printstr (tcp, addr, len);
566 #endif
567 }
568
569
570
571
572 static const struct xlat service_type [] = {
573         { T_COTS,       "T_COTS"        },
574         { T_COTS_ORD,   "T_COTS_ORD"    },
575         { T_CLTS,       "T_CLTS"        },
576         { 0,            NULL            },
577 };
578
579 static const struct xlat ts_state [] = {
580         { TS_UNBND,     "TS_UNBND"      },
581         { TS_WACK_BREQ, "TS_WACK_BREQ"  },
582         { TS_WACK_UREQ, "TS_WACK_UREQ"  },
583         { TS_IDLE,      "TS_IDLE"       },
584         { TS_WACK_OPTREQ,"TS_WACK_OPTREQ"},
585         { TS_WACK_CREQ, "TS_WACK_CREQ"  },
586         { TS_WCON_CREQ, "TS_WCON_CREQ"  },
587         { TS_WRES_CIND, "TS_WRES_CIND"  },
588         { TS_WACK_CRES, "TS_WACK_CRES"  },
589         { TS_DATA_XFER, "TS_DATA_XFER"  },
590         { TS_WIND_ORDREL,"TS_WIND_ORDREL"},
591         { TS_WREQ_ORDREL,"TS_WREQ_ORDREL"},
592         { TS_WACK_DREQ6,"TS_WACK_DREQ6" },
593         { TS_WACK_DREQ7,"TS_WACK_DREQ7" },
594         { TS_WACK_DREQ9,"TS_WACK_DREQ9" },
595         { TS_WACK_DREQ10,"TS_WACK_DREQ10"},
596         { TS_WACK_DREQ11,"TS_WACK_DREQ11"},
597         { 0,            NULL            },
598 };
599
600 static const struct xlat provider_flags [] = {
601         { 0,            "0"             },
602         { SENDZERO,     "SENDZERO"      },
603         { EXPINLINE,    "EXPINLINE"     },
604         { XPG4_1,       "XPG4_1"        },
605         { 0,            NULL            },
606 };
607
608
609 static const struct xlat tli_errors [] = {
610         { TBADADDR,     "TBADADDR"      },
611         { TBADOPT,      "TBADOPT"       },
612         { TACCES,       "TACCES"        },
613         { TBADF,        "TBADF"         },
614         { TNOADDR,      "TNOADDR"       },
615         { TOUTSTATE,    "TOUTSTATE"     },
616         { TBADSEQ,      "TBADSEQ"       },
617         { TSYSERR,      "TSYSERR"       },
618         { TLOOK,        "TLOOK"         },
619         { TBADDATA,     "TBADDATA"      },
620         { TBUFOVFLW,    "TBUFOVFLW"     },
621         { TFLOW,        "TFLOW"         },
622         { TNODATA,      "TNODATA"       },
623         { TNODIS,       "TNODIS"        },
624         { TNOUDERR,     "TNOUDERR"      },
625         { TBADFLAG,     "TBADFLAG"      },
626         { TNOREL,       "TNOREL"        },
627         { TNOTSUPPORT,  "TNOTSUPPORT"   },
628         { TSTATECHNG,   "TSTATECHNG"    },
629         { TNOSTRUCTYPE, "TNOSTRUCTYPE"  },
630         { TBADNAME,     "TBADNAME"      },
631         { TBADQLEN,     "TBADQLEN"      },
632         { TADDRBUSY,    "TADDRBUSY"     },
633         { TINDOUT,      "TINDOUT"       },
634         { TPROVMISMATCH,"TPROVMISMATCH" },
635         { TRESQLEN,     "TRESQLEN"      },
636         { TRESADDR,     "TRESADDR"      },
637         { TQFULL,       "TQFULL"        },
638         { TPROTO,       "TPROTO"        },
639         { 0,            NULL            },
640 };
641
642
643 static int
644 print_transport_message (tcp, expect, addr, len)
645 struct tcb *tcp;
646 int expect;
647 long addr;
648 int len;
649 {
650         union T_primitives m;
651         int c = 0;
652
653         if (len < sizeof m.type) goto dump;
654
655         if (umove (tcp, addr, &m.type) < 0) goto dump;
656
657 #define GET(type, struct)       \
658         do {                                                    \
659                 if (len < sizeof m.struct) goto dump;           \
660                 if (umove (tcp, addr, &m.struct) < 0) goto dump;\
661                 tprintf ("{");                                  \
662                 if (expect != type) {                           \
663                         ++c;                                    \
664                         tprintf (#type);                        \
665                 }                                               \
666         }                                                       \
667         while (0)
668
669 #define COMMA() \
670         do { if (c++) tprintf (", "); } while (0)
671
672
673 #define STRUCT(struct, elem, print)                                     \
674         do {                                                            \
675                 COMMA ();                                               \
676                 if (m.struct.elem##_length < 0 ||                       \
677                     m.struct.elem##_offset < sizeof m.struct ||         \
678                     m.struct.elem##_offset + m.struct.elem##_length > len) \
679                 {                                                       \
680                         tprintf (#elem "_length=%ld, " #elem "_offset=%ld",\
681                                 m.struct.elem##_length,                 \
682                                 m.struct.elem##_offset);                \
683                 }                                                       \
684                 else {                                                  \
685                         tprintf (#elem "=");                            \
686                         print (tcp,                                     \
687                                  addr + m.struct.elem##_offset,         \
688                                  m.struct.elem##_length);               \
689                 }                                                       \
690         }                                                               \
691         while (0)
692
693 #define ADDR(struct, elem) STRUCT (struct, elem, printstr)
694
695         switch (m.type) {
696 #ifdef T_CONN_REQ
697             case T_CONN_REQ:    /* connect request   */
698                 GET (T_CONN_REQ, conn_req);
699                 ADDR (conn_req, DEST);
700                 ADDR (conn_req, OPT);
701                 break;
702 #endif
703 #ifdef T_CONN_RES
704             case T_CONN_RES:    /* connect response   */
705                 GET (T_CONN_RES, conn_res);
706 #ifdef HAVE_STRUCT_T_CONN_RES_QUEUE_PTR
707                 COMMA ();
708                 tprintf ("QUEUE=%p", m.conn_res.QUEUE_ptr);
709 #elif defined HAVE_STRUCT_T_CONN_RES_ACCEPTOR_ID
710                 COMMA ();
711                 tprintf ("ACCEPTOR=%#lx", m.conn_res.ACCEPTOR_id);
712 #endif
713                 ADDR (conn_res, OPT);
714                 COMMA ();
715                 tprintf ("SEQ=%ld", m.conn_res.SEQ_number);
716                 break;
717 #endif
718 #ifdef T_DISCON_REQ
719             case T_DISCON_REQ:  /* disconnect request */
720                 GET (T_DISCON_REQ, discon_req);
721                 COMMA ();
722                 tprintf ("SEQ=%ld", m.discon_req.SEQ_number);
723                 break;
724 #endif
725 #ifdef T_DATA_REQ
726             case T_DATA_REQ:    /* data request       */
727                 GET (T_DATA_REQ, data_req);
728                 COMMA ();
729                 tprintf ("MORE=%ld", m.data_req.MORE_flag);
730                 break;
731 #endif
732 #ifdef T_EXDATA_REQ
733             case T_EXDATA_REQ:  /* expedited data req */
734                 GET (T_EXDATA_REQ, exdata_req);
735                 COMMA ();
736                 tprintf ("MORE=%ld", m.exdata_req.MORE_flag);
737                 break;
738 #endif
739 #ifdef T_INFO_REQ
740             case T_INFO_REQ:    /* information req    */
741                 GET (T_INFO_REQ, info_req);
742                 break;
743 #endif
744 #ifdef T_BIND_REQ
745             case T_BIND_REQ:    /* bind request       */
746 #ifdef O_T_BIND_REQ
747             case O_T_BIND_REQ:  /* Ugly xti/tli hack */
748 #endif
749                 GET (T_BIND_REQ, bind_req);
750                 ADDR (bind_req, ADDR);
751                 COMMA ();
752                 tprintf ("CONIND=%ld", m.bind_req.CONIND_number);
753                 break;
754 #endif
755 #ifdef T_UNBIND_REQ
756             case T_UNBIND_REQ:  /* unbind request     */
757                 GET (T_UNBIND_REQ, unbind_req);
758                 break;
759 #endif
760 #ifdef T_UNITDATA_REQ
761             case T_UNITDATA_REQ:        /* unitdata requset   */
762                 GET (T_UNITDATA_REQ, unitdata_req);
763                 ADDR (unitdata_req, DEST);
764                 ADDR (unitdata_req, OPT);
765                 break;
766 #endif
767 #ifdef T_OPTMGMT_REQ
768             case T_OPTMGMT_REQ: /* manage opt req     */
769                 GET (T_OPTMGMT_REQ, optmgmt_req);
770                 COMMA ();
771                 tprintf ("MGMT=");
772                 printflags (transport_user_flags, m.optmgmt_req.MGMT_flags,
773                             "T_???");
774                 STRUCT (optmgmt_req, OPT, print_optmgmt);
775                 break;
776 #endif
777 #ifdef T_ORDREL_REQ
778             case T_ORDREL_REQ:  /* orderly rel req    */
779                 GET (T_ORDREL_REQ, ordrel_req);
780                 break;
781 #endif
782 #ifdef T_CONN_IND
783             case T_CONN_IND:    /* connect indication */
784                 GET (T_CONN_IND, conn_ind);
785                 ADDR (conn_ind, SRC);
786                 ADDR (conn_ind, OPT);
787                 tprintf (", SEQ=%ld", m.conn_ind.SEQ_number);
788                 break;
789 #endif
790 #ifdef T_CONN_CON
791             case T_CONN_CON:    /* connect corfirm    */
792                 GET (T_CONN_CON, conn_con);
793                 ADDR (conn_con, RES);
794                 ADDR (conn_con, OPT);
795                 break;
796 #endif
797 #ifdef T_DISCON_IND
798             case T_DISCON_IND:  /* discon indication  */
799                 GET (T_DISCON_IND, discon_ind);
800                 COMMA ();
801                 tprintf ("DISCON=%ld, SEQ=%ld",
802                          m.discon_ind.DISCON_reason, m.discon_ind.SEQ_number);
803                 break;
804 #endif
805 #ifdef T_DATA_IND
806             case T_DATA_IND:    /* data indication    */
807                 GET (T_DATA_IND, data_ind);
808                 COMMA ();
809                 tprintf ("MORE=%ld", m.data_ind.MORE_flag);
810                 break;
811 #endif
812 #ifdef T_EXDATA_IND
813             case T_EXDATA_IND:  /* expedited data ind */
814                 GET (T_EXDATA_IND, exdata_ind);
815                 COMMA ();
816                 tprintf ("MORE=%ld", m.exdata_ind.MORE_flag);
817                 break;
818 #endif
819 #ifdef T_INFO_ACK
820             case T_INFO_ACK:    /* info ack           */
821                 GET (T_INFO_ACK, info_ack);
822                 COMMA ();
823                 tprintf ("TSDU=%ld, ETSDU=%ld, CDATA=%ld, DDATA=%ld, "
824                          "ADDR=%ld, OPT=%ld, TIDU=%ld, SERV=",
825                          m.info_ack.TSDU_size, m.info_ack.ETSDU_size,
826                          m.info_ack.CDATA_size, m.info_ack.DDATA_size,
827                          m.info_ack.ADDR_size, m.info_ack.OPT_size,
828                          m.info_ack.TIDU_size);
829                 printxval (service_type, m.info_ack.SERV_type, "T_???");
830                 tprintf (", CURRENT=");
831                 printxval (ts_state, m.info_ack.CURRENT_state, "TS_???");
832                 tprintf (", PROVIDER=");
833                 printflags (provider_flags, m.info_ack.PROVIDER_flag, "???");
834                 break;
835 #endif
836 #ifdef T_BIND_ACK
837             case T_BIND_ACK:    /* bind ack           */
838                 GET (T_BIND_ACK, bind_ack);
839                 ADDR (bind_ack, ADDR);
840                 tprintf (", CONIND=%ld", m.bind_ack.CONIND_number);
841                 break;
842 #endif
843 #ifdef T_ERROR_ACK
844             case T_ERROR_ACK:   /* error ack          */
845                 GET (T_ERROR_ACK, error_ack);
846                 COMMA ();
847                 tprintf ("ERROR=");
848                 printxval (transport_user_options,
849                            m.error_ack.ERROR_prim, "TI_???");
850                 tprintf (", TLI=");
851                 printxval (tli_errors, m.error_ack.TLI_error, "T???");
852                 tprintf ("UNIX=%s", strerror (m.error_ack.UNIX_error));
853                 break;
854 #endif
855 #ifdef T_OK_ACK
856             case T_OK_ACK:      /* ok ack             */
857                 GET (T_OK_ACK, ok_ack);
858                 COMMA ();
859                 tprintf ("CORRECT=");
860                 printxval (transport_user_options,
861                            m.ok_ack.CORRECT_prim, "TI_???");
862                 break;
863 #endif
864 #ifdef T_UNITDATA_IND
865             case T_UNITDATA_IND:        /* unitdata ind       */
866                 GET (T_UNITDATA_IND, unitdata_ind);
867                 ADDR (unitdata_ind, SRC);
868                 ADDR (unitdata_ind, OPT);
869                 break;
870 #endif
871 #ifdef T_UDERROR_IND
872             case T_UDERROR_IND: /* unitdata error ind */
873                 GET (T_UDERROR_IND, uderror_ind);
874                 ADDR (uderror_ind, DEST);
875                 ADDR (uderror_ind, OPT);
876                 tprintf (", ERROR=%ld", m.uderror_ind.ERROR_type);
877                 break;
878 #endif
879 #ifdef T_OPTMGMT_ACK
880             case T_OPTMGMT_ACK: /* manage opt ack     */
881                 GET (T_OPTMGMT_ACK, optmgmt_ack);
882                 COMMA ();
883                 tprintf ("MGMT=");
884                 printflags (transport_user_flags, m.optmgmt_ack.MGMT_flags,
885                             "T_???");
886                 STRUCT (optmgmt_ack, OPT, print_optmgmt);
887                 break;
888 #endif
889 #ifdef T_ORDREL_IND
890         case T_ORDREL_IND:      /* orderly rel ind    */
891                 GET (T_ORDREL_IND, ordrel_ind);
892                 break;
893 #endif
894 #ifdef T_ADDR_REQ
895             case T_ADDR_REQ:    /* address req        */
896                 GET (T_ADDR_REQ, addr_req);
897                 break;
898 #endif
899 #ifdef T_ADDR_ACK
900             case T_ADDR_ACK:    /* address response   */
901                 GET (T_ADDR_ACK, addr_ack);
902                 ADDR (addr_ack, LOCADDR);
903                 ADDR (addr_ack, REMADDR);
904                 break;
905 #endif
906             default:
907             dump:
908                 c = -1;
909                 printstr(tcp, addr, len);
910                 break;
911         }
912
913         if (c >= 0) tprintf ("}");
914
915 #undef ADDR
916 #undef COMMA
917 #undef STRUCT
918
919         return 0;
920 }
921
922
923 #endif /* TI_BIND */
924
925
926 static int
927 internal_stream_ioctl(tcp, arg)
928 struct tcb *tcp;
929 int arg;
930 {
931         struct strioctl si;
932         struct ioctlent *iop;
933         int in_and_out;
934         int timod = 0;
935 #ifdef SI_GETUDATA
936         struct si_udata udata;
937 #endif /* SI_GETUDATA */
938
939         if (!arg)
940                 return 0;
941         if (umove(tcp, arg, &si) < 0) {
942                 if (entering(tcp))
943                         tprintf(", {...}");
944                 return 1;
945         }
946         if (entering(tcp)) {
947                 iop = ioctl_lookup(si.ic_cmd);
948                 if (iop) {
949                         tprintf(", {ic_cmd=%s", iop->symbol);
950                         while ((iop = ioctl_next_match(iop)))
951                                 tprintf(" or %s", iop->symbol);
952                 } else
953                         tprintf(", {ic_cmd=%#x", si.ic_cmd);
954                 if (si.ic_timout == INFTIM)
955                         tprintf(", ic_timout=INFTIM, ");
956                 else
957                         tprintf(" ic_timout=%d, ", si.ic_timout);
958         }
959         in_and_out = 1;
960         switch (si.ic_cmd) {
961 #ifdef SI_GETUDATA
962         case SI_GETUDATA:
963                 in_and_out = 0;
964                 break;
965 #endif /* SI_GETUDATA */
966         }
967         if (in_and_out) {
968                 if (entering(tcp))
969                         tprintf("/* in */ ");
970                 else
971                         tprintf(", /* out */ ");
972         }
973         if (in_and_out || entering(tcp))
974                 tprintf("ic_len=%d, ic_dp=", si.ic_len);
975         switch (si.ic_cmd) {
976 #ifdef TI_BIND
977         case TI_BIND:
978                 /* in T_BIND_REQ, out T_BIND_ACK */
979                 ++timod;
980                 if (entering(tcp)) {
981                         print_transport_message (tcp,
982                                                  T_BIND_REQ,
983                                                  si.ic_dp, si.ic_len);
984                 }
985                 else {
986                         print_transport_message (tcp,
987                                                  T_BIND_ACK,
988                                                  si.ic_dp, si.ic_len);
989                 }
990                 break;
991 #endif /* TI_BIND */
992 #ifdef TI_UNBIND
993         case TI_UNBIND:
994                 /* in T_UNBIND_REQ, out T_OK_ACK */
995                 ++timod;
996                 if (entering(tcp)) {
997                         print_transport_message (tcp,
998                                                  T_UNBIND_REQ,
999                                                  si.ic_dp, si.ic_len);
1000                 }
1001                 else {
1002                         print_transport_message (tcp,
1003                                                  T_OK_ACK,
1004                                                  si.ic_dp, si.ic_len);
1005                 }
1006                 break;
1007 #endif /* TI_UNBIND */
1008 #ifdef TI_GETINFO
1009         case TI_GETINFO:
1010                 /* in T_INFO_REQ, out T_INFO_ACK */
1011                 ++timod;
1012                 if (entering(tcp)) {
1013                         print_transport_message (tcp,
1014                                                  T_INFO_REQ,
1015                                                  si.ic_dp, si.ic_len);
1016                 }
1017                 else {
1018                         print_transport_message (tcp,
1019                                                  T_INFO_ACK,
1020                                                  si.ic_dp, si.ic_len);
1021                 }
1022                 break;
1023 #endif /* TI_GETINFO */
1024 #ifdef TI_OPTMGMT
1025         case TI_OPTMGMT:
1026                 /* in T_OPTMGMT_REQ, out T_OPTMGMT_ACK */
1027                 ++timod;
1028                 if (entering(tcp)) {
1029                         print_transport_message (tcp,
1030                                                  T_OPTMGMT_REQ,
1031                                                  si.ic_dp, si.ic_len);
1032                 }
1033                 else {
1034                         print_transport_message (tcp,
1035                                                  T_OPTMGMT_ACK,
1036                                                  si.ic_dp, si.ic_len);
1037                 }
1038                 break;
1039 #endif /* TI_OPTMGMT */
1040 #ifdef SI_GETUDATA
1041         case SI_GETUDATA:
1042                 if (entering(tcp))
1043                         break;
1044 #if 0
1045                 tprintf("struct si_udata ");
1046 #endif
1047                 if (umove(tcp, (int) si.ic_dp, &udata) < 0)
1048                         tprintf("{...}");
1049                 else {
1050                         tprintf("{tidusize=%d, addrsize=%d, ",
1051                                 udata.tidusize, udata.addrsize);
1052                         tprintf("optsize=%d, etsdusize=%d, ",
1053                                 udata.optsize, udata.etsdusize);
1054                         tprintf("servtype=%d, so_state=%d, ",
1055                                 udata.servtype, udata.so_state);
1056                         tprintf("so_options=%d", udata.so_options);
1057 #if 0
1058                         tprintf(", tsdusize=%d", udata.tsdusize);
1059 #endif
1060                         tprintf("}");
1061                 }
1062                 break;
1063 #endif /* SI_GETUDATA */
1064         default:
1065                 printstr(tcp, (long) si.ic_dp, si.ic_len);
1066                 break;
1067         }
1068         if (exiting(tcp)) {
1069                 tprintf("}");
1070                 if (timod && tcp->u_rval) {
1071                         tcp->auxstr = xlookup (tli_errors, tcp->u_rval);
1072                         return RVAL_STR + 1;
1073                 }
1074         }
1075
1076         return 1;
1077 }
1078
1079 int
1080 stream_ioctl(tcp, code, arg)
1081 struct tcb *tcp;
1082 int code, arg;
1083 {
1084 #ifdef I_LIST
1085         int i;
1086 #endif
1087         int val;
1088 #ifdef I_FLUSHBAND
1089         struct bandinfo bi;
1090 #endif
1091         struct strpeek sp;
1092         struct strfdinsert sfi;
1093         struct strrecvfd srf;
1094 #ifdef I_LIST
1095         struct str_list sl;
1096 #endif
1097
1098         /* I_STR is a special case because the data is read & written. */
1099         if (code == I_STR)
1100                 return internal_stream_ioctl(tcp, arg);
1101         if (entering(tcp))
1102                 return 0;
1103
1104         switch (code) {
1105         case I_PUSH:
1106         case I_LOOK:
1107         case I_FIND:
1108                 /* arg is a string */
1109                 tprintf(", ");
1110                 printpath(tcp, arg);
1111                 return 1;
1112         case I_POP:
1113                 /* doesn't take an argument */
1114                 return 1;
1115         case I_FLUSH:
1116                 /* argument is an option */
1117                 tprintf(", ");
1118                 printxval(stream_flush_options, arg, "FLUSH???");
1119                 return 1;
1120 #ifdef I_FLUSHBAND
1121         case I_FLUSHBAND:
1122                 /* argument is a pointer to a bandinfo struct */
1123                 if (umove(tcp, arg, &bi) < 0)
1124                         tprintf(", {...}");
1125                 else {
1126                         tprintf(", {bi_pri=%d, bi_flag=", bi.bi_pri);
1127                         printflags(stream_flush_options, bi.bi_flag, "FLUSH???");
1128                         tprintf("}");
1129                 }
1130                 return 1;
1131 #endif /* I_FLUSHBAND */
1132         case I_SETSIG:
1133                 /* argument is a set of flags */
1134                 tprintf(", ");
1135                 printflags(stream_setsig_flags, arg, "S_???");
1136                 return 1;
1137         case I_GETSIG:
1138                 /* argument is a pointer to a set of flags */
1139                 if (syserror(tcp))
1140                         return 0;
1141                 tprintf(", [");
1142                 if (umove(tcp, arg, &val) < 0)
1143                         tprintf("?");
1144                 else
1145                         printflags(stream_setsig_flags, val, "S_???");
1146                 tprintf("]");
1147                 return 1;
1148         case I_PEEK:
1149                 /* argument is a pointer to a strpeek structure */
1150                 if (syserror(tcp) || !arg)
1151                         return 0;
1152                 if (umove(tcp, arg, &sp) < 0) {
1153                         tprintf(", {...}");
1154                         return 1;
1155                 }
1156                 tprintf(", {ctlbuf=");
1157                 printstrbuf(tcp, &sp.ctlbuf, 1);
1158                 tprintf(", databuf=");
1159                 printstrbuf(tcp, &sp.databuf, 1);
1160                 tprintf(", flags=");
1161                 printflags(msgflags, sp.flags, "RS_???");
1162                 tprintf("}");
1163                 return 1;
1164         case I_SRDOPT:
1165                 /* argument is an option with flags */
1166                 tprintf(", ");
1167                 printxval(stream_read_options, arg & RMODEMASK, "R???");
1168                 addflags(stream_read_flags, arg & ~RMODEMASK);
1169                 return 1;
1170         case I_GRDOPT:
1171                 /* argument is an pointer to an option with flags */
1172                 if (syserror(tcp))
1173                         return 0;
1174                 tprintf(", [");
1175                 if (umove(tcp, arg, &val) < 0)
1176                         tprintf("?");
1177                 else {
1178                         printxval(stream_read_options,
1179                                   arg & RMODEMASK, "R???");
1180                         addflags(stream_read_flags, arg & ~RMODEMASK);
1181                 }
1182                 tprintf("]");
1183                 return 1;
1184         case I_NREAD:
1185 #ifdef I_GETBAND
1186         case I_GETBAND:
1187 #endif
1188 #ifdef I_SETCLTIME
1189         case I_SETCLTIME:
1190 #endif
1191 #ifdef I_GETCLTIME
1192         case I_GETCLTIME:
1193 #endif
1194                 /* argument is a pointer to a decimal integer */
1195                 if (syserror(tcp))
1196                         return 0;
1197                 tprintf(", ");
1198                 printnum(tcp, arg, "%d");
1199                 return 1;
1200         case I_FDINSERT:
1201                 /* argument is a pointer to a strfdinsert structure */
1202                 if (syserror(tcp) || !arg)
1203                         return 0;
1204                 if (umove(tcp, arg, &sfi) < 0) {
1205                         tprintf(", {...}");
1206                         return 1;
1207                 }
1208                 tprintf(", {ctlbuf=");
1209                 printstrbuf(tcp, &sfi.ctlbuf, 1);
1210                 tprintf(", databuf=");
1211                 printstrbuf(tcp, &sfi.databuf, 1);
1212                 tprintf(", flags=");
1213                 printflags(msgflags, sfi.flags, "RS_???");
1214                 tprintf(", filedes=%d, offset=%d}", sfi.fildes, sfi.offset);
1215                 return 1;
1216 #ifdef I_SWROPT
1217         case I_SWROPT:
1218                 /* argument is a set of flags */
1219                 tprintf(", ");
1220                 printflags(stream_write_flags, arg, "SND???");
1221                 return 1;
1222 #endif /* I_SWROPT */
1223 #ifdef I_GWROPT
1224         case I_GWROPT:
1225                 /* argument is an pointer to an option with flags */
1226                 if (syserror(tcp))
1227                         return 0;
1228                 tprintf(", [");
1229                 if (umove(tcp, arg, &val) < 0)
1230                         tprintf("?");
1231                 else
1232                         printflags(stream_write_flags, arg, "SND???");
1233                 tprintf("]");
1234                 return 1;
1235 #endif /* I_GWROPT */
1236         case I_SENDFD:
1237 #ifdef I_CKBAND
1238         case I_CKBAND:
1239 #endif
1240 #ifdef I_CANPUT
1241         case I_CANPUT:
1242 #endif
1243         case I_LINK:
1244         case I_UNLINK:
1245         case I_PLINK:
1246         case I_PUNLINK:
1247                 /* argument is a decimal integer */
1248                 tprintf(", %d", arg);
1249                 return 1;
1250         case I_RECVFD:
1251                 /* argument is a pointer to a strrecvfd structure */
1252                 if (syserror(tcp) || !arg)
1253                         return 0;
1254                 if (umove(tcp, arg, &srf) < 0) {
1255                         tprintf(", {...}");
1256                         return 1;
1257                 }
1258                 tprintf(", {fd=%d, uid=%lu, gid=%lu}", srf.fd,
1259                         (unsigned long) srf.uid, (unsigned long) srf.gid);
1260                 return 1;
1261 #ifdef I_LIST
1262         case I_LIST:
1263                 if (syserror(tcp))
1264                         return 0;
1265                 if (arg == 0) {
1266                         tprintf(", NULL");
1267                         return 1;
1268                 }
1269                 if (umove(tcp, arg, &sl) < 0) {
1270                         tprintf(", {...}");
1271                         return 1;
1272                 }
1273                 tprintf(", {sl_nmods=%d, sl_modlist=[", sl.sl_nmods);
1274                 for (i = 0; i < tcp->u_rval; i++) {
1275                         if (i)
1276                                 tprintf(", ");
1277                         printpath(tcp, (int) sl.sl_modlist[i].l_name);
1278                 }
1279                 tprintf("]}");
1280                 return 1;
1281 #endif /* I_LIST */
1282 #ifdef I_ATMARK
1283         case I_ATMARK:
1284                 tprintf(", ");
1285                 printxval(stream_atmark_options, arg, "???MARK");
1286                 return 1;
1287 #endif /* I_ATMARK */
1288         default:
1289                 return 0;
1290         }
1291 }
1292
1293 #endif /* !LINUX && !FREEBSD */
1294
1295 #endif /* HAVE_SYS_STREAM_H || LINUX || FREEBSD */