[systemd-devel] [PATCH v4] Bootchart: use /proc/pid/mountinfo for root bdev

Lennart Poettering lennart at poettering.net
Thu Dec 4 16:12:22 PST 2014


On Fri, 31.10.14 16:01, Timofey Titovets (nefelim4ag at gmail.com) wrote:

> 
> +void get_rootbdev(char *rootbdev) {
> +        FILE *file = fopen("/proc/self/mountinfo", "r");
> +        char mnt_point[PATH_MAX];
> +        char mnt_source[PATH_MAX];
> +        char t[256]; // Trash
> +        while (file && strcmp(mnt_point, "/") && !feof(file)){
> +        fscanf(file, "%s %s %s %s %s %s %s %s %s %s %s",
> +               t,t,t,t,
> +               mnt_point,  t,t,t,t,
> +               mnt_source, t);
> +        }
> +        if(!strcmp(mnt_point, "/"))
> +                strncpy(rootbdev, &mnt_source[5], 3);
> +        fclose(file);
> +}
> +

Umm, the error handling is missing. We don't invoke functions from
variable definitions. We do not allocate over-sized buffers on the
stack. Either we do it with the right size or with dyanmic memory. We
generally don't use strcmp() for simple equality checks, but
streq(). We use _cleanup_xyz_ to automatically free resources. Please
have a look at the CODING_STYLE document in the repository.

In general, I wouldn't consult mountinfo for this at all. Instead
stat() the root fs, then use udev_device_new_from_devnum() on the
returned st_dev, and read the device symlinks from that.

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list