2 * lslocks(8) - list local system locks
4 * Copyright (C) 2012 Davidlohr Bueso <dave@gnu.org>
6 * Very generally based on lslk(8) by Victor A. Abell <abe@purdue.edu>
7 * Since it stopped being maingained over a decade ago, this
8 * program should be considered its replacement.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it would be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <sys/types.h>
36 #include <libsmartcols.h>
38 #include "pathnames.h"
39 #include "canonicalize.h"
45 #include "closestream.h"
47 #include "procutils.h"
65 const char *name
; /* header */
66 double whint
; /* width hint (N < 1 is in percent of termwidth) */
67 int flags
; /* SCOLS_FL_* */
71 /* columns descriptions */
72 static struct colinfo infos
[] = {
73 [COL_SRC
] = { "COMMAND",15, 0, N_("command of the process holding the lock") },
74 [COL_PID
] = { "PID", 5, SCOLS_FL_RIGHT
, N_("PID of the process holding the lock") },
75 [COL_TYPE
] = { "TYPE", 5, SCOLS_FL_RIGHT
, N_("kind of lock: FL_FLOCK or FL_POSIX.") },
76 [COL_SIZE
] = { "SIZE", 4, SCOLS_FL_RIGHT
, N_("size of the lock") },
77 [COL_MODE
] = { "MODE", 5, 0, N_("lock access mode") },
78 [COL_M
] = { "M", 1, 0, N_("mandatory state of the lock: 0 (none), 1 (set)")},
79 [COL_START
] = { "START", 10, SCOLS_FL_RIGHT
, N_("relative byte offset of the lock")},
80 [COL_END
] = { "END", 10, SCOLS_FL_RIGHT
, N_("ending offset of the lock")},
81 [COL_PATH
] = { "PATH", 0, SCOLS_FL_TRUNC
, N_("path of the locked file")},
82 [COL_BLOCKER
] = { "BLOCKER", 0, SCOLS_FL_RIGHT
, N_("PID of the process blocking the lock") }
85 static int columns
[ARRAY_SIZE(infos
) * 2];
86 static size_t ncolumns
;
90 static struct libmnt_table
*tab
; /* /proc/self/mountinfo */
92 /* basic output flags */
93 static int no_headings
;
98 struct list_head locks
;
107 unsigned int mandatory
:1,
113 static void disable_columns_truncate(void)
117 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
118 infos
[i
].flags
&= ~SCOLS_FL_TRUNC
;
122 * Associate the device's mountpoint for a filename
124 static char *get_fallback_filename(dev_t dev
)
126 struct libmnt_fs
*fs
;
129 tab
= mnt_new_table_from_file(_PATH_PROC_MOUNTINFO
);
134 fs
= mnt_table_find_devno(tab
, dev
, MNT_ITER_BACKWARD
);
138 return xstrdup(mnt_fs_get_target(fs
));
142 * Return the absolute path of a file from
143 * a given inode number (and its size)
145 static char *get_filename_sz(ino_t inode
, pid_t lock_pid
, size_t *size
)
152 char path
[PATH_MAX
], sym
[PATH_MAX
], *ret
= NULL
;
155 memset(path
, 0, sizeof(path
));
156 memset(sym
, 0, sizeof(sym
));
159 * We know the pid so we don't have to
160 * iterate the *entire* filesystem searching
163 sprintf(path
, "/proc/%d/fd/", lock_pid
);
164 if (!(dirp
= opendir(path
)))
167 if ((len
= strlen(path
)) >= (sizeof(path
) - 2))
170 if ((fd
= dirfd(dirp
)) < 0 )
173 while ((dp
= readdir(dirp
))) {
174 if (!strcmp(dp
->d_name
, ".") ||
175 !strcmp(dp
->d_name
, ".."))
178 /* care only for numerical descriptors */
179 if (!strtol(dp
->d_name
, (char **) NULL
, 10))
182 if (!fstatat(fd
, dp
->d_name
, &sb
, 0)
183 && inode
!= sb
.st_ino
)
186 if ((len
= readlinkat(fd
, dp
->d_name
, sym
, sizeof(sym
) - 1)) < 1)
201 * Return the inode number from a string
203 static ino_t
get_dev_inode(char *str
, dev_t
*dev
)
205 unsigned int maj
= 0, min
= 0;
208 sscanf(str
, "%02x:%02x:%ju", &maj
, &min
, &inum
);
210 *dev
= (dev_t
) makedev(maj
, min
);
214 static int get_local_locks(struct list_head
*locks
)
219 char buf
[PATH_MAX
], *szstr
= NULL
, *tok
= NULL
;
224 if (!(fp
= fopen(_PATH_PROC_LOCKS
, "r")))
227 while (fgets(buf
, sizeof(buf
), fp
)) {
229 l
= xcalloc(1, sizeof(*l
));
230 INIT_LIST_HEAD(&l
->locks
);
232 for (tok
= strtok(buf
, " "), i
= 0; tok
;
233 tok
= strtok(NULL
, " "), i
++) {
236 * /proc/locks has *exactly* 8 "blocks" of text
237 * separated by ' ' - check <kernel>/fs/locks.c
241 tok
[strlen(tok
) - 1] = '\0';
242 l
->id
= strtos32_or_err(tok
, _("failed to parse ID"));
244 case 1: /* posix, flock, etc */
245 if (strcmp(tok
, "->") == 0) { /* optional field */
249 l
->type
= xstrdup(tok
);
252 case 2: /* is this a mandatory lock? other values are advisory or noinode */
253 l
->mandatory
= *tok
== 'M' ? 1 : 0;
255 case 3: /* lock mode */
256 l
->mode
= xstrdup(tok
);
261 * If user passed a pid we filter it later when adding
262 * to the list, no need to worry now.
264 l
->pid
= strtos32_or_err(tok
, _("failed to parse pid"));
265 l
->cmdname
= proc_get_command_name(l
->pid
);
267 l
->cmdname
= xstrdup(_("(unknown)"));
270 case 5: /* device major:minor and inode number */
271 inode
= get_dev_inode(tok
, &dev
);
275 l
->start
= !strcmp(tok
, "EOF") ? 0 :
276 strtou64_or_err(tok
, _("failed to parse start"));
280 /* replace '\n' character */
281 tok
[strlen(tok
)-1] = '\0';
282 l
->end
= !strcmp(tok
, "EOF") ? 0 :
283 strtou64_or_err(tok
, _("failed to parse end"));
289 l
->path
= get_filename_sz(inode
, l
->pid
, &sz
);
291 /* probably no permission to peek into l->pid's path */
292 l
->path
= get_fallback_filename(dev
);
295 szstr
= size_to_human_string(SIZE_SUFFIX_1LETTER
, sz
);
296 l
->size
= xstrdup(szstr
);
300 list_add(&l
->locks
, locks
);
307 static int column_name_to_id(const char *name
, size_t namesz
)
313 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
314 const char *cn
= infos
[i
].name
;
316 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
319 warnx(_("unknown column: %s"), name
);
323 static inline int get_column_id(int num
)
326 assert((size_t) num
< ncolumns
);
327 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
333 static inline struct colinfo
*get_column_info(unsigned num
)
335 return &infos
[ get_column_id(num
) ];
338 static void rem_lock(struct lock
*lock
)
348 list_del(&lock
->locks
);
352 static pid_t
get_blocker(int id
, struct list_head
*locks
)
356 list_for_each(p
, locks
) {
357 struct lock
*l
= list_entry(p
, struct lock
, locks
);
359 if (l
->id
== id
&& !l
->blocked
)
366 static void add_scols_line(struct libscols_table
*table
, struct lock
*l
, struct list_head
*locks
)
369 struct libscols_line
*line
;
371 * Whenever cmdname or filename is NULL it is most
372 * likely because there's no read permissions
373 * for the specified process.
375 const char *notfnd
= "";
380 line
= scols_table_new_line(table
, NULL
);
382 warn(_("failed to add line to output"));
386 for (i
= 0; i
< ncolumns
; i
++) {
389 switch (get_column_id(i
)) {
391 xasprintf(&str
, "%s", l
->cmdname
? l
->cmdname
: notfnd
);
394 xasprintf(&str
, "%d", l
->pid
);
397 xasprintf(&str
, "%s", l
->type
);
400 xasprintf(&str
, "%s", l
->size
);
403 xasprintf(&str
, "%s%s", l
->mode
, l
->blocked
? "*" : "");
406 xasprintf(&str
, "%d", l
->mandatory
? 1 : 0);
409 xasprintf(&str
, "%jd", l
->start
);
412 xasprintf(&str
, "%jd", l
->end
);
415 xasprintf(&str
, "%s", l
->path
? l
->path
: notfnd
);
419 pid_t bl
= l
->blocked
&& l
->id
?
420 get_blocker(l
->id
, locks
) : 0;
422 xasprintf(&str
, "%d", (int) bl
);
429 scols_line_set_data(line
, i
, str
);
433 static int show_locks(struct list_head
*locks
)
437 struct list_head
*p
, *pnext
;
438 struct libscols_table
*table
;
440 table
= scols_new_table();
442 warn(_("failed to initialize output table"));
445 scols_table_enable_raw(table
, raw
);
446 scols_table_enable_json(table
, json
);
447 scols_table_enable_noheadings(table
, no_headings
);
450 scols_table_set_name(table
, "locks");
452 for (i
= 0; i
< ncolumns
; i
++) {
453 struct colinfo
*col
= get_column_info(i
);
455 if (!scols_table_new_column(table
, col
->name
, col
->whint
, col
->flags
)) {
456 warnx(_("failed to initialize output column"));
462 /* prepare data for output */
463 list_for_each(p
, locks
) {
464 struct lock
*l
= list_entry(p
, struct lock
, locks
);
466 if (pid
&& pid
!= l
->pid
)
469 add_scols_line(table
, l
, locks
);
472 /* destroy the list */
473 list_for_each_safe(p
, pnext
, locks
) {
474 struct lock
*l
= list_entry(p
, struct lock
, locks
);
478 scols_print_table(table
);
480 scols_unref_table(table
);
485 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
489 fputs(USAGE_HEADER
, out
);
492 _(" %s [options]\n"), program_invocation_short_name
);
494 fputs(USAGE_SEPARATOR
, out
);
495 fputs(_("List local system locks.\n"), out
);
497 fputs(USAGE_OPTIONS
, out
);
498 fputs(_(" -J, --json use JSON output format\n"), out
);
499 fputs(_(" -n, --noheadings don't print headings\n"), out
);
500 fputs(_(" -o, --output <list> define which output columns to use\n"), out
);
501 fputs(_(" -p, --pid <pid> display only locks held by this process\n"), out
);
502 fputs(_(" -r, --raw use the raw output format\n"), out
);
503 fputs(_(" -u, --notruncate don't truncate text in columns\n"), out
);
505 fputs(USAGE_SEPARATOR
, out
);
506 fputs(USAGE_HELP
, out
);
507 fputs(USAGE_VERSION
, out
);
509 fputs(_("\nAvailable columns (for --output):\n"), out
);
511 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
512 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
514 fprintf(out
, USAGE_MAN_TAIL("lslocks(8)"));
516 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
519 int main(int argc
, char *argv
[])
522 struct list_head locks
;
524 static const struct option long_opts
[] = {
525 { "json", no_argument
, NULL
, 'J' },
526 { "pid", required_argument
, NULL
, 'p' },
527 { "help", no_argument
, NULL
, 'h' },
528 { "output", required_argument
, NULL
, 'o' },
529 { "notruncate", no_argument
, NULL
, 'u' },
530 { "version", no_argument
, NULL
, 'V' },
531 { "noheadings", no_argument
, NULL
, 'n' },
532 { "raw", no_argument
, NULL
, 'r' },
536 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
540 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
541 setlocale(LC_ALL
, "");
542 bindtextdomain(PACKAGE
, LOCALEDIR
);
544 atexit(close_stdout
);
546 while ((c
= getopt_long(argc
, argv
,
547 "Jp:o:nruhV", long_opts
, NULL
)) != -1) {
549 err_exclusive_options(c
, long_opts
, excl
, excl_st
);
556 pid
= strtos32_or_err(optarg
, _("invalid PID argument"));
562 printf(UTIL_LINUX_VERSION
);
573 disable_columns_truncate();
581 INIT_LIST_HEAD(&locks
);
584 /* default columns */
585 columns
[ncolumns
++] = COL_SRC
;
586 columns
[ncolumns
++] = COL_PID
;
587 columns
[ncolumns
++] = COL_TYPE
;
588 columns
[ncolumns
++] = COL_SIZE
;
589 columns
[ncolumns
++] = COL_MODE
;
590 columns
[ncolumns
++] = COL_M
;
591 columns
[ncolumns
++] = COL_START
;
592 columns
[ncolumns
++] = COL_END
;
593 columns
[ncolumns
++] = COL_PATH
;
596 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
597 &ncolumns
, column_name_to_id
) < 0)
602 rc
= get_local_locks(&locks
);
604 if (!rc
&& !list_empty(&locks
))
605 rc
= show_locks(&locks
);
607 mnt_unref_table(tab
);