public inbox for cluster-cvs@sourceware.org help / color / mirror / Atom feed
From: "Fabio M. Di Nitto" <fabbione@fedoraproject.org> To: cluster-cvs-relay@redhat.com Subject: gfs1-utils: master - mkfs.gfs: Remove dep on libvolume_id Date: Mon, 15 Jun 2009 17:22:00 -0000 [thread overview] Message-ID: <20090615172202.E62011201B7@lists.fedorahosted.org> (raw) Gitweb: http://git.fedorahosted.org/git/gfs1-utils.git?p=gfs1-utils.git;a=commitdiff;h=62e05a2daa6c70cc828d61be4a172e969762dfeb Commit: 62e05a2daa6c70cc828d61be4a172e969762dfeb Parent: 41d73ce1e18ea69ff12be6529fba64806e41b892 Author: Steven Whitehouse <swhiteho@redhat.com> AuthorDate: Fri Jun 12 13:33:32 2009 +0100 Committer: Fabio M. Di Nitto <fdinitto@redhat.com> CommitterDate: Mon Jun 15 19:21:52 2009 +0200 mkfs.gfs: Remove dep on libvolume_id We had been using libvolume_id to look at the content of a device or file before mkfs makes any changes to give the user a hint that they are really overwriting the correct thing. Since libvolume_id has been removed from rawhide, Fabio has asked that we remove the dependency from here. In order to do that, I've replaced the call to libvolume_id with a fork()/exec() of file(1) from the GNU file utils. This can identify a wider range of files/fs formats and thus will print a more informative message. There is no new dependency created though: if file(1) doesn't exist then we just print a message saying that we can't identify what is in the device/file and carry on as normal. This might be a bit rough at the edges, but it does the job and should fix the problem for now. Signed-off-by: Steven Whitehouse <swhiteho@redhat.com> Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com> --- gfs/gfs_mkfs/Makefile | 2 +- gfs/gfs_mkfs/device_geometry.c | 2 +- gfs/gfs_mkfs/main.c | 128 ++++++++++++++++++++++++++++++++-------- 3 files changed, 105 insertions(+), 27 deletions(-) diff --git a/gfs/gfs_mkfs/Makefile b/gfs/gfs_mkfs/Makefile index f963a59..e679051 100644 --- a/gfs/gfs_mkfs/Makefile +++ b/gfs/gfs_mkfs/Makefile @@ -17,7 +17,7 @@ OBJS= device_geometry.o \ ondisk.o \ structures.o -CFLAGS += -DHELPER_PROGRAM -D_FILE_OFFSET_BITS=64 +CFLAGS += -DHELPER_PROGRAM -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE CFLAGS += -I${gfskincdir} CFLAGS += -I$(S)/../include -I$(S)/../libgfs CFLAGS += -I${incdir} diff --git a/gfs/gfs_mkfs/device_geometry.c b/gfs/gfs_mkfs/device_geometry.c index a3b5259..6152bca 100644 --- a/gfs/gfs_mkfs/device_geometry.c +++ b/gfs/gfs_mkfs/device_geometry.c @@ -30,7 +30,7 @@ void device_geometry(commandline_t *comline, mkfs_device_t *device) uint64 bytes; int error; - fd = open(comline->device, O_RDONLY); + fd = open(comline->device, O_RDONLY | O_CLOEXEC); if (fd < 0) die("can't open %s: %s\n", comline->device, strerror(errno)); diff --git a/gfs/gfs_mkfs/main.c b/gfs/gfs_mkfs/main.c index e9d57de..4c8a8e0 100644 --- a/gfs/gfs_mkfs/main.c +++ b/gfs/gfs_mkfs/main.c @@ -10,11 +10,12 @@ #include <assert.h> #include <time.h> #include <mntent.h> +#include <poll.h> +#include <signal.h> #include "global.h" #include "gfs_ondisk.h" #include "osi_list.h" -#include "libvolume_id.h" #include "libgfs.h" #include "copyright.cf" @@ -197,6 +198,103 @@ static void decode_arguments(int argc, char *argv[], commandline_t *comline) } } +static int get_file_output(int fd, char *buffer, size_t buflen) +{ + struct pollfd pf = { .fd = fd, .events = POLLIN|POLLRDHUP }; + int flags; + int pos = 0; + int rv; + + flags = fcntl(fd, F_GETFL, 0); + if (flags < 0) + return flags; + + flags |= O_NONBLOCK; + rv = fcntl(fd, F_SETFL, flags); + if (rv < 0) + return rv; + + while (1) { + rv = poll(&pf, 1, 10 * 1000); + if (rv == 0) + break; + if (rv < 0) + return rv; + if (pf.revents & POLLIN) { + rv = read(fd, buffer + pos, + buflen - pos); + if (rv < 0) { + if (errno == EAGAIN) + continue; + return rv; + } + if (rv == 0) + break; + pos += rv; + if (pos >= buflen) + return -1; + buffer[pos] = 0; + continue; + } + if (pf.revents & (POLLRDHUP | POLLHUP | POLLERR)) + break; + } + return 0; +} + +static void check_dev_content(const char *devname) +{ + struct sigaction sa; + char content[1024] = { 0, }; + char * const args[] = { + (char *)"/usr/bin/file", + (char *)"-bs", + (char *)devname, + NULL }; + int p[2]; + int ret; + int pid; + + ret = sigaction(SIGCHLD, NULL, &sa); + if (ret) + return; + sa.sa_handler = SIG_IGN; + sa.sa_flags |= (SA_NOCLDSTOP | SA_NOCLDWAIT); + ret = sigaction(SIGCHLD, &sa, NULL); + if (ret) + goto fail; + + ret = pipe(p); + if (ret) + goto fail; + + pid = fork(); + + if (pid < 0) { + close(p[1]); + goto fail; + } + + if (pid) { + close(p[1]); + ret = get_file_output(p[0], content, sizeof(content)); + if (ret) { +fail: + printf("Content of file or device unknown (do you have GNU fileutils installed?)\n"); + } else { + if (*content == 0) + goto fail; + printf("It appears to contain: %s", content); + } + close(p[0]); + return; + } + + close(p[0]); + dup2(p[1], STDOUT_FILENO); + close(STDIN_FILENO); + exit(execv(args[0], args)); +} /** * are_you_sure - protect lusers from themselves @@ -207,34 +305,14 @@ static void decode_arguments(int argc, char *argv[], commandline_t *comline) static void are_you_sure(commandline_t *comline) { char input[32]; - struct volume_id *vid = NULL; int fd; - fd = open(comline->device, O_RDONLY); + fd = open(comline->device, O_RDONLY | O_CLOEXEC); if (fd < 0) die("Error: device %s not found.\n", comline->device); - vid = volume_id_open_fd(fd); - if (vid == NULL) { - close(fd); - die("error identifying the contents of %s: %s\n", - comline->device, strerror(errno)); - } printf("This will destroy any data on %s.\n", comline->device); - if (volume_id_probe_all(vid, 0, MKFS_DEFAULT_BSIZE) == 0) { - const char *fstype, *fsusage; - int rc; - - rc = volume_id_get_type(vid, &fstype); - if (rc) { - rc = volume_id_get_usage(vid, &fsusage); - if (!rc || strncmp(fsusage, "other", 5) == 0) - fsusage = "partition"; - printf(" It appears to contain a %s %s.\n", fstype, - fsusage); - } - } - volume_id_close(vid); + check_dev_content(comline->device); close(fd); printf("\nAre you sure you want to proceed? [y/n] "); if (fgets(input, 32, stdin) == NULL || input[0] != 'y') @@ -260,7 +338,7 @@ static void check_mount(char *device) if (!S_ISBLK(st_buf.st_mode)) die("%s is not a block device\n", device); - fd = open(device, O_RDONLY | O_NONBLOCK | O_EXCL); + fd = open(device, O_RDONLY | O_NONBLOCK | O_CLOEXEC); if (fd < 0) { if (errno == EBUSY) { @@ -392,7 +470,7 @@ int main(int argc, char *argv[]) /* Start writing stuff out */ - comline.fd = open(comline.device, O_RDWR); + comline.fd = open(comline.device, O_RDWR | O_CLOEXEC); if (comline.fd < 0) die("can't open device %s\n", comline.device);
reply other threads:[~2009-06-15 17:22 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20090615172202.E62011201B7@lists.fedorahosted.org \ --to=fabbione@fedoraproject.org \ --cc=cluster-cvs-relay@redhat.com \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).