]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lslocks: detect blocked locks, fix /proc/locks parser
authorKarel Zak <kzak@redhat.com>
Thu, 14 Feb 2013 15:29:51 +0000 (16:29 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 14 Feb 2013 15:29:51 +0000 (16:29 +0100)
$ flock foo -c "sleep 100" & flock foo -c "sleep 100"

old version:
  $ lslocks
  lslocks: failed to parse pid: 'WRITE'

new version:

COMMAND           PID  TYPE  SIZE MODE   M      START        END PATH
[...]
flock            1318 FLOCK    0B WRITE* 0          0          0 /home/projects/
flock            1319 FLOCK    0B WRITE  0          0          0 /home/projects/

 The asterisk (e.g. WRITE*) is used for blocked processes.

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

index 24cda144e44494e484fda7b231d9bcbbd64df989..518ff0fbaae381137730d4ca66017cbccf8a3cca 100644 (file)
@@ -44,8 +44,8 @@ Type of lock, can be FLOCK (created with flock(2)) or POSIX (created with fcntl(
 Size of the locked file.
 
 .IP "MODE"
-Lock access permissions (read, write).
-
+Lock access permissions (read, write). If the process is blocked and waiting for the lock
+than the '*' (asterisk) postfix is used for the mode.
 .IP "M"
 Mandatory state of the lock: 0 if none; 1 if set.  (See chmod(1)).
 
index e837b014e432819452462fa300c8fb7c8ecc3556..fb40dcb73d800a84685bfed18c3a1749036ede73 100644 (file)
@@ -93,7 +93,8 @@ struct lock {
        char *mode;
        off_t start;
        off_t end;
-       int mandatory;
+       unsigned int mandatory :1,
+                    blocked   :1;
        char *size;
 };
 
@@ -250,11 +251,15 @@ static int get_local_locks(struct list_head *locks)
                        case 0: /* ignore */
                                break;
                        case 1: /* posix, flock, etc */
-                               l->type = xstrdup(tok);
+                               if (strcmp(tok, "->") == 0) {   /* optional field */
+                                       l->blocked = 1;
+                                       i--;
+                               } else
+                                       l->type = xstrdup(tok);
                                break;
 
                        case 2: /* is this a mandatory lock? other values are advisory or noinode */
-                               l->mandatory = *tok == 'M' ? TRUE : FALSE;
+                               l->mandatory = *tok == 'M' ? 1 : 0;
                                break;
                        case 3: /* lock mode */
                                l->mode = xstrdup(tok);
@@ -406,10 +411,10 @@ static void add_tt_line(struct tt *tt, struct lock *l)
                        xasprintf(&str, "%s", l->size);
                        break;
                case COL_MODE:
-                       xasprintf(&str, "%s", l->mode);
+                       xasprintf(&str, "%s%s", l->mode, l->blocked ? "*" : "");
                        break;
                case COL_M:
-                       xasprintf(&str, "%d", l->mandatory);
+                       xasprintf(&str, "%d", l->mandatory ? 1 : 0);
                        break;
                case COL_START:
                        xasprintf(&str, "%jd", l->start);