summaryrefslogtreecommitdiffstats
path: root/misc-utils/lslocks.c
diff options
context:
space:
mode:
authorKarel Zak2016-07-01 14:45:04 +0200
committerKarel Zak2016-07-01 14:45:04 +0200
commitf29bc6e1cc4ed9f76bded543c6ab393f674ec3ed (patch)
treeddbc53b7e39234416fb671bb9c4775d7ac1db812 /misc-utils/lslocks.c
parentMerge branch 'utmpdump' of git://github.com/kerolasa/lelux-utiliteetit (diff)
downloadkernel-qcow2-util-linux-f29bc6e1cc4ed9f76bded543c6ab393f674ec3ed.tar.gz
kernel-qcow2-util-linux-f29bc6e1cc4ed9f76bded543c6ab393f674ec3ed.tar.xz
kernel-qcow2-util-linux-f29bc6e1cc4ed9f76bded543c6ab393f674ec3ed.zip
lslocks: add --noinaccessible
* 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>
Diffstat (limited to 'misc-utils/lslocks.c')
-rw-r--r--misc-utils/lslocks.c64
1 files changed, 39 insertions, 25 deletions
diff --git a/misc-utils/lslocks.c b/misc-utils/lslocks.c
index 1f33dfde9..684de082b 100644
--- a/misc-utils/lslocks.c
+++ b/misc-utils/lslocks.c
@@ -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;