]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lslocks: add --noinaccessible
authorKarel Zak <kzak@redhat.com>
Fri, 1 Jul 2016 12:45:04 +0000 (14:45 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 1 Jul 2016 12:45:04 +0000 (14:45 +0200)
* add --noinaccessible to not print incomplete information

* make incomplete paths more visible (append "...")

* don't print 0B size if lock file inaccessible

Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/lslocks.8
misc-utils/lslocks.c

index 7d983c839d7ccddb5b9707d32b2cd983f1099e32..2be0a28b9a4773b991ce6e947539632eb94a067c 100644 (file)
@@ -15,6 +15,9 @@ lists information about all the currently held file locks in a Linux system.
 
 .SH OPTIONS
 .TP
+.BR \-i , " \-\-noinaccessible"
+Ignore lock files which are inaccessible for the current user.
+.TP
 .BR \-J , " \-\-json"
 Use JSON output format.
 .TP
@@ -65,8 +68,9 @@ Relative byte offset of the lock.
 .IP "END"
 Ending offset of the lock.
 .IP "PATH"
-Full path of the lock.  If none is found, or there are no permissions to read the path,
-it will fall back to the device's mountpoint.  The path might be truncated; use
+Full path of the lock.  If none is found, or there are no permissions to read
+the path, it will fall back to the device's mountpoint and "..." is appended to
+the path.  The path might be truncated; use
 \fB\-\-notruncate\fR to get the full path.
 .IP "BLOCKER"
 The PID of the process which blocks the lock.
index 1f33dfde9dbd048914e27804feff8e299c44ed82..684de082bfea5c4b0d46e4ceccd8fb5d78680ac5 100644 (file)
@@ -91,6 +91,7 @@ static struct libmnt_table *tab;              /* /proc/self/mountinfo */
 
 /* basic output flags */
 static int no_headings;
+static int no_inaccessible;
 static int raw;
 static int json;
 
@@ -110,6 +111,20 @@ struct lock {
        int id;
 };
 
+static void rem_lock(struct lock *lock)
+{
+       if (!lock)
+               return;
+
+       free(lock->path);
+       free(lock->size);
+       free(lock->mode);
+       free(lock->cmdname);
+       free(lock->type);
+       list_del(&lock->locks);
+       free(lock);
+}
+
 static void disable_columns_truncate(void)
 {
        size_t i;
@@ -124,6 +139,7 @@ static void disable_columns_truncate(void)
 static char *get_fallback_filename(dev_t dev)
 {
        struct libmnt_fs *fs;
+       char *res = NULL;
 
        if (!tab) {
                tab = mnt_new_table_from_file(_PATH_PROC_MOUNTINFO);
@@ -135,7 +151,8 @@ static char *get_fallback_filename(dev_t dev)
        if (!fs)
                return NULL;
 
-       return xstrdup(mnt_fs_get_target(fs));
+       xasprintf(&res, "%s...", mnt_fs_get_target(fs));
+       return res;
 }
 
 /*
@@ -216,7 +233,7 @@ static int get_local_locks(struct list_head *locks)
        int i;
        ino_t inode = 0;
        FILE *fp;
-       char buf[PATH_MAX], *szstr = NULL, *tok = NULL;
+       char buf[PATH_MAX], *tok = NULL;
        size_t sz;
        struct lock *l;
        dev_t dev = 0;
@@ -285,18 +302,24 @@ static int get_local_locks(struct list_head *locks)
                        default:
                                break;
                        }
+               }
 
-                       l->path = get_filename_sz(inode, l->pid, &sz);
-                       if (!l->path)
-                               /* probably no permission to peek into l->pid's path */
-                               l->path = get_fallback_filename(dev);
+               l->path = get_filename_sz(inode, l->pid, &sz);
 
-                       /* avoid leaking */
-                       szstr = size_to_human_string(SIZE_SUFFIX_1LETTER, sz);
-                       l->size = xstrdup(szstr);
-                       free(szstr);
+               /* no permissions -- ignore */
+               if (!l->path && no_inaccessible) {
+                       rem_lock(l);
+                       continue;
                }
 
+               if (!l->path) {
+                       /* probably no permission to peek into l->pid's path */
+                       l->path = get_fallback_filename(dev);
+                       l->size = xstrdup("");
+               } else
+                       /* avoid leaking */
+                       l->size = size_to_human_string(SIZE_SUFFIX_1LETTER, sz);
+
                list_add(&l->locks, locks);
        }
 
@@ -335,20 +358,6 @@ static inline struct colinfo *get_column_info(unsigned num)
        return &infos[ get_column_id(num) ];
 }
 
-static void rem_lock(struct lock *lock)
-{
-       if (!lock)
-               return;
-
-       free(lock->path);
-       free(lock->size);
-       free(lock->mode);
-       free(lock->cmdname);
-       free(lock->type);
-       list_del(&lock->locks);
-       free(lock);
-}
-
 static pid_t get_blocker(int id, struct list_head *locks)
 {
        struct list_head *p;
@@ -496,6 +505,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
 
        fputs(USAGE_OPTIONS, out);
        fputs(_(" -J, --json             use JSON output format\n"), out);
+       fputs(_(" -i, --noinaccessible   ignore locks without read permissions\n"), out);
        fputs(_(" -n, --noheadings       don't print headings\n"), out);
        fputs(_(" -o, --output <list>    define which output columns to use\n"), out);
        fputs(_(" -p, --pid <pid>        display only locks held by this process\n"), out);
@@ -530,6 +540,7 @@ int main(int argc, char *argv[])
                { "version",    no_argument,       NULL, 'V' },
                { "noheadings", no_argument,       NULL, 'n' },
                { "raw",        no_argument,       NULL, 'r' },
+               { "noinaccessible", no_argument, NULL, 'i' },
                { NULL, 0, NULL, 0 }
        };
 
@@ -544,11 +555,14 @@ int main(int argc, char *argv[])
        atexit(close_stdout);
 
        while ((c = getopt_long(argc, argv,
-                               "Jp:o:nruhV", long_opts, NULL)) != -1) {
+                               "iJp:o:nruhV", long_opts, NULL)) != -1) {
 
                err_exclusive_options(c, long_opts, excl, excl_st);
 
                switch(c) {
+               case 'i':
+                       no_inaccessible = 1;
+                       break;
                case 'J':
                        json = 1;
                        break;