From f7f29b564073831c9d1a8cdf9deaf039c0bcb1b6 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 28 Jun 2012 15:40:16 +0200 Subject: [PATCH] libmount: save task ID for mountinfo tiles Add new function mnt_fs_get_tid() to get task ID for the given filesystem. Signed-off-by: Karel Zak --- libmount/src/fs.c | 18 +++++++++++++++++- libmount/src/libmount.h.in | 1 + libmount/src/libmount.sym | 5 +++-- libmount/src/mountP.h | 1 + libmount/src/tab_parse.c | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 3 deletions(-) diff --git a/libmount/src/fs.c b/libmount/src/fs.c index 543ffb1537..ff930806fc 100644 --- a/libmount/src/fs.c +++ b/libmount/src/fs.c @@ -139,6 +139,7 @@ struct libmnt_fs *mnt_copy_fs(struct libmnt_fs *dest, dest->id = src->id; dest->parent = src->parent; dest->devno = src->devno; + dest->tid = src->tid; if (cpy_str_at_offset(dest, src, offsetof(struct libmnt_fs, source))) goto err; @@ -1157,6 +1158,18 @@ dev_t mnt_fs_get_devno(struct libmnt_fs *fs) return fs ? fs->devno : 0; } +/** + * mnt_fs_get_tid: + * @fs: /proc//mountinfo entry + * + * Returns: TID (task ID) for filesystems read from mountinfo file + */ +pid_t mnt_fs_get_tid(struct libmnt_fs *fs) +{ + assert(fs); + return fs ? fs->tid : 0; +} + /** * mnt_fs_get_option: * @fs: fstab/mtab/mountinfo entry pointer @@ -1406,7 +1419,10 @@ int mnt_fs_print_debug(struct libmnt_fs *fs, FILE *file) fprintf(file, "parent: %d\n", mnt_fs_get_parent_id(fs)); if (mnt_fs_get_devno(fs)) fprintf(file, "devno: %d:%d\n", major(mnt_fs_get_devno(fs)), - minor(mnt_fs_get_devno(fs))); + minor(mnt_fs_get_devno(fs))); + if (mnt_fs_get_tid(fs)) + fprintf(file, "tid: %d\n", mnt_fs_get_tid(fs)); + return 0; } diff --git a/libmount/src/libmount.h.in b/libmount/src/libmount.h.in index e122439620..6b87f984c3 100644 --- a/libmount/src/libmount.h.in +++ b/libmount/src/libmount.h.in @@ -274,6 +274,7 @@ extern int mnt_fs_set_bindsrc(struct libmnt_fs *fs, const char *src); extern int mnt_fs_get_id(struct libmnt_fs *fs); extern int mnt_fs_get_parent_id(struct libmnt_fs *fs); extern dev_t mnt_fs_get_devno(struct libmnt_fs *fs); +extern pid_t mnt_fs_get_tid(struct libmnt_fs *fs); extern const char *mnt_fs_get_swaptype(struct libmnt_fs *fs); extern off_t mnt_fs_get_size(struct libmnt_fs *fs); diff --git a/libmount/src/libmount.sym b/libmount/src/libmount.sym index d4d5b84bf4..d07b570f14 100644 --- a/libmount/src/libmount.sym +++ b/libmount/src/libmount.sym @@ -239,8 +239,9 @@ global: mnt_table_parse_swaps; mnt_get_mountpoint; mnt_get_swaps_path; - mnt_fs_get_swaptype; + mnt_fs_get_priority; mnt_fs_get_size; + mnt_fs_get_swaptype; + mnt_fs_get_tid; mnt_fs_get_usedsize; - mnt_fs_get_priority; } MOUNT_2.21; diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h index f7cd7d53dd..a2922c2b3e 100644 --- a/libmount/src/mountP.h +++ b/libmount/src/mountP.h @@ -218,6 +218,7 @@ struct libmnt_fs { int priority; /* swaps[5]: swap priority */ int flags; /* MNT_FS_* flags */ + pid_t tid; /* /proc//mountinfo otherwise zero */ void *userdata; /* library independent data */ }; diff --git a/libmount/src/tab_parse.c b/libmount/src/tab_parse.c index 73a7f83c8b..ab5d51acd4 100644 --- a/libmount/src/tab_parse.c +++ b/libmount/src/tab_parse.c @@ -423,6 +423,35 @@ err: return tb->errcb ? tb->errcb(tb, filename, *nlines) : 1; } +static pid_t path_to_tid(const char *filename) +{ + char *path = mnt_resolve_path(filename, NULL); + char *p, *end = NULL; + pid_t tid = 0; + + if (!path) + goto done; + p = strrchr(path, '/'); + if (!p) + goto done; + *p = '\0'; + p = strrchr(path, '/'); + if (!p) + goto done; + p++; + + errno = 0; + tid = strtol(p, &end, 10); + if (errno || p == end || (end && *end)) { + tid = 0; + goto done; + } + DBG(TAB, mnt_debug("TID for %s is %d", filename, tid)); +done: + free(path); + return tid; +} + /** * mnt_table_parse_stream: * @tb: tab pointer @@ -436,6 +465,7 @@ int mnt_table_parse_stream(struct libmnt_table *tb, FILE *f, const char *filenam int nlines = 0; int rc = -1; int flags = 0; + pid_t tid = -1; assert(tb); assert(f); @@ -460,6 +490,11 @@ int mnt_table_parse_stream(struct libmnt_table *tb, FILE *f, const char *filenam if (!rc) { rc = mnt_table_add_fs(tb, fs); fs->flags |= flags; + if (tb->fmt == MNT_FMT_MOUNTINFO && filename) { + if (tid == -1) + tid = path_to_tid(filename); + fs->tid = tid; + } } if (rc) { mnt_free_fs(fs); -- 2.47.3