From: Karel Zak Date: Thu, 14 Feb 2013 15:29:51 +0000 (+0100) Subject: lslocks: detect blocked locks, fix /proc/locks parser X-Git-Tag: v2.23-rc1~213 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=55c0d16bab8cc84b72bf11cb2fdd8aa6205ac608;p=thirdparty%2Futil-linux.git lslocks: detect blocked locks, fix /proc/locks parser $ 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 Signed-off-by: Karel Zak --- diff --git a/misc-utils/lslocks.8 b/misc-utils/lslocks.8 index 24cda144e4..518ff0fbaa 100644 --- a/misc-utils/lslocks.8 +++ b/misc-utils/lslocks.8 @@ -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)). diff --git a/misc-utils/lslocks.c b/misc-utils/lslocks.c index e837b014e4..fb40dcb73d 100644 --- a/misc-utils/lslocks.c +++ b/misc-utils/lslocks.c @@ -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);