Update copyright dates (again)
[xfstests-dev.git] / dmapi / src / suite1 / cmd / test_assumption.c
1 /*
2  * Copyright (c) 2000-2001 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 <sys/types.h>
34 #include <sys/stat.h>
35
36 #include <ctype.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39
40 #include <lib/hsm.h>
41
42 /*
43         Test that the new session ID is correctly returned to the caller on
44         dm_create_session when the oldsid parameter is not DM_NO_SESSION.
45
46         Test to make sure that DM_NO_TOKEN does not work if the session ID
47         is invalid.
48
49         Both are needed so that session assumption works correctly.  The
50         test creates a session, queries the session, assumes the session,
51         then attempts to check the event list of the old session ID while
52         using DM_NO_TOKEN.
53
54         The only parameter is the pathname of a DMAPI filesystem.  If it is
55         working correctly, you should get the error message:
56
57                 SUCCESS!
58
59         Any message containing the word FAILURE indicates a problem.
60 */
61
62 char            *Progname;
63 dm_sessid_t     sid = DM_NO_SESSION;    /* session ID of original session */
64 dm_sessid_t     newsid = DM_NO_SESSION; /* session ID after session resumed */
65 dm_eventset_t   eventlist;
66
67 int
68 main(
69         int     argc, 
70         char    **argv)
71 {
72         char            buffer[DM_SESSION_INFO_LEN];
73         void            *hanp;
74         size_t          hlen;
75         u_int           nelem;
76         size_t           rlen;
77
78         Progname = argv[0];
79
80         if (argc != 2) {
81                 fprintf(stderr, "usage:\t%s filesystem\n", Progname);
82                 exit(1);
83         }
84
85         /* Initialize the DMAPI interface and obtain a session ID, then verify
86            that the filesystem supports DMAPI.
87         */
88
89         if (setup_dmapi(&sid))
90                 exit(1);
91
92         if (dm_path_to_handle(argv[1], &hanp, &hlen)) {
93                 perror("FAILURE: can't get handle for filesystem");
94                 exit(1);
95         }
96
97         /* Query the session just to make sure things are working okay. */
98
99         if (dm_query_session(sid, sizeof(buffer), buffer, &rlen)) {
100                 errno_msg("FAILURE: can't query the original session ID %d",
101                         sid);
102                 exit(1);
103         }
104         fprintf(stdout, "Initial session ID: %d\n", sid);
105         fprintf(stdout, "Initial session message length: '%d'\n", rlen);
106         if (rlen > 0) {
107                 fprintf(stdout, "Initial session message: '%s'\n", buffer);
108         }
109
110         /* Now try to assume the session. */
111
112         if (dm_create_session(sid, "this is a new message", &newsid))  {
113                 fprintf(stderr, "FAILURE: can't assume session %d\n", sid);
114                 exit(1);
115         }
116
117         /* Now query the new session. */
118
119         if (dm_query_session(newsid, sizeof(buffer), buffer, &rlen)) {
120                 errno_msg("FAILURE: can't query the assumed session ID %d",
121                         newsid);
122                 exit(1);
123         }
124         fprintf(stdout, "Assumed session ID: %d\n", newsid);
125         fprintf(stdout, "Assumed session message length: '%d'\n", rlen);
126         if (rlen > 0) {
127                 fprintf(stdout, "Assumed session message: '%s'\n", buffer);
128         }
129
130         /* Get rid of the new session as we are done with it. */
131
132         if (dm_destroy_session(newsid))  {
133                 fprintf(stderr, "FAILURE: Can't shut down assumed session %d\n",
134                         newsid);
135                 exit(1);
136         }
137
138         /* Now verify that DM_NO_TOKEN will not work with the old session ID,
139            which is now invalid.
140         */
141
142         DMEV_ZERO(eventlist);
143
144         if (dm_get_eventlist(sid, hanp, hlen, DM_NO_TOKEN, DM_EVENT_MAX,
145                         &eventlist, &nelem) == 0) {
146                 fprintf(stderr, "FAILURE: dm_get_eventlist() worked when it "
147                         "should have failed!\n");
148         }
149 #ifdef  VERITAS_21
150         if (errno != ESRCH) {
151 #else
152         if (errno != EINVAL) {
153 #endif
154                 errno_msg("FAILURE: unexpected errno");
155                 exit(1);
156         }
157         fprintf(stdout, "SUCCESS!\n");
158         exit(0);
159 }