jeudi 25 février 2016

Mounting and Umounting devices with Libudev

Good afternoon,

I'm going through the scarce libudev examples and documentation and I am finding it lacking any information on mounting and umounting devices.

My application may involve the SD card being accidentally ejected without prior notification and the application needs to detect that the SD card has been ejected, close all file descriptors (FILE* objects) writing to the SD card, Umount the SD card, monitor for the SD card to be re-inserted and re-open those file descriptors and continue writing from the end of the file descriptor.

I know this is not an ideal way of ejecting the SD card, but this is designed for accidental ejection. I have already tested that the SD card can be removed while the File Descriptors are open and writing. If the file descriptors are first closed, the SD card can be umounted while it's ejected. Then the application can detect the SD card is inserted, mount it again, and re-open the file descriptors.

It would be better to know if there are multiple SD card states, for example: SD card is inserted, but SD_DETECT pin is high, meaning the card is in the process of being ejected but the rest of the pins are still making contact. However, if this is not an option, the method described above is perfectly fine for this application, even if a write operation writes some corrupt data. This is not meant to happen regularly, just as a precaution. it's also worthwhile noting that this application does not require immediate interrupts when these events occur. The operating system can handle write() calls in the absence of the SD card, therefore the file descriptors do not need to be immediately closed. A simple thread with a while loop checking the card status with a 200ms sleep function inside is sufficient.

Next, my questions:

I can find that the SD card is inserted using the following function:

bool CheckSD(struct udev_device *device)
{
    bool retVal = false;
    struct udev_list_entry *list_entry = 0;
    struct udev_list_entry* model_entry = 0;

    list_entry = udev_device_get_properties_list_entry(device);
    model_entry = udev_list_entry_get_by_name(list_entry, "DEVNAME");

    if (0 != model_entry)
    {
        const char* szModelValue = udev_list_entry_get_value(model_entry);
        if (strcmp(szModelValue, "/dev/mmcblk1") == 0)
        {
            return  true;
        }
    }
    return false;
}

However, if I perform a loop enumerating each device entry,

    udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device))
            printf("%s=%s\n", udev_list_entry_get_name(list_entry), udev_list_entry_get_value(list_entry));

There is no entry that signifies whether the sd card is mounted or not mounted.

What is the proper way to check the device is mounted with libudev?

I would also like to be able to mount and umount with libudev instead of using system calls, which may not always work if the resource is busy.

Is there a way to mount and umount with libudev?

Some additional information: The manual located at http://ift.tt/1OyOYY9 also seems to be partially missing from the website

There is a pretty good user tutorial, but it doesn't cover mounting devices http://ift.tt/Nn0PBd

I'm running a Yocto (OpenEmbedded) linux build on Imx6 platform

The output of the printf statements in the second line of code is:

DEVLINKS=/dev/disk/by-id/mmc-SU02G_0x09f36779 /dev/disk/by-path/platform-2194000.usdhc
DEVNAME=/dev/mmcblk1
DEVPATH=/devices/soc0/soc.1/2100000.aips-bus/2194000.usdhc/mmc_host/mmc1/mmc1:aaaa/block/mmcblk1
DEVTYPE=disk
ID_NAME=SU02G
ID_PART_TABLE_TYPE=dos
ID_PATH=platform-2194000.usdhc
ID_PATH_TAG=platform-2194000_usdhc
ID_SERIAL=0x09f36779
MAJOR=179
MINOR=32
SUBSYSTEM=block
USEC_INITIALIZED=3348892

Aucun commentaire:

Enregistrer un commentaire