No Message Supplied
[xfstests-dev.git] / dmapi / src / suite1 / cmd / get_events.c
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 #include <lib/hsm.h>
34
35 #include <string.h>
36 #include <getopt.h>
37
38 /*---------------------------------------------------------------------------
39
40 Test program used to test the DMAPI function dm_get_events().  The
41 command line is:
42
43         get_events [-b buflen] [-m maxmsgs] [-f] sid
44
45 where buflen is the size of the buffer to use, maxmsgs is the number of messages
46 to read, -f, if selected, is DM_EV_WAIT, and sid is the session ID
47 whose dispositions you are interested in.
48
49 ----------------------------------------------------------------------------*/
50
51 #ifndef linux
52 extern  char    *sys_errlist[];
53 #endif
54 extern  int     optind;
55 extern  char    *optarg;
56
57
58 char    *Progname;
59
60 static void
61 usage(void)
62 {
63         fprintf(stderr, "usage:\t%s [-b buflen] [-m maxmsgs] [-f] sid\n",
64                 Progname);
65         exit(1);
66 }
67
68
69 int
70 main(
71         int     argc, 
72         char    **argv)
73 {
74         dm_eventmsg_t   *msg;
75         dm_sessid_t     sid;
76         u_int           flags = 0;
77         void            *bufp;
78         size_t          buflen = 10000;
79         u_int           maxmsgs = 1;
80         size_t          rlenp;
81         char            *name;
82         int             opt;
83
84         if (Progname = strrchr(argv[0], '/')) {
85                 Progname++;
86         } else {
87                 Progname = argv[0];
88         }
89
90         /* Crack and validate the command line options. */
91
92         while ((opt = getopt(argc, argv, "b:m:f")) != EOF) {
93                 switch (opt) {
94                 case 'b':
95                         buflen = atol(optarg);
96                         break;
97                 case 'm':
98                         maxmsgs = atol(optarg);
99                         break;
100                 case 'f':
101                         flags = DM_EV_WAIT;
102                         break;
103                 case '?':
104                         usage();
105                 }
106         }
107         if (optind + 1 != argc)
108                 usage();
109         sid = atol(argv[optind]);
110
111         if (dm_init_service(&name) == -1)  {
112                 fprintf(stderr, "Can't initialize the DMAPI\n");
113                 exit(1);
114         }
115
116         if (buflen > 0) {
117                 if ((bufp = malloc(buflen)) == NULL) {
118                         fprintf(stderr, "malloc failed, %s\n", strerror(errno));
119                         exit(1);
120                 }
121         }
122
123         if (dm_get_events(sid, maxmsgs, flags, buflen, bufp, &rlenp)) {
124                 if (errno == E2BIG) {
125                         fprintf(stderr, "dm_get_events buffer too small, "
126                                 "should be %d bytes\n", rlenp);
127                 } else {
128                         fprintf(stderr, "dm_get_events failed, (%d)%s\n",
129                                 errno, strerror(errno));
130                 }
131                 exit(1);
132         }
133         fprintf(stdout, "rlenp is %d\n", rlenp);
134
135         if (rlenp == 0)
136                 return(0);
137
138         msg = bufp;
139         while (msg != NULL) {
140                 print_one_message(msg);
141                 msg = DM_STEP_TO_NEXT(msg, dm_eventmsg_t *);
142         }
143         return(0);
144 }