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