2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
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.
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.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
39 /*******************************************************************************
42 * find_test_session - find or create a test DMAPI session.
45 * find_test_session() is used to find a test DMAPI session to
46 * use. There is only one test session per host; all test processes
47 * share the same session. If the session does not already exist,
48 * then the first process to call find_test_session() causes
49 * the session to be created.
51 * Note: It is possible for N different programs to call this routine
52 * at the same time. Each would find that a test session does not
53 * exist, and each one would then create a new test session. Since
54 * excess test sessions are not automatically released on death of
55 * process, we need to make sure that we don't leave such excess
56 * sessions around. So, after creating a session we go back and find
57 * the test session with the lowest session number. If it is ours,
58 * great; we are done. If not, then we must destroy our session
59 * and use the one with the lower session ID. There is still a risk
60 * here of creating a session and crashing before it can be removed
61 * again. To deal with this, the daemon will periodically remove all
62 * test sessions except for the one with the lowest ID value.
67 *******************************************************************************/
69 #define TEST_MSG "DMAPI test session"
76 return(*((dm_sessid_t *)a) - *((dm_sessid_t *)b));
83 char buffer[DM_SESSION_INFO_LEN];
84 dm_sessid_t *sidbuf = NULL;
85 dm_sessid_t new_session;
92 /* Retrieve the list of all active sessions on the host. */
94 nelem = 100; /* a reasonable first guess */
96 if (allocelem < nelem) {
98 sidbuf = realloc(sidbuf, nelem * sizeof(*sidbuf));
100 fprintf(stderr, "realloc of %d bytes failed\n",
101 nelem * sizeof(*sidbuf));
105 error = dm_getall_sessions(allocelem, sidbuf, &nelem);
106 } while (error < 0 && errno == E2BIG);
108 /* If an error occurred, translate it into something meaningful. */
111 fprintf(stderr, "unexpected dm_getall_sessions failure, %s\n",
117 /* We have the list of all active sessions. Scan the list looking
118 for an existing "test" session that we can use. The list must
119 first be sorted in case other processes happen to be creating test
120 sessions at the same time; we need to make sure that we pick the one
121 with the lowest session ID.
124 qsort(sidbuf, nelem, sizeof(sidbuf[0]), session_compare);
126 for (i = 0; i < nelem; i++) {
127 error = dm_query_session(sidbuf[i], sizeof(buffer),
130 fprintf(stderr, "unexpected dm_query_session "
131 "failure, %s\n", strerror(errno));
136 if (!strncmp(buffer, TEST_MSG, strlen(TEST_MSG)))
140 *session = (dm_sessid_t)sidbuf[i];
145 /* No test session exists, so we have to create one ourselves. */
147 if (dm_create_session(DM_NO_SESSION, TEST_MSG, &new_session) != 0) {
148 fprintf(stderr, "dm_create_session failed, %s\n",
154 /* Now re-retrieve the list of active sessions. */
157 if (allocelem < nelem) {
159 sidbuf = realloc(sidbuf, nelem * sizeof(*sidbuf));
160 if (sidbuf == NULL) {
161 fprintf(stderr, "realloc of %d bytes failed\n",
162 nelem * sizeof(*sidbuf));
166 error = dm_getall_sessions(allocelem, sidbuf, &nelem);
167 } while (error < 0 && errno == E2BIG);
170 fprintf(stderr, "dm_getall_sessions failed, %s\n",
176 /* Sort the session ID list into ascending ID order, then find the
177 test session with the lowest session ID. We better find at
178 least one since we created one!
181 qsort(sidbuf, nelem, sizeof(sidbuf[0]), session_compare);
183 for (i = 0; i < nelem; i++) {
184 error = dm_query_session(sidbuf[i], sizeof(buffer),
187 fprintf(stderr, "dm_query_session failed, %s\n",
192 if (!strncmp(buffer, TEST_MSG, strlen(TEST_MSG)))
196 fprintf(stderr, "can't find the session we created\n");
200 *session = (dm_sessid_t)sidbuf[i];
203 /* If the session we are going to use is not the one we created,
204 then we need to get rid of the created one.
207 if (*session != new_session) {
208 if ((new_session) != 0) {
209 fprintf(stderr, "dm_destroy_session failed, %s\n",