[stgt] [PATCH 06/11] os.h: Services unavailable in BSD

Boaz Harrosh bharrosh at panasas.com
Thu Mar 12 12:02:06 CET 2009


* ipc_perm => os_ipc_perm
	Let in any request. credential checks are turned
	off in BSD.

* oom_adjust - => os_oom_adjust Not available on BSD

* ioctl(, BLKGETSIZE64) => os_blockdev_size

Signed-off-by: Boaz Harrosh <bharrosh at panasas.com>
---
 usr/bsd/os.c   |   17 +++++++++++++++++
 usr/linux/os.c |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 usr/mgmt.c     |   27 +--------------------------
 usr/os.h       |    7 +++++++
 usr/tgtd.c     |   23 +----------------------
 usr/util.c     |    2 +-
 6 files changed, 79 insertions(+), 49 deletions(-)

diff --git a/usr/bsd/os.c b/usr/bsd/os.c
index 7fbcd5c..7fec458 100644
--- a/usr/bsd/os.c
+++ b/usr/bsd/os.c
@@ -21,6 +21,8 @@
 
 #include <errno.h>
 #include <unistd.h>
+#include <sys/disk.h>
+#include <sys/ioctl.h>
 
 #include <linux/fs.h>
 
@@ -31,3 +33,18 @@ int os_sync_file_range(int fd, __off64_t offset, __off64_t bytes,
 {
 	return fsync(fd);
 }
+
+int os_ipc_perm(int fd)
+{
+	return 0;
+}
+
+int os_oom_adjust(void)
+{
+	return 0;
+}
+
+int os_blockdev_size(int fd, uint64_t *size)
+{
+	return ioctl(fd, DIOCGMEDIASIZE, &size);
+}
diff --git a/usr/linux/os.c b/usr/linux/os.c
index 2094eec..b4027cb 100644
--- a/usr/linux/os.c
+++ b/usr/linux/os.c
@@ -20,10 +20,18 @@
  */
 
 #include <errno.h>
+#include <stdio.h>
+#include <inttypes.h>
 #include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
 #include <sys/syscall.h>
 
+#include <linux/fs.h>
+
 #include "os.h"
+#include "log.h"
 
 /*
  * the latest glibc have a proper sync_file_range definition but
@@ -52,3 +60,47 @@ int os_sync_file_range(int fd, __off64_t offset, __off64_t bytes,
 	return ret;
 }
 
+int os_ipc_perm(int fd)
+{
+	struct ucred cred;
+	socklen_t len;
+	int err;
+
+	len = sizeof(cred);
+	err = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void *) &cred, &len);
+	if (err) {
+		eprintf("can't get sockopt, %m\n");
+		return -1;
+	}
+
+	if (cred.uid || cred.gid)
+		return -EPERM;
+
+	return 0;
+}
+
+int os_oom_adjust(void)
+{
+	int fd, err;
+	char path[64];
+
+	/* Avoid oom-killer */
+	sprintf(path, "/proc/%d/oom_adj", getpid());
+	fd = open(path, O_WRONLY);
+	if (fd < 0) {
+		eprintf("can't adjust oom-killer's pardon %s, %m\n", path);
+		return errno;
+	}
+	err = write(fd, "-17\n", 4);
+	if (err < 0) {
+		eprintf("can't adjust oom-killer's pardon %s, %m\n", path);
+		return errno;
+	}
+	close(fd);
+	return 0;
+}
+
+int os_blockdev_size(int fd, uint64_t *size)
+{
+	return ioctl(fd, BLKGETSIZE64, size);
+}
diff --git a/usr/mgmt.c b/usr/mgmt.c
index 5332dc0..bd98d19 100644
--- a/usr/mgmt.c
+++ b/usr/mgmt.c
@@ -19,19 +19,13 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA
  */
-#include <ctype.h>
-#include <dirent.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
 #include <sys/epoll.h>
 #include <sys/socket.h>
-#include <sys/stat.h>
-#include <sys/types.h>
 #include <sys/un.h>
 
 #include "list.h"
@@ -379,25 +373,6 @@ static int ipc_accept(int accept_fd)
 	return fd;
 }
 
-static int ipc_perm(int fd)
-{
-	struct ucred cred;
-	socklen_t len;
-	int err;
-
-	len = sizeof(cred);
-	err = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void *) &cred, &len);
-	if (err) {
-		eprintf("can't get sockopt, %m\n");
-		return -1;
-	}
-
-	if (cred.uid || cred.gid)
-		return -EPERM;
-
-	return 0;
-}
-
 static void mtask_handler(int fd, int events, void *data)
 {
 	int err, len;
@@ -506,7 +481,7 @@ static void mgmt_event_handler(int accept_fd, int events, void *data)
 		return;
 	}
 
-	err = ipc_perm(fd);
+	err = os_ipc_perm(fd);
 	if (err < 0) {
 		eprintf("permission error\n");
 		goto out;
diff --git a/usr/os.h b/usr/os.h
index c32ce29..03ad898 100644
--- a/usr/os.h
+++ b/usr/os.h
@@ -1,6 +1,7 @@
 #ifndef __TGT_OS_H__
 #define __TGT_OS_H__
 
+#include <inttypes.h>
 #include <sys/types.h>
 #include <linux/fs.h>
 
@@ -12,4 +13,10 @@
 int os_sync_file_range(int fd, __off64_t offset, __off64_t bytes,
 			unsigned int flags);
 
+int os_ipc_perm(int fd);
+
+int os_oom_adjust(void);
+
+int os_blockdev_size(int fd, uint64_t *size);
+
 #endif /* ndef __TGT_OS_H__*/
diff --git a/usr/tgtd.c b/usr/tgtd.c
index 8569d41..d243151 100644
--- a/usr/tgtd.c
+++ b/usr/tgtd.c
@@ -76,27 +76,6 @@ Target framework daemon, version %s\n\
 static void signal_catch(int signo) {
 }
 
-static int oom_adjust(void)
-{
-	int fd, err;
-	char path[64];
-
-	/* Avoid oom-killer */
-	sprintf(path, "/proc/%d/oom_adj", getpid());
-	fd = open(path, O_WRONLY);
-	if (fd < 0) {
-		fprintf(stderr, "can't adjust oom-killer's pardon %s, %m\n", path);
-		return errno;
-	}
-	err = write(fd, "-17\n", 4);
-	if (err < 0) {
-		fprintf(stderr, "can't adjust oom-killer's pardon %s, %m\n", path);
-		return errno;
-	}
-	close(fd);
-	return 0;
-}
-
 static int nr_file_adjust(void)
 {
 	int ret, fd, max = 1024 * 1024;
@@ -368,7 +347,7 @@ int main(int argc, char **argv)
 	if (is_daemon && daemon(0,0))
 		exit(1);
 
-	err = oom_adjust();
+	err = os_oom_adjust();
 	if (err)
 		exit(1);
 
diff --git a/usr/util.c b/usr/util.c
index 2f4f488..ac14a7b 100644
--- a/usr/util.c
+++ b/usr/util.c
@@ -101,7 +101,7 @@ int backed_file_open(char *path, int oflag, uint64_t *size)
 	if (S_ISREG(st.st_mode))
 		*size = st.st_size;
 	else if (S_ISBLK(st.st_mode)) {
-		err = ioctl(fd, BLKGETSIZE64, size);
+		err = os_blockdev_size(fd, size);
 		if (err < 0) {
 			eprintf("Cannot get size, %m\n");
 			goto close_fd;
-- 
1.6.0.6

--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html



More information about the stgt mailing list