geom_util.c (9ac6b8ae23da47a9675e861b58cf4c3038a68e28) | geom_util.c (f805f204b63aaab5b49c7371deb8c2fd015bd894) |
---|---|
1/*- 2 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 28 unchanged lines hidden (view full) --- 37#include <stdint.h> 38#include <unistd.h> 39#include <string.h> 40#include <stdlib.h> 41#include <paths.h> 42 43#include <libgeom.h> 44 | 1/*- 2 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 28 unchanged lines hidden (view full) --- 37#include <stdint.h> 38#include <unistd.h> 39#include <string.h> 40#include <stdlib.h> 41#include <paths.h> 42 43#include <libgeom.h> 44 |
45static char *g_device_path_open(const char *, int *, int); 46 |
|
45/* 46 * Open the given provider and at least check if this is a block device. 47 */ 48int 49g_open(const char *name, int dowrite) 50{ | 47/* 48 * Open the given provider and at least check if this is a block device. 49 */ 50int 51g_open(const char *name, int dowrite) 52{ |
51 char path[MAXPATHLEN]; | 53 char *path; |
52 int fd; 53 | 54 int fd; 55 |
54 if (name[0] == '/') 55 strlcpy(path, name, sizeof(path)); 56 else 57 snprintf(path, sizeof(path), "%s%s", _PATH_DEV, name); 58 59 fd = open(path, dowrite ? O_RDWR : O_RDONLY); | 56 path = g_device_path_open(name, &fd, dowrite); 57 if (path != NULL) 58 free(path); |
60 if (fd == -1) 61 return (-1); | 59 if (fd == -1) 60 return (-1); |
62 /* Let try to get sectorsize, which will prove it is a GEOM provider. */ 63 if (g_sectorsize(fd) == -1) { 64 close(fd); 65 errno = EFTYPE; 66 return (-1); 67 } | |
68 return (fd); 69} 70 71int 72g_close(int fd) 73{ 74 75 return (close(fd)); --- 40 unchanged lines hidden (view full) --- 116 u_int sectorsize; 117 118 if (g_ioctl_arg(fd, DIOCGSECTORSIZE, §orsize) == -1) 119 return (-1); 120 return ((ssize_t)sectorsize); 121} 122 123/* | 61 return (fd); 62} 63 64int 65g_close(int fd) 66{ 67 68 return (close(fd)); --- 40 unchanged lines hidden (view full) --- 109 u_int sectorsize; 110 111 if (g_ioctl_arg(fd, DIOCGSECTORSIZE, §orsize) == -1) 112 return (-1); 113 return ((ssize_t)sectorsize); 114} 115 116/* |
117 * Return the correct provider name. 118 */ 119char * 120g_providername(int fd) 121{ 122 char name[MAXPATHLEN]; 123 124 if (g_ioctl_arg(fd, DIOCGPROVIDERNAME, name) == -1) 125 return (NULL); 126 return (strdup(name)); 127} 128 129/* |
|
124 * Call BIO_FLUSH for the given provider. 125 */ 126int 127g_flush(int fd) 128{ 129 130 return (g_ioctl(fd, DIOCGFLUSH)); 131} --- 97 unchanged lines hidden (view full) --- 229end: 230 geom_deletetree(&mesh); 231 if (error != 0) { 232 errno = error; 233 return (-1); 234 } 235 return (fd); 236} | 130 * Call BIO_FLUSH for the given provider. 131 */ 132int 133g_flush(int fd) 134{ 135 136 return (g_ioctl(fd, DIOCGFLUSH)); 137} --- 97 unchanged lines hidden (view full) --- 235end: 236 geom_deletetree(&mesh); 237 if (error != 0) { 238 errno = error; 239 return (-1); 240 } 241 return (fd); 242} |
243 244/* 245 * Return the device path device given a partial or full path to its node. 246 * A pointer can be provided, which will be set to an opened file descriptor of 247 * not NULL. 248 */ 249static char * 250g_device_path_open(const char *devpath, int *fdp, int dowrite) 251{ 252 char *path; 253 int fd; 254 255 /* Make sure that we can fail. */ 256 if (fdp != NULL) 257 *fdp = -1; 258 /* Use the device node if we're able to open it. */ 259 do { 260 fd = open(devpath, dowrite ? O_RDWR : O_RDONLY); 261 if (fd == -1) 262 break; 263 /* 264 * Let try to get sectorsize, which will prove it is a GEOM 265 * provider. 266 */ 267 if (g_sectorsize(fd) == -1) { 268 close(fd); 269 errno = EFTYPE; 270 return (NULL); 271 } 272 if ((path = strdup(devpath)) == NULL) { 273 close(fd); 274 return (NULL); 275 } 276 if (fdp != NULL) 277 *fdp = fd; 278 else 279 close(fd); 280 return (path); 281 } while (0); 282 283 /* If we're not given an absolute path, assume /dev/ prefix. */ 284 if (*devpath != '/') { 285 asprintf(&path, "%s%s", _PATH_DEV, devpath); 286 if (path == NULL) 287 return (NULL); 288 fd = open(path, dowrite ? O_RDWR : O_RDONLY); 289 if (fd == -1) { 290 free(path); 291 return (NULL); 292 } 293 /* 294 * Let try to get sectorsize, which will prove it is a GEOM 295 * provider. 296 */ 297 if (g_sectorsize(fd) == -1) { 298 free(path); 299 close(fd); 300 errno = EFTYPE; 301 return (NULL); 302 } 303 if (fdp != NULL) 304 *fdp = fd; 305 else 306 close(fd); 307 return (path); 308 } 309 return (NULL); 310} 311 312char * 313g_device_path(const char *devpath) 314{ 315 return (g_device_path_open(devpath, NULL, 0)); 316} |
|