xref: /titanic_51/usr/src/lib/libsmbfs/smb/iod_cl.c (revision de3d2ce46fc25c7b67ccbae4afe5f15e5357568f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Client-side interface to the IO Daemon (IOD)
29  */
30 
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <netdb.h>
38 #include <libintl.h>
39 #include <door.h>
40 
41 #include <sys/byteorder.h>
42 #include <sys/types.h>
43 #include <sys/fcntl.h>
44 #include <sys/ioctl.h>
45 #include <sys/time.h>
46 #include <sys/socket.h>
47 
48 #include <netinet/in.h>
49 #include <netinet/tcp.h>
50 #include <arpa/inet.h>
51 
52 #include <netsmb/smb_lib.h>
53 #include <netsmb/netbios.h>
54 #include <netsmb/nb_lib.h>
55 #include <netsmb/smb_dev.h>
56 
57 #include "charsets.h"
58 #include "private.h"
59 
60 static const char smbiod_path[] = "/usr/lib/smbfs/smbiod";
61 
62 /*
63  * This is constant for the life of a process,
64  * and initialized at startup, so no locks.
65  */
66 static char door_path[40];
67 
68 char *
69 smb_iod_door_path(void)
70 {
71 	static const char fmtR[] = "/var/run/smbiod-%d";
72 	static const char fmtU[] = "/tmp/.smbiod-%d";
73 	const char *fmt;
74 	uid_t uid;
75 
76 	if (door_path[0] == '\0') {
77 		uid = getuid();
78 		fmt = (uid == 0) ? fmtR : fmtU;
79 		snprintf(door_path, sizeof (door_path), fmt, uid);
80 	}
81 
82 	return (door_path);
83 }
84 
85 /*
86  * Open the door (client side) and
87  * find out if the service is there.
88  */
89 int
90 smb_iod_open_door(int *fdp)
91 {
92 	door_arg_t da;
93 	char *path;
94 	int fd, rc;
95 	int err = 0;
96 
97 	path = smb_iod_door_path();
98 	fd = open(path, O_RDONLY, 0);
99 	if (fd < 0)
100 		return (errno);
101 
102 	/*
103 	 * Make sure the IOD is running.
104 	 * Pass NULL args.
105 	 */
106 	memset(&da, 0, sizeof (da));
107 	da.rbuf = (void *) &err;
108 	da.rsize = sizeof (err);
109 	rc = door_call(fd, &da);
110 	if (rc < 0) {
111 		err = errno;
112 		close(fd);
113 		return (err);
114 	}
115 	if (err != 0) {
116 		close(fd);
117 		return (err);
118 	}
119 
120 	/* This handle controls per-process resources. */
121 	(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
122 
123 	*fdp = fd;
124 	return (0);
125 }
126 
127 /*
128  * Start the IOD and wait until we can
129  * open its client-side door.
130  */
131 static int
132 start_iod(int *fdp)
133 {
134 	int err, pid, t;
135 
136 	pid = vfork();
137 	if (pid < 0)
138 		return (errno);
139 
140 	/*
141 	 * child: start smbiod
142 	 */
143 	if (pid == 0) {
144 		char *argv[2];
145 		argv[0] = "smbiod";
146 		argv[1] = NULL;
147 		execv(smbiod_path, argv);
148 		return (errno);
149 	}
150 
151 	/*
152 	 * parent: wait for smbiod to start
153 	 */
154 	for (t = 0; t < 10; t++) {
155 		sleep(1);
156 		err = smb_iod_open_door(fdp);
157 		if (err == 0)
158 			break;
159 	}
160 
161 	return (err);
162 }
163 
164 
165 /*
166  * Start smbiod if necessary, and then
167  * ask it to connect using the info in ctx.
168  */
169 int
170 smb_iod_cl_newvc(smb_ctx_t *ctx)
171 {
172 	door_arg_t da;
173 	int fd, err = 0;
174 
175 	err = smb_iod_open_door(&fd);
176 	if (err != 0) {
177 		err = start_iod(&fd);
178 		if (err)
179 			return (err);
180 	}
181 
182 	da.data_ptr = (void *) &ctx->ct_iod_ssn;
183 	da.data_size = sizeof (ctx->ct_iod_ssn);
184 	da.desc_ptr = NULL;
185 	da.desc_num = 0;
186 	da.rbuf = (void *) &err;
187 	da.rsize = sizeof (err);
188 	if (door_call(fd, &da) < 0) {
189 		err = errno;
190 		DPRINT("door_call, err=%d", err);
191 	}
192 	close(fd);
193 
194 	return (err);
195 }
196