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/
37 /*******************************************************************************
40 * find_test_session - find or create a test DMAPI session.
43 * find_test_session() is used to find a test DMAPI session to
44 * use. There is only one test session per host; all test processes
45 * share the same session. If the session does not already exist,
46 * then the first process to call find_test_session() causes
47 * the session to be created.
49 * Note: It is possible for N different programs to call this routine
50 * at the same time. Each would find that a test session does not
51 * exist, and each one would then create a new test session. Since
52 * excess test sessions are not automatically released on death of
53 * process, we need to make sure that we don't leave such excess
54 * sessions around. So, after creating a session we go back and find
55 * the test session with the lowest session number. If it is ours,
56 * great; we are done. If not, then we must destroy our session
57 * and use the one with the lower session ID. There is still a risk
58 * here of creating a session and crashing before it can be removed
59 * again. To deal with this, the daemon will periodically remove all
60 * test sessions except for the one with the lowest ID value.
65 *******************************************************************************/
67 #define TEST_MSG "DMAPI test session"
74 return(*((dm_sessid_t *)a) - *((dm_sessid_t *)b));
81 char buffer[DM_SESSION_INFO_LEN];
82 dm_sessid_t *sidbuf = NULL;
83 dm_sessid_t new_session;
90 /* Retrieve the list of all active sessions on the host. */
92 nelem = 100; /* a reasonable first guess */
94 if (allocelem < nelem) {
96 sidbuf = realloc(sidbuf, nelem * sizeof(*sidbuf));
98 fprintf(stderr, "realloc of %d bytes failed\n",
99 nelem * sizeof(*sidbuf));
103 error = dm_getall_sessions(allocelem, sidbuf, &nelem);
104 } while (error < 0 && errno == E2BIG);
106 /* If an error occurred, translate it into something meaningful. */
109 fprintf(stderr, "unexpected dm_getall_sessions failure, %s\n",
115 /* We have the list of all active sessions. Scan the list looking
116 for an existing "test" session that we can use. The list must
117 first be sorted in case other processes happen to be creating test
118 sessions at the same time; we need to make sure that we pick the one
119 with the lowest session ID.
122 qsort(sidbuf, nelem, sizeof(sidbuf[0]), session_compare);
124 for (i = 0; i < nelem; i++) {
125 error = dm_query_session(sidbuf[i], sizeof(buffer),
128 fprintf(stderr, "unexpected dm_query_session "
129 "failure, %s\n", strerror(errno));
134 if (!strncmp(buffer, TEST_MSG, strlen(TEST_MSG)))
138 *session = (dm_sessid_t)sidbuf[i];
143 /* No test session exists, so we have to create one ourselves. */
145 if (dm_create_session(DM_NO_SESSION, TEST_MSG, &new_session) != 0) {
146 fprintf(stderr, "dm_create_session failed, %s\n",
152 /* Now re-retrieve the list of active sessions. */
155 if (allocelem < nelem) {
157 sidbuf = realloc(sidbuf, nelem * sizeof(*sidbuf));
158 if (sidbuf == NULL) {
159 fprintf(stderr, "realloc of %d bytes failed\n",
160 nelem * sizeof(*sidbuf));
164 error = dm_getall_sessions(allocelem, sidbuf, &nelem);
165 } while (error < 0 && errno == E2BIG);
168 fprintf(stderr, "dm_getall_sessions failed, %s\n",
174 /* Sort the session ID list into ascending ID order, then find the
175 test session with the lowest session ID. We better find at
176 least one since we created one!
179 qsort(sidbuf, nelem, sizeof(sidbuf[0]), session_compare);
181 for (i = 0; i < nelem; i++) {
182 error = dm_query_session(sidbuf[i], sizeof(buffer),
185 fprintf(stderr, "dm_query_session failed, %s\n",
190 if (!strncmp(buffer, TEST_MSG, strlen(TEST_MSG)))
194 fprintf(stderr, "can't find the session we created\n");
198 *session = (dm_sessid_t)sidbuf[i];
201 /* If the session we are going to use is not the one we created,
202 then we need to get rid of the created one.
205 if (*session != new_session) {
206 if ((new_session) != 0) {
207 fprintf(stderr, "dm_destroy_session failed, %s\n",