summaryrefslogtreecommitdiff
path: root/usr/src/lib/libsmedia/library/common/l_misc.c
blob: e1e3b386aa3f69f064134eefb76701511a5c0807 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * l_misc.c :
 *      This file contains the miscelleneous routines for libsm.so
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <fcntl.h>
#include <sys/dkio.h>
#include <sys/vtoc.h>
#include <sys/dktp/fdisk.h>
#include <dirent.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <strings.h>
#include "l_defines.h"
#include <rpc/rpc.h>
#include "smed.h"
#include <sys/smedia.h>
#include "../inc/rmedia.h"
#include <smserver.h>
#include <sys/mman.h>
#include <utmpx.h>
#include <limits.h>

#ifdef _LP64
#ifdef __sparc
#define	PATHNAME "/usr/lib/smedia/sparcv9"
#else
#define	PATHNAME "/usr/lib/smedia/amd64"
#endif
#else
#define	PATHNAME "/usr/lib/smedia"
#endif

#define	PERROR(string)	my_perror(gettext(string))
#define	RUN_LIBSMEDIA_SERVER	"	/usr/lib/smedia/rpc.smserverd &\n"

static void
my_perror(char *err_string)
{

	int error_no;
	if (errno == 0)
		return;

	error_no = errno;
	(void) fprintf(stderr, gettext(err_string));
	(void) fprintf(stderr, gettext(" : "));
	errno = error_no;
	perror("");
}

static int
is_server_running(rmedia_handle_t *handle)
{
	door_arg_t	door_args;
	smedia_reqping_t	reqping;
	smedia_retping_t	*retping;
	int		ret_val;
	int		door_fd;
	CLIENT		*clnt;
	char	rbuf[sizeof (smedia_services_t) + sizeof (door_desc_t)];
	smserver_info	*server_info;

	/*
	 * We will assume that we are running at level 2 or greater
	 * and attempt to contact the server using RPC mecahnisms.
	 * If that fails then we will attempt to contact the server
	 * using non-rpc mechanism. This will enable the libsmedia
	 * to be used in SINGLE user mode when inetd is not running.
	 * We expect the server to have been started manually by user.
	 *
	 * Note that "localhost" is used (vs hostname (eg, "uname -n")),
	 * as this minimizes interference with common IPSec rules.
	 */

	clnt = clnt_create("localhost", SMSERVERPROG, SMSERVERVERS,
	    "circuit_v");
	if (clnt == (CLIENT *)NULL) {
		/*
		 * The failure could be that we are running at level 1
		 */
		door_fd = open(smedia_service, O_RDONLY, 0644);
		if (door_fd < 0) {
			DPRINTF1("Error in opening %s\n",
			    smedia_service);
			return (0);
		}

		DPRINTF1("rbuf address=%p\n", rbuf);
		reqping.cnum = SMEDIA_CNUM_PING;
		door_args.data_ptr = (char *)&reqping;
		door_args.data_size = sizeof (smedia_services_t);
		door_args.desc_ptr = NULL;
		door_args.desc_num = 0;
		door_args.rbuf = rbuf;
		door_args.rsize = sizeof (rbuf);

		ret_val = door_call(door_fd, &door_args);
		(void) close(door_fd);
		if (ret_val < 0) {
			return (0);
		}
		DPRINTF3("rsize = %d data_size = %d data_ptr = %p \n",
		    door_args.rsize, door_args.data_size,
		    door_args.data_ptr);
		retping = (smedia_retping_t *)(
		    (void *)door_args.data_ptr);
		if (retping->cnum != SMEDIA_CNUM_PING) {
			DPRINTF1("*** door call failed *** cnum "
			    "returned = 0x%x\n", retping->cnum);
			return (0);
		}
		return (1);
	}
	server_info = smserverproc_get_serverinfo_1(NULL, clnt);
	if (server_info == NULL) {
		if (clnt)
			clnt_destroy(clnt);
		return (0);
	}
	if (server_info->status != 0) {
		if (clnt)
			clnt_destroy(clnt);
		DPRINTF1("get server_info call failed. "
		    "status = %d\n", server_info->status);
		return (0);
	}
	if (server_info->vernum != SMSERVERVERS) {
		if (clnt)
			clnt_destroy(clnt);
		DPRINTF2("version expected = %d version "
		    "returned = %d\n", SMSERVERVERS,
		    server_info->vernum);
		return (0);
	}

	door_fd = open(smedia_service, O_RDONLY, 0644);
	if (door_fd < 0) {
		DPRINTF1("Error in opening %s\n", smedia_service);
		return (0);
	}

	DPRINTF1("rbuf address=%p\n", rbuf);
	reqping.cnum = SMEDIA_CNUM_PING;
	door_args.data_ptr = (char *)&reqping;
	door_args.data_size = sizeof (smedia_services_t);
	door_args.desc_ptr = NULL;
	door_args.desc_num = 0;
	door_args.rbuf = rbuf;
	door_args.rsize = sizeof (rbuf);

	ret_val = door_call(door_fd, &door_args);
	(void) close(door_fd);
	if (ret_val < 0) {
		return (0);
	}
	DPRINTF3("rsize = %d data_size = %d data_ptr = %p \n",
	    door_args.rsize, door_args.data_size,
	    door_args.data_ptr);
	retping = (smedia_retping_t *)((void *)door_args.data_ptr);
	if (retping->cnum != SMEDIA_CNUM_PING) {
		DPRINTF1("*** door call failed *** cnum returned "
		    "= 0x%x\n", retping->cnum);
		return (0);
	}
	handle->sm_clnt = clnt;
	return (1);
}

static void *
get_dev_library_handle(int32_t fd)
{
	void *handle;
	void *old_handle = NULL;
	struct dk_cinfo dkinfo;
	DIR *dirp;
	struct dirent *dp;
	char *pathname;
	int32_t (*d_fcn_ptr)(ushort_t, ushort_t);
	int32_t (*v_fcn_ptr)(void);
	int32_t ret_val;

	if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) {
		PERROR("DKIOCINFO failed");
		return (NULL);
	}
	DPRINTF1("dki_ctype = 0x%x\n", dkinfo.dki_ctype);

	if ((pathname = malloc(PATH_MAX)) == NULL) {
		PERROR("malloc failed");
		return (NULL);
	}

	dirp = opendir(PATHNAME);
	if (dirp == NULL) {
		(void) fprintf(stderr, gettext("Couldnot open %s\n"), PATHNAME);
		free(pathname);
		return (NULL);
	}

	while ((dp = readdir(dirp)) != NULL) {
		if (strncmp("sm_", dp->d_name, 3) != 0) {
			DPRINTF1("not a library %s\n", dp->d_name);
			continue;
		}
		if (snprintf(pathname, PATH_MAX, "%s/%s",
		    PATHNAME, dp->d_name) >= PATH_MAX) {
			continue;
		}

		handle = dlopen(pathname, RTLD_LAZY);
		if (handle == NULL) {
			PERROR("Error opening library file");
			continue;
		}
		d_fcn_ptr = (int32_t (*)(ushort_t, ushort_t))dlsym(handle,
		    "_m_device_type");
		if (d_fcn_ptr == NULL) {
			DPRINTF("Could not find _m_device_type\n");
			(void) dlclose(handle);
			continue;
		}
		ret_val = (*d_fcn_ptr)(dkinfo.dki_ctype, 0);
		if (ret_val == 0) {
			DPRINTF1("NAME %s\n", dp->d_name);
			v_fcn_ptr = (int32_t (*)(void))dlsym(handle,
			    "_m_version_no");
			if (v_fcn_ptr == NULL) {
				DPRINTF("Could not find _m_version_no\n");
				(void) dlclose(handle);
				continue;
			}
			ret_val = (*v_fcn_ptr)();
			if ((ret_val >= 0) &&
			    (ret_val >= SM_PLUGIN_VERSION)) {
				if (old_handle != NULL)
					(void) dlclose(old_handle);
				old_handle = handle;
				continue;
			} else {
				(void) dlclose(handle);
			}
		} else {
			(void) dlclose(handle);
		}
	}
	free(pathname);
	(void) closedir(dirp);
	return (old_handle);
}

int32_t
call_function(rmedia_handle_t *handle, void *ip, char *func_name)
{

	int32_t ret_val;
	int32_t (*fcn_ptr)(rmedia_handle_t *handle, void *ip);
	void *lib_handle;

	if (handle == NULL) {
		DPRINTF("Handle is NULL\n");
		errno = EINVAL;
		return (-1);
	}
	lib_handle = handle->sm_lib_handle;
	if (handle->sm_signature != LIBSMEDIA_SIGNATURE) {
		DPRINTF2("call_function:signature expected=0x%x, found=0x%x\n",
		    LIBSMEDIA_SIGNATURE, handle->sm_signature);
		errno = EINVAL;
		return (-1);
	}

	fcn_ptr = (int32_t (*)(rmedia_handle_t *, void*))
	    dlsym(lib_handle, func_name);
	if (fcn_ptr == NULL) {
		DPRINTF1("Could not find %s\n", func_name);
		errno = ENOTSUP;
		return (-1);
	}
	ret_val = (*fcn_ptr)(handle, ip);
	return (ret_val);
}

int32_t
release_handle(rmedia_handle_t *handle)
{
	if (handle == NULL) {
		DPRINTF("Handle is NULL\n");
		errno = EINVAL;
		return (-1);
	}
	if ((handle->sm_dkinfo.dki_ctype == DKC_SCSI_CCS) ||
	    (handle->sm_dkinfo.dki_ctype == DKC_MD21) ||
	    (handle->sm_dkinfo.dki_ctype == DKC_CDROM)) {
		(void) close(handle->sm_door);
		(void) close(handle->sm_death_door);
		if (handle->sm_buf != NULL)
			(void) munmap(handle->sm_buf, handle->sm_bufsize);
		if (handle->sm_clnt != NULL)
			clnt_destroy(handle->sm_clnt);
	}
	(void) close(handle->sm_buffd);
	handle->sm_signature = 0;
	(void) dlclose(handle->sm_lib_handle);
	free(handle);
	return (0);
}

smedia_handle_t
get_handle_from_fd(int32_t fd)
{
	rmedia_handle_t	*handle;
	void	*lib_handle;
	int	door_fd, door_server;
	int	ret_val;
	door_arg_t	door_args;
	smedia_reqopen_t	reqopen;
	smedia_reterror_t	*reterror;
	door_desc_t	ddesc[2];
	char	rbuf[sizeof (smedia_services_t) + sizeof (door_desc_t)];
	struct stat	stat;

	DPRINTF("smedia_get_handle called\n");
	handle = (rmedia_handle_t *)malloc(sizeof (rmedia_handle_t));
	if (handle == NULL) {
		DPRINTF("Could not allocate memory for handle\n");
		return (NULL);
	}
	(void) memset((void *) handle, 0, sizeof (rmedia_handle_t));
	handle->sm_fd = -1;
	handle->sm_door = -1;
	handle->sm_death_door = -1;
	handle->sm_buffd = -1;
	handle->sm_buf = NULL;
	handle->sm_bufsize = 0;

	if (ioctl(fd, DKIOCINFO, &handle->sm_dkinfo) == -1) {
		free(handle);
		PERROR("DKIOCINFO failed");
		return (NULL);
	}
	lib_handle = get_dev_library_handle(fd);
	if (lib_handle == NULL) {
		free(handle);
		DPRINTF("lib_Handle is NULL\n");
		errno = ENOTSUP;
		return (NULL);
	}
	DPRINTF("Handle initialised successfully.\n");
	/* Initialise the handle elements */
	handle->sm_lib_handle = lib_handle;
	handle->sm_signature = LIBSMEDIA_SIGNATURE;
	DPRINTF2("fd=%d signature=0x%x\n", handle->sm_fd, handle->sm_signature);

	if ((handle->sm_dkinfo.dki_ctype == DKC_SCSI_CCS) ||
	    (handle->sm_dkinfo.dki_ctype == DKC_MD21) ||
	    (handle->sm_dkinfo.dki_ctype == DKC_CDROM)) {

		ret_val = is_server_running(handle);
		if (ret_val == 0) {
			(void) dlclose(handle->sm_lib_handle);
			free(handle);
			return (NULL);
		}
		door_fd = open(smedia_service, O_RDONLY, 0644);
		if (door_fd < 0) {
			(void) dlclose(handle->sm_lib_handle);
			free(handle);
			if (handle->sm_clnt)
				clnt_destroy(handle->sm_clnt);
			DPRINTF1("Error in opening %s\n", smedia_service);
			PERROR(smedia_service);
			return (NULL);
		}

		DPRINTF1("rbuf address=%p\n", rbuf);
		ddesc[0].d_data.d_desc.d_descriptor = fd;
		ddesc[0].d_attributes = DOOR_DESCRIPTOR;
		reqopen.cnum = SMEDIA_CNUM_OPEN_FD;
		door_args.data_ptr = (char *)&reqopen;
		door_args.data_size = sizeof (smedia_services_t);
		door_args.desc_ptr = &ddesc[0];
		door_args.desc_num = 1;
		door_args.rbuf = rbuf;
		door_args.rsize = sizeof (rbuf);

		ret_val = door_call(door_fd, &door_args);
		(void) close(door_fd);
		if (ret_val < 0) {
			(void) dlclose(handle->sm_lib_handle);
			free(handle);
			if (handle->sm_clnt)
				clnt_destroy(handle->sm_clnt);
			PERROR("door_call");
			return (NULL);
		}
		DPRINTF3("rsize = %d data_size = %d data_ptr = %p \n",
		    door_args.rsize, door_args.data_size,
		    door_args.data_ptr);
		reterror = (smedia_reterror_t *)((void *)door_args.data_ptr);
		if (reterror->cnum != SMEDIA_CNUM_OPEN_FD) {
			(void) dlclose(handle->sm_lib_handle);
			free(handle);
			if (handle->sm_clnt)
				clnt_destroy(handle->sm_clnt);
			DPRINTF1(
	"*** door call failed *** cnum returned = 0x%x\n", reterror->cnum);
			errno = reterror->errnum;
			return (NULL);
		}
		/*
		 * 2 door descriptors are returned after the above door call.
		 * The first door descriptor is the one that will be used
		 * in subsequent smedia calls. A dedicated thread is
		 * associated with this door to handle client calls.
		 * The second door descriptor is needed to signal unexpected
		 * death of the client to the server. This will help the server
		 * to do the necessary cleanup.
		 */
		if (door_args.desc_num != 2) {
			(void) dlclose(handle->sm_lib_handle);
			free(handle);
			if (handle->sm_clnt)
				clnt_destroy(handle->sm_clnt);
			DPRINTF("Num of door descriptors returned by "
			    "server is not 2");
			if (door_args.desc_num)
				(void) close(door_args.desc_ptr->\
				    d_data.d_desc.d_descriptor);
			return (NULL);
		}
		door_server = door_args.desc_ptr->d_data.d_desc.d_descriptor;
		/* Check if the descriptor returned is S_IFDOOR */
		if (fstat(door_server, &stat) < 0) {
			PERROR("fstat");
			(void) dlclose(handle->sm_lib_handle);
			free(handle);
			if (handle->sm_clnt)
				clnt_destroy(handle->sm_clnt);
			return (NULL);
		}
		if (!S_ISDOOR(stat.st_mode)) {
			DPRINTF(
		"Descriptor returned by door_call is not of type DOOR\n");
			(void) dlclose(handle->sm_lib_handle);
			free(handle);
			if (handle->sm_clnt)
				clnt_destroy(handle->sm_clnt);
			return (NULL);
		}
		handle->sm_door = door_server;
		handle->sm_fd = fd;
		door_args.desc_ptr++;
		handle->sm_death_door =
		    door_args.desc_ptr->d_data.d_desc.d_descriptor;
		DPRINTF("door call succeeded.\n");
		return ((smedia_handle_t)handle);

	} else {
		handle->sm_fd = fd;
		return ((smedia_handle_t)handle);
	}

}