Module Name:    src
Committed By:   pooka
Date:           Sun Sep  6 18:38:17 UTC 2009

Modified Files:
        src/dist/smbfs/lib/smb: ctx.c file.c nbns_rq.c rq.c
Added Files:
        src/dist/smbfs/include: smb_kernelops.h
        src/dist/smbfs/lib/smb: smb_kernelops.c

Log Message:
Implement smbfs kernel operations (ioctl etc.) through a vector of
function pointers instead of direct linkage so that rump syscalls
are possible.


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/dist/smbfs/include/smb_kernelops.h
cvs rdiff -u -r1.13 -r1.14 src/dist/smbfs/lib/smb/ctx.c
cvs rdiff -u -r1.3 -r1.4 src/dist/smbfs/lib/smb/file.c
cvs rdiff -u -r1.6 -r1.7 src/dist/smbfs/lib/smb/nbns_rq.c
cvs rdiff -u -r1.4 -r1.5 src/dist/smbfs/lib/smb/rq.c
cvs rdiff -u -r0 -r1.1 src/dist/smbfs/lib/smb/smb_kernelops.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/dist/smbfs/lib/smb/ctx.c
diff -u src/dist/smbfs/lib/smb/ctx.c:1.13 src/dist/smbfs/lib/smb/ctx.c:1.14
--- src/dist/smbfs/lib/smb/ctx.c:1.13	Fri Jun 26 22:41:26 2009
+++ src/dist/smbfs/lib/smb/ctx.c	Sun Sep  6 18:38:17 2009
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: ctx.c,v 1.13 2009/06/26 22:41:26 njoly Exp $");
+__RCSID("$NetBSD: ctx.c,v 1.14 2009/09/06 18:38:17 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/sysctl.h>
@@ -59,6 +59,8 @@
 #include <netsmb/smb_conn.h>
 #include <cflib.h>
 
+#include "smb_kernelops.h"
+
 /*
  * Prescan command line for [-U user] argument
  * and fill context with defaults
@@ -601,7 +603,7 @@
 	/*
 	 * First, try to open as cloned device
 	 */
-	fd = open("/dev/"NSMB_NAME, O_RDWR);
+	fd = smb_kops.ko_open("/dev/"NSMB_NAME, O_RDWR, 0);
 	if (fd >= 0) {
 		ctx->ct_fd = fd;
 		return 0;
@@ -613,7 +615,7 @@
 	 */
 	 for (i = 0; i < 1024; i++) {
 	         snprintf(buf, sizeof(buf), "/dev/"NSMB_NAME"%d", i);
-		 fd = open(buf, O_RDWR);
+		 fd = smb_kops.ko_open(buf, O_RDWR, 0);
 		 if (fd >= 0) {
 			ctx->ct_fd = fd;
 			return 0;
@@ -652,7 +654,7 @@
 		return EINVAL;
 	}
 	if (ctx->ct_fd != -1) {
-		close(ctx->ct_fd);
+		smb_kops.ko_close(ctx->ct_fd);
 		ctx->ct_fd = -1;
 	}
 	error = smb_ctx_gethandle(ctx);
@@ -665,10 +667,10 @@
 	bcopy(&ctx->ct_sh, &rq.ioc_sh, sizeof(struct smbioc_oshare));
 	rq.ioc_flags = flags;
 	rq.ioc_level = level;
-	if (ioctl(ctx->ct_fd, SMBIOC_LOOKUP, &rq) == -1) {
+	if (smb_kops.ko_ioctl(ctx->ct_fd, SMBIOC_LOOKUP, &rq) == -1) {
 		error = errno;
 
-		close(ctx->ct_fd);
+		smb_kops.ko_close(ctx->ct_fd);
 		ctx->ct_fd = -1;
 
 		/*
@@ -689,7 +691,7 @@
 
 		bcopy(&ctx->ct_ssn, &rq.ioc_ssn, sizeof(struct smbioc_ossn));
 
-		if (ioctl(ctx->ct_fd, SMBIOC_LOOKUP, &rq) != -1)
+		if (smb_kops.ko_ioctl(ctx->ct_fd, SMBIOC_LOOKUP, &rq) != -1)
 			goto success;
 		error = errno;
 
@@ -715,7 +717,7 @@
 		return EINVAL;
 	}
 	if (ctx->ct_fd != -1) {
-		close(ctx->ct_fd);
+		smb_kops.ko_close(ctx->ct_fd);
 		ctx->ct_fd = -1;
 	}
 	error = smb_ctx_gethandle(ctx);
@@ -723,14 +725,14 @@
 		smb_error("can't get handle to requester", 0);
 		return EINVAL;
 	}
-	if (ioctl(ctx->ct_fd, SMBIOC_OPENSESSION, ssn) == -1) {
+	if (smb_kops.ko_ioctl(ctx->ct_fd, SMBIOC_OPENSESSION, ssn) == -1) {
 		error = errno;
 		smb_error("can't open session to server %s", error, ssn->ioc_srvname);
 		return error;
 	}
 	if (sh->ioc_share[0] == 0)
 		return 0;
-	if (ioctl(ctx->ct_fd, SMBIOC_OPENSHARE, sh) == -1) {
+	if (smb_kops.ko_ioctl(ctx->ct_fd, SMBIOC_OPENSHARE, sh) == -1) {
 		error = errno;
 		smb_error("can't connect to share //%s/%s", error,
 		    ssn->ioc_srvname, sh->ioc_share);
@@ -749,7 +751,7 @@
 	fl.ioc_level = level;
 	fl.ioc_mask = mask;
 	fl.ioc_flags = flags;
-	if (ioctl(ctx->ct_fd, SMBIOC_SETFLAGS, &fl) == -1)
+	if (smb_kops.ko_ioctl(ctx->ct_fd, SMBIOC_SETFLAGS, &fl) == -1)
 		return errno;
 	return 0;
 }

Index: src/dist/smbfs/lib/smb/file.c
diff -u src/dist/smbfs/lib/smb/file.c:1.3 src/dist/smbfs/lib/smb/file.c:1.4
--- src/dist/smbfs/lib/smb/file.c:1.3	Wed May 10 06:24:02 2006
+++ src/dist/smbfs/lib/smb/file.c	Sun Sep  6 18:38:17 2009
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: file.c,v 1.3 2006/05/10 06:24:02 skrll Exp $");
+__RCSID("$NetBSD: file.c,v 1.4 2009/09/06 18:38:17 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/sysctl.h>
@@ -54,6 +54,8 @@
 #include <netsmb/smb_conn.h>
 #include <cflib.h>
 
+#include "smb_kernelops.h"
+
 int
 smb_read(struct smb_ctx *ctx, smbfh fh, off_t offset, size_t count, char *dst)
 {
@@ -63,7 +65,7 @@
 	rwrq.ioc_base = dst;
 	rwrq.ioc_cnt = count;
 	rwrq.ioc_offset = offset;
-	if (ioctl(ctx->ct_fd, SMBIOC_READ, &rwrq) == -1)
+	if (smb_kops.ko_ioctl(ctx->ct_fd, SMBIOC_READ, &rwrq) == -1)
 		return -1;
 	return rwrq.ioc_cnt;
 }
@@ -78,7 +80,7 @@
 	rwrq.ioc_base = __UNCONST(src);
 	rwrq.ioc_cnt = count;
 	rwrq.ioc_offset = offset;
-	if (ioctl(ctx->ct_fd, SMBIOC_WRITE, &rwrq) == -1)
+	if (smb_kops.ko_ioctl(ctx->ct_fd, SMBIOC_WRITE, &rwrq) == -1)
 		return -1;
 	return rwrq.ioc_cnt;
 }

Index: src/dist/smbfs/lib/smb/nbns_rq.c
diff -u src/dist/smbfs/lib/smb/nbns_rq.c:1.6 src/dist/smbfs/lib/smb/nbns_rq.c:1.7
--- src/dist/smbfs/lib/smb/nbns_rq.c:1.6	Sun Sep  6 17:02:36 2009
+++ src/dist/smbfs/lib/smb/nbns_rq.c	Sun Sep  6 18:38:17 2009
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: nbns_rq.c,v 1.6 2009/09/06 17:02:36 pooka Exp $");
+__RCSID("$NetBSD: nbns_rq.c,v 1.7 2009/09/06 18:38:17 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/socket.h>
@@ -53,6 +53,7 @@
 #include <netsmb/smb_lib.h>
 #include <netsmb/nb_lib.h>
 
+#include "smb_kernelops.h"
 
 static int  nbns_rq_create(int opcode, struct nb_ctx *ctx, struct nbns_rq **rqpp);
 static void nbns_rq_done(struct nbns_rq *rqp);
@@ -173,7 +174,7 @@
 	if (rqp == NULL)
 		return;
 	if (rqp->nr_fd >= 0)
-		close(rqp->nr_fd);
+		smb_kops.ko_close(rqp->nr_fd);
 	mb_done(&rqp->nr_rq);
 	mb_done(&rqp->nr_rp);
 	free(rqp);
@@ -264,7 +265,7 @@
 	int n, len;
 
 	len = sizeof(sender);
-	n = recvfrom(s, rpdata, mbp->mb_top->m_maxlen, 0,
+	n = smb_kops.ko_recvfrom(s, rpdata, mbp->mb_top->m_maxlen, 0,
 	    (struct sockaddr*)&sender, &len);
 	if (n < 0) {
 		if (errno == EAGAIN)
@@ -283,16 +284,18 @@
 	struct timeval tv;
 	int opt, s;
 
-	s = rqp->nr_fd = socket(AF_INET, SOCK_DGRAM, 0);
+	s = rqp->nr_fd = smb_kops.ko_socket(AF_INET, SOCK_DGRAM, 0);
 	if (s < 0)
 		return errno;
 	if (rqp->nr_flags & NBRQF_BROADCAST) {
 		opt = 1;
-		if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt)) < 0)
+		if (smb_kops.ko_setsockopt(s, SOL_SOCKET, SO_BROADCAST,
+		    &opt, sizeof(opt)) < 0)
 			return errno;
 		tv.tv_sec = rqp->nr_nbd->nb_timo;
 		tv.tv_usec = 0;
-		if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
+		if (smb_kops.ko_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO,
+		    &tv, sizeof(tv)) < 0)
 			return errno;
 		if (rqp->nr_if == NULL)
 			return NBERROR(NBERR_NOBCASTIFS);
@@ -301,7 +304,7 @@
 		locaddr.sin_len = sizeof(locaddr);
 		locaddr.sin_addr = rqp->nr_if->id_addr;
 		rqp->nr_dest.sin_addr.s_addr = rqp->nr_if->id_addr.s_addr | ~rqp->nr_if->id_mask.s_addr;
-		if (bind(s, (struct sockaddr*)&locaddr, sizeof(locaddr)) < 0)
+		if (smb_kops.ko_bind(s, (struct sockaddr*)&locaddr, sizeof(locaddr)) < 0)
 			return errno;
 	}
 	return 0;
@@ -313,7 +316,7 @@
 	struct mbdata *mbp = &rqp->nr_rq;
 	int s = rqp->nr_fd;
 
-	if (sendto(s, mtod(mbp->mb_top, char *), mbp->mb_count, 0,
+	if (smb_kops.ko_sendto(s, mtod(mbp->mb_top, char *), mbp->mb_count, 0,
 	      (struct sockaddr*)&rqp->nr_dest, sizeof(rqp->nr_dest)) < 0)
 		return errno;
 	return 0;
@@ -344,7 +347,7 @@
 				    rqp->nr_if != NULL &&
 				    rqp->nr_if->id_next != NULL) {
 					rqp->nr_if = rqp->nr_if->id_next;
-					close(rqp->nr_fd);
+					smb_kops.ko_close(rqp->nr_fd);
 					goto again;
 				} else
 					return error;

Index: src/dist/smbfs/lib/smb/rq.c
diff -u src/dist/smbfs/lib/smb/rq.c:1.4 src/dist/smbfs/lib/smb/rq.c:1.5
--- src/dist/smbfs/lib/smb/rq.c:1.4	Wed May 10 06:24:02 2006
+++ src/dist/smbfs/lib/smb/rq.c	Sun Sep  6 18:38:17 2009
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: rq.c,v 1.4 2006/05/10 06:24:02 skrll Exp $");
+__RCSID("$NetBSD: rq.c,v 1.5 2009/09/06 18:38:17 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/ioctl.h>
@@ -51,6 +51,7 @@
 #include <netsmb/smb_conn.h>
 #include <netsmb/smb_rap.h>
 
+#include "smb_kernelops.h"
 
 int
 smb_rq_init(struct smb_ctx *ctx, u_char cmd, size_t rpbufsz, struct smb_rq **rqpp)
@@ -143,7 +144,7 @@
 	mbp = smb_rq_getreply(rqp);
 	krq.ioc_rpbufsz = mbp->mb_top->m_maxlen;
 	krq.ioc_rpbuf = mtod(mbp->mb_top, char *);
-	if (ioctl(rqp->rq_ctx->ct_fd, SMBIOC_REQUEST, &krq) == -1)
+	if (smb_kops.ko_ioctl(rqp->rq_ctx->ct_fd, SMBIOC_REQUEST, &krq) == -1)
 		return errno;
 	mbp->mb_top->m_len = krq.ioc_rwc * 2 + krq.ioc_rbc;
 	rqp->rq_wcount = krq.ioc_rwc;
@@ -173,7 +174,7 @@
 	krq.ioc_rparam = rparam;
 	krq.ioc_rdatacnt = *rdatacnt;
 	krq.ioc_rdata = rdata;
-	if (ioctl(ctx->ct_fd, SMBIOC_T2RQ, &krq) == -1)
+	if (smb_kops.ko_ioctl(ctx->ct_fd, SMBIOC_T2RQ, &krq) == -1)
 		return errno;
 	*rparamcnt = krq.ioc_rparamcnt;
 	*rdatacnt = krq.ioc_rdatacnt;

Added files:

Index: src/dist/smbfs/include/smb_kernelops.h
diff -u /dev/null src/dist/smbfs/include/smb_kernelops.h:1.1
--- /dev/null	Sun Sep  6 18:38:17 2009
+++ src/dist/smbfs/include/smb_kernelops.h	Sun Sep  6 18:38:17 2009
@@ -0,0 +1,52 @@
+/*      $NetBSD: smb_kernelops.h,v 1.1 2009/09/06 18:38:17 pooka Exp $        */
+
+/*
+ * Copyright (c) 2009 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _SMBFS_KERNEL_OPS_H_
+#define _SMBFS_KERNEL_OPS_H_
+
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <fcntl.h>
+
+struct smb_kernelops {
+	int (*ko_open)(const char *, int, mode_t);
+	int (*ko_ioctl)(int, unsigned long, void *);
+	int (*ko_close)(int);
+
+	int (*ko_socket)(int, int, int);
+	int (*ko_setsockopt)(int, int, int, const void *, socklen_t);
+	int (*ko_bind)(int, const struct sockaddr *, socklen_t);
+	ssize_t (*ko_sendto)(int, const void *, size_t, int,
+			     const struct sockaddr *, socklen_t);
+	ssize_t (*ko_recvfrom)(int, void *, size_t, int,
+			      struct sockaddr *, socklen_t *);
+};
+extern const struct smb_kernelops smb_kops;
+
+#endif /* _SMBFS_KERNEL_OPS_H_ */

Index: src/dist/smbfs/lib/smb/smb_kernelops.c
diff -u /dev/null src/dist/smbfs/lib/smb/smb_kernelops.c:1.1
--- /dev/null	Sun Sep  6 18:38:17 2009
+++ src/dist/smbfs/lib/smb/smb_kernelops.c	Sun Sep  6 18:38:17 2009
@@ -0,0 +1,53 @@
+/*	$NetBSD: smb_kernelops.c,v 1.1 2009/09/06 18:38:17 pooka Exp $	*/
+
+/*
+ * Copyright (c) 2009 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+__RCSID("$NetBSD: smb_kernelops.c,v 1.1 2009/09/06 18:38:17 pooka Exp $");
+#endif /* !lint */
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "smb_kernelops.h"
+
+const struct smb_kernelops smb_kops = {
+	.ko_open = (void *)open, /* XXX: rump_syscalls prototype */
+	.ko_ioctl = (void *)ioctl,
+	.ko_close = close,
+
+	.ko_socket = socket,
+	.ko_setsockopt = setsockopt,
+	.ko_bind = bind,
+	.ko_sendto = sendto,
+	.ko_recvfrom = recvfrom,
+};

Reply via email to