From d963e58dc5393d770191c2265512f41f9d85d406 Mon Sep 17 00:00:00 2001 From: Hong Jen Yee (PCMan) Date: Fri, 25 Feb 2011 21:39:36 +0800 Subject: [PATCH] Fix #34710 - [udisks] CD-ROM polling failed due to O_EXCL flag (poller.c). --- src/poller.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 54 insertions(+), 0 deletions(-) diff --git a/src/poller.c b/src/poller.c index 9517b5d..01002c5 100644 --- a/src/poller.c +++ b/src/poller.c @@ -31,6 +31,7 @@ #include #include #include +#include #include "poller.h" #include "device.h" @@ -105,6 +106,43 @@ static gchar **poller_devices_to_poll = NULL; static guint poller_timeout_id = 0; + +/** Check if a filesystem on a special device file is mounted + * + * @param device_file Special device file, e.g. /dev/cdrom + * @return TRUE iff there is a filesystem system mounted + * on the special device file + */ +/* Taken from hal/hald/linux/addons/addon-storage.c + * Copyright (C) 2004 David Zeuthen, + * Licensed under the Academic Free License version 2.1 +*/ +static gboolean +is_mounted (const char *device_file) +{ + FILE *f; + gboolean rc; + struct mntent mnt; + char buf[512]; + + rc = FALSE; + + if ((f = setmntent ("/etc/mtab", "r")) == NULL) + goto out; + + while (getmntent_r (f, &mnt, buf, sizeof(buf)) != NULL) { + if (strcmp (device_file, mnt.mnt_fsname) == 0) { + rc = TRUE; + goto out1; + } + } + +out1: + endmntent (f); +out: + return rc; +} + static void poller_poll_device (const gchar *device_file) { @@ -126,6 +164,22 @@ poller_poll_device (const gchar *device_file) * - use O_EXCL to avoid interferring with cd burning software / audio playback / etc */ fd = open (device_file, O_RDONLY | O_NONBLOCK | O_EXCL); + if (fd == -1 && errno == EBUSY) + { + /* From hal/hald/linux/addons/addon-storage.c: */ + /* this means the disc is mounted or some other app, + * like a cd burner, has already opened O_EXCL */ + + /* HOWEVER, when starting hald, a disc may be + * mounted; so check /etc/mtab to see if it + * actually is mounted. If it is we retry to open + * without O_EXCL + */ + if (is_mounted (device_file)) + { + fd = open (device_file, O_RDONLY | O_NONBLOCK); + } + } if (fd != -1) { close (fd); -- 1.7.4.1