Module Name: src
Committed By: christos
Date: Mon Dec 29 16:27:06 UTC 2014
Modified Files:
src/sbin/gpt: Makefile gpt.c gpt.h
Added Files:
src/sbin/gpt: drvctl.c
Log Message:
Factor out the getdisksize() drvctl method, and provide an alternative that
directly uses the disk ioctl's instead of relying on the drvctl device driver
which is currently not mandatory.
To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/sbin/gpt/Makefile
cvs rdiff -u -r0 -r1.1 src/sbin/gpt/drvctl.c
cvs rdiff -u -r1.39 -r1.40 src/sbin/gpt/gpt.c
cvs rdiff -u -r1.17 -r1.18 src/sbin/gpt/gpt.h
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/sbin/gpt/Makefile
diff -u src/sbin/gpt/Makefile:1.12 src/sbin/gpt/Makefile:1.13
--- src/sbin/gpt/Makefile:1.12 Tue Sep 30 13:59:59 2014
+++ src/sbin/gpt/Makefile Mon Dec 29 11:27:06 2014
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.12 2014/09/30 17:59:59 christos Exp $
+# $NetBSD: Makefile,v 1.13 2014/12/29 16:27:06 christos Exp $
# $FreeBSD: src/sbin/gpt/Makefile,v 1.7 2005/09/01 02:49:20 marcel Exp $
PROG= gpt
@@ -11,6 +11,16 @@ MAN= gpt.8
SRCS+= backup.c restore.c
LDADD+= -lprop -lutil
DPADD+= ${LIBPROP} ${LIBUTIL}
+
+.if ${USE_DRVCTL:Uno} == "yes"
+CPPFLAGS+=-DUSE_DRVCTL
+SRCS+=drvctl.c
+.else
+.PATH: ${.CURDIR}/../fsck
+CPPFLAGS+=-I${.CURDIR}/../fsck
+SRCS+=partutil.c
+.endif
.endif
+
.include <bsd.prog.mk>
Index: src/sbin/gpt/gpt.c
diff -u src/sbin/gpt/gpt.c:1.39 src/sbin/gpt/gpt.c:1.40
--- src/sbin/gpt/gpt.c:1.39 Mon Nov 17 02:15:28 2014
+++ src/sbin/gpt/gpt.c Mon Dec 29 11:27:06 2014
@@ -35,7 +35,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/gpt.c,v 1.16 2006/07/07 02:44:23 marcel Exp $");
#endif
#ifdef __RCSID
-__RCSID("$NetBSD: gpt.c,v 1.39 2014/11/17 07:15:28 mlelstv Exp $");
+__RCSID("$NetBSD: gpt.c,v 1.40 2014/12/29 16:27:06 christos Exp $");
#endif
#include <sys/param.h>
@@ -54,11 +54,6 @@ __RCSID("$NetBSD: gpt.c,v 1.39 2014/11/1
#include <string.h>
#include <unistd.h>
#include <ctype.h>
-#ifndef HAVE_NBTOOL_CONFIG_H
-#include <prop/proplib.h>
-#include <sys/drvctlio.h>
-#include <sys/disk.h>
-#endif
#include "map.h"
#include "gpt.h"
@@ -364,96 +359,6 @@ gpt_mbr(int fd, off_t lba)
return (0);
}
-#ifndef HAVE_NBTOOL_CONFIG_H
-static int
-drvctl(const char *name, u_int *sector_size, off_t *media_size)
-{
- prop_dictionary_t command_dict, args_dict, results_dict, data_dict,
- disk_info, geometry;
- prop_string_t string;
- prop_number_t number;
- int dfd, res;
- char *dname, *p;
-
- if (*sector_size && *media_size)
- return 0;
-
- if ((dfd = open("/dev/drvctl", O_RDONLY)) == -1) {
- warn("%s: /dev/drvctl", __func__);
- return -1;
- }
-
- command_dict = prop_dictionary_create();
- args_dict = prop_dictionary_create();
-
- string = prop_string_create_cstring_nocopy("get-properties");
- prop_dictionary_set(command_dict, "drvctl-command", string);
- prop_object_release(string);
-
- if ((dname = strdup(name[0] == 'r' ? name + 1 : name)) == NULL) {
- (void)close(dfd);
- return -1;
- }
- for (p = dname; *p; p++)
- continue;
- for (--p; p >= dname && !isdigit((unsigned char)*p); *p-- = '\0')
- continue;
-
- string = prop_string_create_cstring(dname);
- free(dname);
- prop_dictionary_set(args_dict, "device-name", string);
- prop_object_release(string);
-
- prop_dictionary_set(command_dict, "drvctl-arguments", args_dict);
- prop_object_release(args_dict);
-
- res = prop_dictionary_sendrecv_ioctl(command_dict, dfd, DRVCTLCOMMAND,
- &results_dict);
- (void)close(dfd);
- prop_object_release(command_dict);
- if (res) {
- warn("%s: prop_dictionary_sendrecv_ioctl", __func__);
- errno = res;
- return -1;
- }
-
- number = prop_dictionary_get(results_dict, "drvctl-error");
- if ((errno = prop_number_integer_value(number)) != 0)
- return -1;
-
- data_dict = prop_dictionary_get(results_dict, "drvctl-result-data");
- if (data_dict == NULL)
- goto out;
-
- disk_info = prop_dictionary_get(data_dict, "disk-info");
- if (disk_info == NULL)
- goto out;
-
- geometry = prop_dictionary_get(disk_info, "geometry");
- if (geometry == NULL)
- goto out;
-
- number = prop_dictionary_get(geometry, "sector-size");
- if (number == NULL)
- goto out;
-
- if (*sector_size == 0)
- *sector_size = prop_number_integer_value(number);
-
- number = prop_dictionary_get(geometry, "sectors-per-unit");
- if (number == NULL)
- goto out;
-
- if (*media_size == 0)
- *media_size = prop_number_integer_value(number) * *sector_size;
-
- return 0;
-out:
- errno = EINVAL;
- return -1;
-}
-#endif
-
int
gpt_gpt(int fd, off_t lba, int found)
{
@@ -580,9 +485,8 @@ gpt_open(const char *dev)
if ((secsz == 0 && ioctl(fd, DIOCGSECTORSIZE, &secsz) == -1) ||
(mediasz == 0 && ioctl(fd, DIOCGMEDIASIZE, &mediasz) == -1))
goto close;
-#endif
-#ifndef HAVE_NBTOOL_CONFIG_H
- if (drvctl(device_name, &secsz, &mediasz) == -1)
+#else
+ if (getdisksize(device_name, &secsz, &mediasz) == -1)
goto close;
#endif
if (secsz == 0 || mediasz == 0)
Index: src/sbin/gpt/gpt.h
diff -u src/sbin/gpt/gpt.h:1.17 src/sbin/gpt/gpt.h:1.18
--- src/sbin/gpt/gpt.h:1.17 Mon Nov 17 02:15:28 2014
+++ src/sbin/gpt/gpt.h Mon Dec 29 11:27:06 2014
@@ -94,4 +94,14 @@ int cmd_show(int, char *[]);
int cmd_type(int, char *[]);
int cmd_unset(int, char *[]);
+#ifndef HAVE_NBTOOL_CONFIG_H
+# ifdef USE_DRVCTL
+int getdisksize(const char *, u_int *, off_t *);
+# else
+# include "partutil.h"
+# endif
+#else
+# define getdisksize(a, b, c) 0
+#endif
+
#endif /* _GPT_H_ */
Added files:
Index: src/sbin/gpt/drvctl.c
diff -u /dev/null src/sbin/gpt/drvctl.c:1.1
--- /dev/null Mon Dec 29 11:27:06 2014
+++ src/sbin/gpt/drvctl.c Mon Dec 29 11:27:06 2014
@@ -0,0 +1,134 @@
+/* $NetBSD: drvctl.c,v 1.1 2014/12/29 16:27:06 christos Exp $ */
+
+/*-
+ * Copyright (c) 2014 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>
+__RCSID("$NetBSD: drvctl.c,v 1.1 2014/12/29 16:27:06 christos Exp $");
+
+#include <sys/types.h>
+#include <sys/disk.h>
+#include <sys/drvctlio.h>
+
+#include <prop/proplib.h>
+
+#include <errno.h>
+#include <err.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <ctype.h>
+
+#include "map.h"
+#include "gpt.h"
+
+int
+getdisksize(const char *name, u_int *sector_size, off_t *media_size)
+{
+ prop_dictionary_t command_dict, args_dict, results_dict, data_dict,
+ disk_info, geometry;
+ prop_string_t string;
+ prop_number_t number;
+ int dfd, res;
+ char *dname, *p;
+
+ if (*sector_size && *media_size)
+ return 0;
+
+ if ((dfd = open("/dev/drvctl", O_RDONLY)) == -1) {
+ warn("%s: /dev/drvctl", __func__);
+ return -1;
+ }
+
+ command_dict = prop_dictionary_create();
+ args_dict = prop_dictionary_create();
+
+ string = prop_string_create_cstring_nocopy("get-properties");
+ prop_dictionary_set(command_dict, "drvctl-command", string);
+ prop_object_release(string);
+
+ if ((dname = strdup(name[0] == 'r' ? name + 1 : name)) == NULL) {
+ (void)close(dfd);
+ return -1;
+ }
+ for (p = dname; *p; p++)
+ continue;
+ for (--p; p >= dname && !isdigit((unsigned char)*p); *p-- = '\0')
+ continue;
+
+ string = prop_string_create_cstring(dname);
+ free(dname);
+ prop_dictionary_set(args_dict, "device-name", string);
+ prop_object_release(string);
+
+ prop_dictionary_set(command_dict, "drvctl-arguments", args_dict);
+ prop_object_release(args_dict);
+
+ res = prop_dictionary_sendrecv_ioctl(command_dict, dfd, DRVCTLCOMMAND,
+ &results_dict);
+ (void)close(dfd);
+ prop_object_release(command_dict);
+ if (res) {
+ warn("%s: prop_dictionary_sendrecv_ioctl", __func__);
+ errno = res;
+ return -1;
+ }
+
+ number = prop_dictionary_get(results_dict, "drvctl-error");
+ if ((errno = prop_number_integer_value(number)) != 0)
+ return -1;
+
+ data_dict = prop_dictionary_get(results_dict, "drvctl-result-data");
+ if (data_dict == NULL)
+ goto out;
+
+ disk_info = prop_dictionary_get(data_dict, "disk-info");
+ if (disk_info == NULL)
+ goto out;
+
+ geometry = prop_dictionary_get(disk_info, "geometry");
+ if (geometry == NULL)
+ goto out;
+
+ number = prop_dictionary_get(geometry, "sector-size");
+ if (number == NULL)
+ goto out;
+
+ if (*sector_size == 0)
+ *sector_size = prop_number_integer_value(number);
+
+ number = prop_dictionary_get(geometry, "sectors-per-unit");
+ if (number == NULL)
+ goto out;
+
+ if (*media_size == 0)
+ *media_size = prop_number_integer_value(number) * *sector_size;
+
+ return 0;
+out:
+ errno = EINVAL;
+ return -1;
+}