]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * lslocks(8) - list local system locks | |
3 | * | |
4 | * Copyright (C) 2012 Davidlohr Bueso <dave@gnu.org> | |
5 | * | |
6 | * Very generally based on lslk(8) by Victor A. Abell <abe@purdue.edu> | |
7 | * Since it stopped being maintained over a decade ago, this | |
8 | * program should be considered its replacement. | |
9 | * | |
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. | |
14 | * | |
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. | |
19 | * | |
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 | |
23 | */ | |
24 | ||
25 | #include <stdio.h> | |
26 | #include <string.h> | |
27 | #include <getopt.h> | |
28 | #include <stdlib.h> | |
29 | #include <assert.h> | |
30 | #include <dirent.h> | |
31 | #include <unistd.h> | |
32 | #include <sys/stat.h> | |
33 | #include <sys/types.h> | |
34 | #include <stdbool.h> | |
35 | #include <search.h> | |
36 | ||
37 | #include <libmount.h> | |
38 | #include <libsmartcols.h> | |
39 | ||
40 | #include "pathnames.h" | |
41 | #include "canonicalize.h" | |
42 | #include "nls.h" | |
43 | #include "xalloc.h" | |
44 | #include "strutils.h" | |
45 | #include "c.h" | |
46 | #include "cctype.h" | |
47 | #include "list.h" | |
48 | #include "closestream.h" | |
49 | #include "optutils.h" | |
50 | #include "procfs.h" | |
51 | #include "column-list-table.h" | |
52 | #include "fileutils.h" | |
53 | ||
54 | /* column IDs */ | |
55 | enum { | |
56 | COL_SRC = 0, | |
57 | COL_PID, | |
58 | COL_TYPE, | |
59 | COL_SIZE, | |
60 | COL_INODE, | |
61 | COL_MAJMIN, | |
62 | COL_MODE, | |
63 | COL_M, | |
64 | COL_START, | |
65 | COL_END, | |
66 | COL_PATH, | |
67 | COL_BLOCKER, | |
68 | COL_HOLDERS, | |
69 | }; | |
70 | ||
71 | /* column names */ | |
72 | struct colinfo { | |
73 | const char * const name; /* header */ | |
74 | double whint; /* width hint (N < 1 is in percent of termwidth) */ | |
75 | int flags; /* SCOLS_FL_* */ | |
76 | const char *help; | |
77 | }; | |
78 | ||
79 | /* columns descriptions */ | |
80 | static struct colinfo infos[] = { | |
81 | [COL_SRC] = { "COMMAND",15, 0, N_("command of the process holding the lock") }, | |
82 | [COL_PID] = { "PID", 5, SCOLS_FL_RIGHT, N_("PID of the process holding the lock") }, | |
83 | [COL_TYPE] = { "TYPE", 5, SCOLS_FL_RIGHT, N_("kind of lock") }, | |
84 | [COL_SIZE] = { "SIZE", 4, SCOLS_FL_RIGHT, N_("size of the lock, use <number> if --bytes is given") }, | |
85 | [COL_INODE] = { "INODE", 5, SCOLS_FL_RIGHT, N_("inode number") }, | |
86 | [COL_MAJMIN] = { "MAJ:MIN", 6, 0, N_("major:minor device number") }, | |
87 | [COL_MODE] = { "MODE", 5, 0, N_("lock access mode") }, | |
88 | [COL_M] = { "M", 1, 0, N_("mandatory state of the lock: 0 (none), 1 (set)")}, | |
89 | [COL_START] = { "START", 10, SCOLS_FL_RIGHT, N_("relative byte offset of the lock")}, | |
90 | [COL_END] = { "END", 10, SCOLS_FL_RIGHT, N_("ending offset of the lock")}, | |
91 | [COL_PATH] = { "PATH", 0, SCOLS_FL_TRUNC, N_("path of the locked file")}, | |
92 | [COL_BLOCKER] = { "BLOCKER", 0, SCOLS_FL_RIGHT, N_("PID of the process blocking the lock") }, | |
93 | [COL_HOLDERS] = { "HOLDERS", 0, SCOLS_FL_WRAP, N_("holders of the lock") }, | |
94 | }; | |
95 | ||
96 | static int columns[ARRAY_SIZE(infos) * 2]; | |
97 | static size_t ncolumns; | |
98 | ||
99 | static struct libmnt_table *tab; /* /proc/self/mountinfo */ | |
100 | ||
101 | /* basic output flags */ | |
102 | static int no_headings; | |
103 | static int no_inaccessible; | |
104 | static int raw; | |
105 | static int json; | |
106 | static int bytes; | |
107 | ||
108 | struct lock { | |
109 | struct list_head locks; | |
110 | ||
111 | char *cmdname; | |
112 | pid_t pid; | |
113 | char *path; | |
114 | char *type; | |
115 | char *mode; | |
116 | off_t start; | |
117 | off_t end; | |
118 | ino_t inode; | |
119 | dev_t dev; | |
120 | bool mandatory, | |
121 | blocked; | |
122 | uint64_t size; | |
123 | int fd; | |
124 | int id; | |
125 | }; | |
126 | ||
127 | struct lock_tnode { | |
128 | dev_t dev; | |
129 | ino_t inode; | |
130 | ||
131 | struct list_head chain; | |
132 | }; | |
133 | ||
134 | static int lock_tnode_compare(const void *a, const void *b) | |
135 | { | |
136 | struct lock_tnode *anode = ((struct lock_tnode *)a); | |
137 | struct lock_tnode *bnode = ((struct lock_tnode *)b); | |
138 | ||
139 | if (anode->dev > bnode->dev) | |
140 | return 1; | |
141 | else if (anode->dev < bnode->dev) | |
142 | return -1; | |
143 | ||
144 | if (anode->inode > bnode->inode) | |
145 | return 1; | |
146 | else if (anode->inode < bnode->inode) | |
147 | return -1; | |
148 | ||
149 | return 0; | |
150 | } | |
151 | ||
152 | static void add_to_tree(void *troot, struct lock *l) | |
153 | { | |
154 | struct lock_tnode tmp = { .dev = l->dev, .inode = l->inode, }; | |
155 | struct lock_tnode **head = tfind(&tmp, troot, lock_tnode_compare); | |
156 | struct lock_tnode *new_head; | |
157 | ||
158 | if (head) { | |
159 | list_add_tail(&l->locks, &(*head)->chain); | |
160 | return; | |
161 | } | |
162 | ||
163 | new_head = xmalloc(sizeof(*new_head)); | |
164 | new_head->dev = l->dev; | |
165 | new_head->inode = l->inode; | |
166 | INIT_LIST_HEAD(&new_head->chain); | |
167 | if (tsearch(new_head, troot, lock_tnode_compare) == NULL) | |
168 | errx(EXIT_FAILURE, _("failed to allocate memory")); | |
169 | ||
170 | list_add_tail(&l->locks, &new_head->chain); | |
171 | } | |
172 | ||
173 | static void rem_lock(struct lock *lock) | |
174 | { | |
175 | if (!lock) | |
176 | return; | |
177 | ||
178 | free(lock->path); | |
179 | free(lock->mode); | |
180 | free(lock->cmdname); | |
181 | free(lock->type); | |
182 | list_del(&lock->locks); | |
183 | free(lock); | |
184 | } | |
185 | ||
186 | static void disable_columns_truncate(void) | |
187 | { | |
188 | size_t i; | |
189 | ||
190 | for (i = 0; i < ARRAY_SIZE(infos); i++) | |
191 | infos[i].flags &= ~SCOLS_FL_TRUNC; | |
192 | } | |
193 | ||
194 | /* | |
195 | * Associate the device's mountpoint for a filename | |
196 | */ | |
197 | static char *get_fallback_filename(dev_t dev) | |
198 | { | |
199 | struct libmnt_fs *fs; | |
200 | char *res = NULL; | |
201 | ||
202 | if (!tab) { | |
203 | tab = mnt_new_table_from_file(_PATH_PROC_MOUNTINFO); | |
204 | if (!tab) | |
205 | return NULL; | |
206 | } | |
207 | ||
208 | fs = mnt_table_find_devno(tab, dev, MNT_ITER_BACKWARD); | |
209 | if (!fs) | |
210 | return NULL; | |
211 | ||
212 | xasprintf(&res, "%s...", mnt_fs_get_target(fs)); | |
213 | return res; | |
214 | } | |
215 | ||
216 | /* | |
217 | * Return the absolute path of a file from | |
218 | * a given inode number (and its size) | |
219 | */ | |
220 | static char *get_filename_sz(ino_t inode, pid_t lock_pid, size_t *size) | |
221 | { | |
222 | struct stat sb; | |
223 | struct dirent *dp; | |
224 | DIR *dirp; | |
225 | int fd; | |
226 | char path[PATH_MAX] = { 0 }, | |
227 | sym[PATH_MAX] = { 0 }, *ret = NULL; | |
228 | ||
229 | *size = 0; | |
230 | ||
231 | if (lock_pid < 0) | |
232 | /* pid could be -1 for OFD locks */ | |
233 | return NULL; | |
234 | ||
235 | /* | |
236 | * We know the pid so we don't have to | |
237 | * iterate the *entire* filesystem searching | |
238 | * for the damn file. | |
239 | */ | |
240 | snprintf(path, sizeof(path), "/proc/%d/fd/", lock_pid); | |
241 | if (!(dirp = opendir(path))) | |
242 | return NULL; | |
243 | ||
244 | if (strlen(path) >= (sizeof(path) - 2)) | |
245 | goto out; | |
246 | ||
247 | if ((fd = dirfd(dirp)) < 0 ) | |
248 | goto out; | |
249 | ||
250 | while ((dp = xreaddir(dirp))) { | |
251 | ssize_t len; | |
252 | ||
253 | errno = 0; | |
254 | ||
255 | /* care only for numerical descriptors */ | |
256 | if (!strtol(dp->d_name, (char **) NULL, 10) || errno) | |
257 | continue; | |
258 | ||
259 | if (!fstatat(fd, dp->d_name, &sb, 0) | |
260 | && inode != sb.st_ino) | |
261 | continue; | |
262 | ||
263 | if ((len = readlinkat(fd, dp->d_name, sym, sizeof(sym) - 1)) < 1) | |
264 | goto out; | |
265 | ||
266 | *size = sb.st_size; | |
267 | sym[len] = '\0'; | |
268 | ||
269 | ret = xstrdup(sym); | |
270 | break; | |
271 | } | |
272 | out: | |
273 | closedir(dirp); | |
274 | return ret; | |
275 | } | |
276 | ||
277 | /* | |
278 | * Return the inode number from a string | |
279 | */ | |
280 | static ino_t get_dev_inode(char *str, dev_t *dev) | |
281 | { | |
282 | unsigned int maj = 0, min = 0; | |
283 | ino_t inum = 0; | |
284 | ||
285 | if (sscanf(str, "%x:%x:%ju", &maj, &min, &inum) != 3) | |
286 | errx(EXIT_FAILURE, _("failed to parse '%s'"), str); | |
287 | ||
288 | *dev = (dev_t) makedev(maj, min); | |
289 | return inum; | |
290 | } | |
291 | ||
292 | struct override_info { | |
293 | pid_t pid; | |
294 | const char *cmdname; | |
295 | }; | |
296 | ||
297 | static bool is_holder(struct lock *l, struct lock *m) | |
298 | { | |
299 | return (l->start == m->start && | |
300 | l->end == m->end && | |
301 | l->inode == m->inode && | |
302 | l->dev == m->dev && | |
303 | l->mandatory == m->mandatory && | |
304 | l->blocked == m->blocked && | |
305 | strcmp(l->type, m->type) == 0 && | |
306 | strcmp(l->mode, m->mode) == 0); | |
307 | } | |
308 | ||
309 | static void patch_lock(struct lock *l, void *fallback) | |
310 | { | |
311 | struct lock_tnode tmp = { .dev = l->dev, .inode = l->inode, }; | |
312 | struct lock_tnode **head = tfind(&tmp, fallback, lock_tnode_compare); | |
313 | struct list_head *p; | |
314 | ||
315 | if (!head) | |
316 | return; | |
317 | ||
318 | list_for_each(p, &(*head)->chain) { | |
319 | struct lock *m = list_entry(p, struct lock, locks); | |
320 | if (is_holder(l, m)) { | |
321 | /* size and id can be ignored. */ | |
322 | l->pid = m->pid; | |
323 | l->cmdname = xstrdup(m->cmdname); | |
324 | break; | |
325 | } | |
326 | } | |
327 | } | |
328 | ||
329 | static void add_to_list(void *locks, struct lock *l) | |
330 | { | |
331 | list_add(&l->locks, locks); | |
332 | } | |
333 | ||
334 | static struct lock *get_lock(char *buf, struct override_info *oinfo, void *fallback) | |
335 | { | |
336 | int i; | |
337 | char *tok = NULL; | |
338 | size_t sz; | |
339 | struct lock *l = xcalloc(1, sizeof(*l)); | |
340 | INIT_LIST_HEAD(&l->locks); | |
341 | l->fd = -1; | |
342 | ||
343 | bool cmdname_unknown = false; | |
344 | ||
345 | for (tok = strtok(buf, " "), i = 0; tok; | |
346 | tok = strtok(NULL, " "), i++) { | |
347 | ||
348 | /* | |
349 | * /proc/locks has *exactly* 8 "blocks" of text | |
350 | * separated by ' ' - check <kernel>/fs/locks.c | |
351 | */ | |
352 | switch (i) { | |
353 | case 0: /* ID: */ | |
354 | if (oinfo) | |
355 | l->id = -1; | |
356 | else { | |
357 | tok[strlen(tok) - 1] = '\0'; | |
358 | l->id = strtos32_or_err(tok, _("failed to parse ID")); | |
359 | } | |
360 | break; | |
361 | case 1: /* posix, flock, etc */ | |
362 | if (strcmp(tok, "->") == 0) { /* optional field */ | |
363 | l->blocked = 1; | |
364 | i--; | |
365 | } else | |
366 | l->type = xstrdup(tok); | |
367 | break; | |
368 | ||
369 | case 2: /* is this a mandatory lock? other values are advisory or noinode */ | |
370 | l->mandatory = *tok == 'M' ? 1 : 0; | |
371 | break; | |
372 | case 3: /* lock mode */ | |
373 | l->mode = xstrdup(tok); | |
374 | break; | |
375 | ||
376 | case 4: /* PID */ | |
377 | /* | |
378 | * If user passed a pid we filter it later when adding | |
379 | * to the list, no need to worry now. OFD locks use -1 PID. | |
380 | */ | |
381 | if (oinfo) { | |
382 | l->pid = oinfo->pid; | |
383 | l->cmdname = xstrdup(oinfo->cmdname); | |
384 | } else { | |
385 | /* strtopid_or_err() is not suitable here; tok can be -1.*/ | |
386 | l->pid = strtos32_or_err(tok, _("invalid PID argument")); | |
387 | if (l->pid > 0) { | |
388 | l->cmdname = pid_get_cmdname(l->pid); | |
389 | if (!l->cmdname) | |
390 | cmdname_unknown = true; | |
391 | } else | |
392 | l->cmdname = NULL; | |
393 | } | |
394 | break; | |
395 | ||
396 | case 5: /* device major:minor and inode number */ | |
397 | l->inode = get_dev_inode(tok, &l->dev); | |
398 | break; | |
399 | ||
400 | case 6: /* start */ | |
401 | l->start = !strcmp(tok, "EOF") ? 0 : | |
402 | strtou64_or_err(tok, _("failed to parse start")); | |
403 | break; | |
404 | ||
405 | case 7: /* end */ | |
406 | /* replace '\n' character */ | |
407 | tok[strlen(tok)-1] = '\0'; | |
408 | l->end = !strcmp(tok, "EOF") ? 0 : | |
409 | strtou64_or_err(tok, _("failed to parse end")); | |
410 | break; | |
411 | default: | |
412 | break; | |
413 | } | |
414 | } | |
415 | ||
416 | if ((!l->blocked) && fallback && !l->cmdname) | |
417 | patch_lock(l, fallback); | |
418 | if (!l->cmdname) { | |
419 | if (cmdname_unknown) | |
420 | l->cmdname = xstrdup(_("(unknown)")); | |
421 | else | |
422 | l->cmdname = xstrdup(_("(undefined)")); | |
423 | } | |
424 | l->path = get_filename_sz(l->inode, l->pid, &sz); | |
425 | ||
426 | /* no permissions -- ignore */ | |
427 | if (!l->path && no_inaccessible) { | |
428 | rem_lock(l); | |
429 | return NULL; | |
430 | } | |
431 | ||
432 | if (!l->path) { | |
433 | /* probably no permission to peek into l->pid's path */ | |
434 | l->path = get_fallback_filename(l->dev); | |
435 | l->size = 0; | |
436 | } else | |
437 | l->size = sz; | |
438 | ||
439 | return l; | |
440 | } | |
441 | ||
442 | static int get_pid_lock(void *locks, void (*add_lock)(void *, struct lock *), FILE *fp, | |
443 | pid_t pid, const char *cmdname, int fd) | |
444 | { | |
445 | char buf[PATH_MAX]; | |
446 | struct override_info oinfo = { | |
447 | .pid = pid, | |
448 | .cmdname = cmdname, | |
449 | }; | |
450 | ||
451 | while (fgets(buf, sizeof(buf), fp)) { | |
452 | struct lock *l; | |
453 | if (strncmp(buf, "lock:\t", 6)) | |
454 | continue; | |
455 | l = get_lock(buf + 6, &oinfo, NULL); | |
456 | if (l) { | |
457 | add_lock(locks, l); | |
458 | l->fd = fd; | |
459 | } | |
460 | /* no break here. | |
461 | Multiple recode locks can be taken via one fd. */ | |
462 | } | |
463 | ||
464 | return 0; | |
465 | } | |
466 | ||
467 | static int get_pid_locks(void *locks, void (*add_lock)(void *, struct lock *), struct path_cxt *pc, | |
468 | pid_t pid, const char *cmdname) | |
469 | { | |
470 | DIR *sub = NULL; | |
471 | struct dirent *d = NULL; | |
472 | int rc = 0; | |
473 | ||
474 | while (ul_path_next_dirent(pc, &sub, "fdinfo", &d) == 0) { | |
475 | uint64_t num; | |
476 | FILE *fdinfo; | |
477 | ||
478 | if (ul_strtou64(d->d_name, &num, 10) != 0) /* only numbers */ | |
479 | continue; | |
480 | ||
481 | fdinfo = ul_path_fopenf(pc, "r", "fdinfo/%ju", num); | |
482 | if (fdinfo == NULL) | |
483 | continue; | |
484 | ||
485 | get_pid_lock(locks, add_lock, fdinfo, pid, cmdname, (int)num); | |
486 | fclose(fdinfo); | |
487 | } | |
488 | ||
489 | return rc; | |
490 | } | |
491 | ||
492 | static void get_pids_locks(void *locks, void (*add_lock)(void *, struct lock *)) | |
493 | { | |
494 | DIR *dir; | |
495 | struct dirent *d; | |
496 | struct path_cxt *pc = NULL; | |
497 | ||
498 | pc = ul_new_path(NULL); | |
499 | if (!pc) | |
500 | err(EXIT_FAILURE, _("failed to alloc procfs handler")); | |
501 | ||
502 | dir = opendir(_PATH_PROC); | |
503 | if (!dir) | |
504 | err(EXIT_FAILURE, _("failed to open /proc")); | |
505 | ||
506 | while ((d = readdir(dir))) { | |
507 | pid_t pid; | |
508 | char buf[BUFSIZ]; | |
509 | const char *cmdname = NULL; | |
510 | ||
511 | if (procfs_dirent_get_pid(d, &pid) != 0) | |
512 | continue; | |
513 | ||
514 | if (procfs_process_init_path(pc, pid) != 0) | |
515 | continue; | |
516 | ||
517 | if (procfs_process_get_cmdname(pc, buf, sizeof(buf)) <= 0) | |
518 | continue; | |
519 | cmdname = buf; | |
520 | ||
521 | get_pid_locks(locks, add_lock, pc, pid, cmdname); | |
522 | } | |
523 | ||
524 | closedir(dir); | |
525 | ul_unref_path(pc); | |
526 | ||
527 | return; | |
528 | } | |
529 | ||
530 | static int get_proc_locks(void *locks, void (*add_lock)(void *, struct lock *), void *fallback) | |
531 | { | |
532 | FILE *fp; | |
533 | char buf[PATH_MAX]; | |
534 | ||
535 | if (!(fp = fopen(_PATH_PROC_LOCKS, "r"))) | |
536 | return -1; | |
537 | ||
538 | while (fgets(buf, sizeof(buf), fp)) { | |
539 | struct lock *l = get_lock(buf, NULL, fallback); | |
540 | if (l) | |
541 | add_lock(locks, l); | |
542 | } | |
543 | ||
544 | fclose(fp); | |
545 | return 0; | |
546 | } | |
547 | ||
548 | static int column_name_to_id(const char *name, size_t namesz) | |
549 | { | |
550 | size_t i; | |
551 | ||
552 | assert(name); | |
553 | ||
554 | for (i = 0; i < ARRAY_SIZE(infos); i++) { | |
555 | const char *cn = infos[i].name; | |
556 | ||
557 | if (!c_strncasecmp(name, cn, namesz) && !*(cn + namesz)) | |
558 | return i; | |
559 | } | |
560 | warnx(_("unknown column: %s"), name); | |
561 | return -1; | |
562 | } | |
563 | ||
564 | static inline int get_column_id(int num) | |
565 | { | |
566 | assert(num >= 0); | |
567 | assert((size_t) num < ncolumns); | |
568 | assert(columns[num] < (int) ARRAY_SIZE(infos)); | |
569 | ||
570 | return columns[num]; | |
571 | } | |
572 | ||
573 | ||
574 | static inline const struct colinfo *get_column_info(unsigned num) | |
575 | { | |
576 | return &infos[ get_column_id(num) ]; | |
577 | } | |
578 | ||
579 | static pid_t get_blocker(int id, struct list_head *locks) | |
580 | { | |
581 | struct list_head *p; | |
582 | ||
583 | list_for_each(p, locks) { | |
584 | struct lock *l = list_entry(p, struct lock, locks); | |
585 | ||
586 | if (l->id == id && !l->blocked) | |
587 | return l->pid; | |
588 | } | |
589 | ||
590 | return 0; | |
591 | } | |
592 | ||
593 | static void xstrcoholder(char **str, struct lock *l) | |
594 | { | |
595 | xstrfappend(str, "%d,%s,%d", | |
596 | l->pid, l->cmdname, l->fd); | |
597 | } | |
598 | ||
599 | static void add_scols_line(struct libscols_table *table, struct lock *l, struct list_head *locks, void *pid_locks) | |
600 | { | |
601 | size_t i; | |
602 | struct libscols_line *line; | |
603 | /* | |
604 | * Whenever cmdname or filename is NULL it is most | |
605 | * likely because there's no read permissions | |
606 | * for the specified process. | |
607 | */ | |
608 | const char *notfnd = ""; | |
609 | ||
610 | assert(l); | |
611 | assert(table); | |
612 | ||
613 | line = scols_table_new_line(table, NULL); | |
614 | if (!line) | |
615 | err(EXIT_FAILURE, _("failed to allocate output line")); | |
616 | ||
617 | for (i = 0; i < ncolumns; i++) { | |
618 | char *str = NULL; | |
619 | ||
620 | switch (get_column_id(i)) { | |
621 | case COL_SRC: | |
622 | xasprintf(&str, "%s", l->cmdname ? l->cmdname : notfnd); | |
623 | break; | |
624 | case COL_PID: | |
625 | xasprintf(&str, "%d", l->pid); | |
626 | break; | |
627 | case COL_TYPE: | |
628 | xasprintf(&str, "%s", l->type); | |
629 | break; | |
630 | case COL_INODE: | |
631 | xasprintf(&str, "%ju", (uintmax_t) l->inode); | |
632 | break; | |
633 | case COL_MAJMIN: | |
634 | if (json || raw) | |
635 | xasprintf(&str, "%u:%u", major(l->dev), minor(l->dev)); | |
636 | else | |
637 | xasprintf(&str, "%3u:%-3u", major(l->dev), minor(l->dev)); | |
638 | break; | |
639 | case COL_SIZE: | |
640 | if (!l->size) | |
641 | break; | |
642 | if (bytes) | |
643 | xasprintf(&str, "%ju", l->size); | |
644 | else | |
645 | str = size_to_human_string(SIZE_SUFFIX_1LETTER, l->size); | |
646 | break; | |
647 | case COL_MODE: | |
648 | xasprintf(&str, "%s%s", l->mode, l->blocked ? "*" : ""); | |
649 | break; | |
650 | case COL_M: | |
651 | xasprintf(&str, "%d", l->mandatory ? 1 : 0); | |
652 | break; | |
653 | case COL_START: | |
654 | xasprintf(&str, "%jd", l->start); | |
655 | break; | |
656 | case COL_END: | |
657 | xasprintf(&str, "%jd", l->end); | |
658 | break; | |
659 | case COL_PATH: | |
660 | xasprintf(&str, "%s", l->path ? l->path : notfnd); | |
661 | break; | |
662 | case COL_BLOCKER: | |
663 | { | |
664 | pid_t bl = l->blocked && l->id ? | |
665 | get_blocker(l->id, locks) : 0; | |
666 | if (bl) | |
667 | xasprintf(&str, "%d", (int) bl); | |
668 | break; | |
669 | } | |
670 | case COL_HOLDERS: | |
671 | { | |
672 | struct lock_tnode tmp = { .dev = l->dev, .inode = l->inode, }; | |
673 | struct lock_tnode **head = tfind(&tmp, pid_locks, lock_tnode_compare); | |
674 | struct list_head *p; | |
675 | ||
676 | if (!head) | |
677 | break; | |
678 | ||
679 | list_for_each(p, &(*head)->chain) { | |
680 | struct lock *m = list_entry(p, struct lock, locks); | |
681 | ||
682 | if (!is_holder(l, m)) | |
683 | continue; | |
684 | ||
685 | if (str) | |
686 | xstrputc(&str, '\n'); | |
687 | xstrcoholder(&str, m); | |
688 | } | |
689 | break; | |
690 | } | |
691 | default: | |
692 | break; | |
693 | } | |
694 | ||
695 | if (str && scols_line_refer_data(line, i, str)) | |
696 | err(EXIT_FAILURE, _("failed to add output data")); | |
697 | } | |
698 | } | |
699 | ||
700 | static void rem_locks(struct list_head *locks) | |
701 | { | |
702 | struct list_head *p, *pnext; | |
703 | ||
704 | /* destroy the list */ | |
705 | list_for_each_safe(p, pnext, locks) { | |
706 | struct lock *l = list_entry(p, struct lock, locks); | |
707 | rem_lock(l); | |
708 | } | |
709 | } | |
710 | ||
711 | static void rem_tnode(void *node) | |
712 | { | |
713 | struct lock_tnode *tnode = node; | |
714 | ||
715 | rem_locks(&tnode->chain); | |
716 | free(node); | |
717 | } | |
718 | ||
719 | static int get_json_type_for_column(int column_id, int representing_in_bytes) | |
720 | { | |
721 | switch (column_id) { | |
722 | case COL_SIZE: | |
723 | if (!representing_in_bytes) | |
724 | return SCOLS_JSON_STRING; | |
725 | FALLTHROUGH; | |
726 | case COL_PID: | |
727 | case COL_START: | |
728 | case COL_END: | |
729 | case COL_BLOCKER: | |
730 | case COL_INODE: | |
731 | return SCOLS_JSON_NUMBER; | |
732 | case COL_M: | |
733 | return SCOLS_JSON_BOOLEAN; | |
734 | case COL_HOLDERS: | |
735 | return SCOLS_JSON_ARRAY_STRING; | |
736 | default: | |
737 | return SCOLS_JSON_STRING; | |
738 | } | |
739 | } | |
740 | ||
741 | static int show_locks(struct list_head *locks, pid_t target_pid, void *pid_locks) | |
742 | { | |
743 | int rc = 0; | |
744 | size_t i; | |
745 | struct list_head *p; | |
746 | struct libscols_table *table; | |
747 | ||
748 | table = scols_new_table(); | |
749 | if (!table) | |
750 | err(EXIT_FAILURE, _("failed to allocate output table")); | |
751 | ||
752 | scols_table_enable_raw(table, raw); | |
753 | scols_table_enable_json(table, json); | |
754 | scols_table_enable_noheadings(table, no_headings); | |
755 | ||
756 | if (json) | |
757 | scols_table_set_name(table, "locks"); | |
758 | ||
759 | for (i = 0; i < ncolumns; i++) { | |
760 | struct libscols_column *cl; | |
761 | const struct colinfo *col = get_column_info(i); | |
762 | ||
763 | cl = scols_table_new_column(table, col->name, col->whint, col->flags); | |
764 | if (!cl) | |
765 | err(EXIT_FAILURE, _("failed to allocate output column")); | |
766 | ||
767 | if (col->flags & SCOLS_FL_WRAP) { | |
768 | scols_column_set_wrapfunc(cl, | |
769 | scols_wrapnl_chunksize, | |
770 | scols_wrapnl_nextchunk, | |
771 | NULL); | |
772 | scols_column_set_safechars(cl, "\n"); | |
773 | } | |
774 | ||
775 | if (json) { | |
776 | int id = get_column_id(i); | |
777 | int json_type = get_json_type_for_column(id, bytes); | |
778 | scols_column_set_json_type(cl, json_type); | |
779 | } | |
780 | ||
781 | } | |
782 | ||
783 | /* prepare data for output */ | |
784 | list_for_each(p, locks) { | |
785 | struct lock *l = list_entry(p, struct lock, locks); | |
786 | ||
787 | if (target_pid && target_pid != l->pid) | |
788 | continue; | |
789 | ||
790 | add_scols_line(table, l, locks, pid_locks); | |
791 | } | |
792 | ||
793 | scols_print_table(table); | |
794 | scols_unref_table(table); | |
795 | return rc; | |
796 | } | |
797 | ||
798 | ||
799 | static void __attribute__((__noreturn__)) usage(void) | |
800 | { | |
801 | FILE *out = stdout; | |
802 | ||
803 | fputs(USAGE_HEADER, out); | |
804 | ||
805 | fprintf(out, | |
806 | _(" %s [options]\n"), program_invocation_short_name); | |
807 | ||
808 | fputs(USAGE_SEPARATOR, out); | |
809 | fputs(_("List local system locks.\n"), out); | |
810 | ||
811 | fputs(USAGE_OPTIONS, out); | |
812 | fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out); | |
813 | fputs(_(" -J, --json use JSON output format\n"), out); | |
814 | fputs(_(" -i, --noinaccessible ignore locks without read permissions\n"), out); | |
815 | fputs(_(" -n, --noheadings don't print headings\n"), out); | |
816 | fputs(_(" -o, --output <list> output columns (see --list-columns)\n"), out); | |
817 | fputs(_(" --output-all output all columns\n"), out); | |
818 | fputs(_(" -p, --pid <pid> display only locks held by this process\n"), out); | |
819 | fputs(_(" -r, --raw use the raw output format\n"), out); | |
820 | fputs(_(" -u, --notruncate don't truncate text in columns\n"), out); | |
821 | ||
822 | fputs(USAGE_SEPARATOR, out); | |
823 | fputs(_(" -H, --list-columns list the available columns\n"), out); | |
824 | fprintf(out, USAGE_HELP_OPTIONS(24)); | |
825 | fprintf(out, USAGE_MAN_TAIL("lslocks(8)")); | |
826 | ||
827 | exit(EXIT_SUCCESS); | |
828 | } | |
829 | ||
830 | static void __attribute__((__noreturn__)) list_colunms(void) | |
831 | { | |
832 | struct libscols_table *col_tb = xcolumn_list_table_new( | |
833 | "lslocks-columns", stdout, raw, json); | |
834 | ||
835 | for (size_t i = 0; i < ARRAY_SIZE(infos); i++) { | |
836 | if (i != COL_SIZE) { | |
837 | int json_type = get_json_type_for_column(i, bytes); | |
838 | xcolumn_list_table_append_line(col_tb, infos[i].name, | |
839 | json_type, NULL, | |
840 | _(infos[i].help)); | |
841 | } else | |
842 | xcolumn_list_table_append_line(col_tb, infos[i].name, | |
843 | -1, "<string|number>", | |
844 | _(infos[i].help)); | |
845 | } | |
846 | ||
847 | scols_print_table(col_tb); | |
848 | scols_unref_table(col_tb); | |
849 | ||
850 | exit(EXIT_SUCCESS); | |
851 | } | |
852 | ||
853 | int main(int argc, char *argv[]) | |
854 | { | |
855 | int c, rc = 0, collist = 0; | |
856 | struct list_head proc_locks; | |
857 | void *pid_locks = NULL; | |
858 | char *outarg = NULL; | |
859 | enum { | |
860 | OPT_OUTPUT_ALL = CHAR_MAX + 1 | |
861 | }; | |
862 | static const struct option long_opts[] = { | |
863 | { "bytes", no_argument, NULL, 'b' }, | |
864 | { "json", no_argument, NULL, 'J' }, | |
865 | { "pid", required_argument, NULL, 'p' }, | |
866 | { "help", no_argument, NULL, 'h' }, | |
867 | { "output", required_argument, NULL, 'o' }, | |
868 | { "output-all", no_argument, NULL, OPT_OUTPUT_ALL }, | |
869 | { "notruncate", no_argument, NULL, 'u' }, | |
870 | { "version", no_argument, NULL, 'V' }, | |
871 | { "noheadings", no_argument, NULL, 'n' }, | |
872 | { "raw", no_argument, NULL, 'r' }, | |
873 | { "noinaccessible", no_argument, NULL, 'i' }, | |
874 | { "list-columns", no_argument, NULL, 'H' }, | |
875 | { NULL, 0, NULL, 0 } | |
876 | }; | |
877 | ||
878 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ | |
879 | { 'J','r' }, | |
880 | { 0 } | |
881 | }; | |
882 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
883 | pid_t target_pid = 0; | |
884 | ||
885 | setlocale(LC_ALL, ""); | |
886 | bindtextdomain(PACKAGE, LOCALEDIR); | |
887 | textdomain(PACKAGE); | |
888 | close_stdout_atexit(); | |
889 | ||
890 | while ((c = getopt_long(argc, argv, | |
891 | "biJp:o:nruhVH", long_opts, NULL)) != -1) { | |
892 | ||
893 | err_exclusive_options(c, long_opts, excl, excl_st); | |
894 | ||
895 | switch(c) { | |
896 | case 'b': | |
897 | bytes = 1; | |
898 | break; | |
899 | case 'i': | |
900 | no_inaccessible = 1; | |
901 | break; | |
902 | case 'J': | |
903 | json = 1; | |
904 | break; | |
905 | case 'p': | |
906 | target_pid = strtopid_or_err(optarg, _("invalid PID argument")); | |
907 | break; | |
908 | case 'o': | |
909 | outarg = optarg; | |
910 | break; | |
911 | case OPT_OUTPUT_ALL: | |
912 | for (ncolumns = 0; ncolumns < ARRAY_SIZE(infos); ncolumns++) | |
913 | columns[ncolumns] = ncolumns; | |
914 | break; | |
915 | case 'n': | |
916 | no_headings = 1; | |
917 | break; | |
918 | case 'r': | |
919 | raw = 1; | |
920 | break; | |
921 | case 'u': | |
922 | disable_columns_truncate(); | |
923 | break; | |
924 | ||
925 | case 'H': | |
926 | collist = 1; | |
927 | break; | |
928 | case 'V': | |
929 | print_version(EXIT_SUCCESS); | |
930 | case 'h': | |
931 | usage(); | |
932 | default: | |
933 | errtryhelp(EXIT_FAILURE); | |
934 | } | |
935 | } | |
936 | ||
937 | if (collist) | |
938 | list_colunms(); /* print end exit */ | |
939 | ||
940 | INIT_LIST_HEAD(&proc_locks); | |
941 | ||
942 | if (!ncolumns) { | |
943 | /* default columns */ | |
944 | columns[ncolumns++] = COL_SRC; | |
945 | columns[ncolumns++] = COL_PID; | |
946 | columns[ncolumns++] = COL_TYPE; | |
947 | columns[ncolumns++] = COL_SIZE; | |
948 | columns[ncolumns++] = COL_MODE; | |
949 | columns[ncolumns++] = COL_M; | |
950 | columns[ncolumns++] = COL_START; | |
951 | columns[ncolumns++] = COL_END; | |
952 | columns[ncolumns++] = COL_PATH; | |
953 | } | |
954 | ||
955 | if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns), | |
956 | &ncolumns, column_name_to_id) < 0) | |
957 | return EXIT_FAILURE; | |
958 | ||
959 | scols_init_debug(0); | |
960 | ||
961 | /* get_pids_locks() get locks related information from "lock:" fields | |
962 | * of /proc/$pid/fdinfo/$fd as fallback information. | |
963 | * get_proc_locks() used the fallback information if /proc/locks | |
964 | * doesn't provides enough information or provides staled information. */ | |
965 | get_pids_locks(&pid_locks, add_to_tree); | |
966 | rc = get_proc_locks(&proc_locks, add_to_list, &pid_locks); | |
967 | ||
968 | if (!rc && !list_empty(&proc_locks)) | |
969 | rc = show_locks(&proc_locks, target_pid, &pid_locks); | |
970 | ||
971 | tdestroy(pid_locks, rem_tnode); | |
972 | rem_locks(&proc_locks); | |
973 | ||
974 | mnt_unref_table(tab); | |
975 | return rc; | |
976 | } |