]>
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> | |
34 | ||
07c916cf | 35 | #include <libmount.h> |
ba1bf716 | 36 | #include <libsmartcols.h> |
07c916cf | 37 | |
3dc02ef4 DB |
38 | #include "pathnames.h" |
39 | #include "canonicalize.h" | |
40 | #include "nls.h" | |
3dc02ef4 | 41 | #include "xalloc.h" |
3dc02ef4 DB |
42 | #include "strutils.h" |
43 | #include "c.h" | |
ba1bf716 | 44 | #include "list.h" |
c05a80ca | 45 | #include "closestream.h" |
b3a2e889 | 46 | #include "optutils.h" |
0e756dae | 47 | #include "procutils.h" |
3dc02ef4 DB |
48 | |
49 | /* column IDs */ | |
50 | enum { | |
51 | COL_SRC = 0, | |
52 | COL_PID, | |
74fa8244 | 53 | COL_TYPE, |
3dc02ef4 | 54 | COL_SIZE, |
74fa8244 | 55 | COL_MODE, |
3dc02ef4 DB |
56 | COL_M, |
57 | COL_START, | |
58 | COL_END, | |
59 | COL_PATH, | |
7badb909 | 60 | COL_BLOCKER |
3dc02ef4 DB |
61 | }; |
62 | ||
63 | /* column names */ | |
64 | struct colinfo { | |
65 | const char *name; /* header */ | |
66 | double whint; /* width hint (N < 1 is in percent of termwidth) */ | |
ba1bf716 | 67 | int flags; /* SCOLS_FL_* */ |
3dc02ef4 DB |
68 | const char *help; |
69 | }; | |
70 | ||
71 | /* columns descriptions */ | |
c4137d39 | 72 | static struct colinfo infos[] = { |
74fa8244 | 73 | [COL_SRC] = { "COMMAND",15, 0, N_("command of the process holding the lock") }, |
ba1bf716 | 74 | [COL_PID] = { "PID", 5, SCOLS_FL_RIGHT, N_("PID of the process holding the lock") }, |
4707bc83 | 75 | [COL_TYPE] = { "TYPE", 5, SCOLS_FL_RIGHT, N_("kind of lock") }, |
ba1bf716 | 76 | [COL_SIZE] = { "SIZE", 4, SCOLS_FL_RIGHT, N_("size of the lock") }, |
74fa8244 DB |
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)")}, | |
ba1bf716 OO |
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") } | |
3dc02ef4 | 83 | }; |
21abf83d KZ |
84 | |
85 | static int columns[ARRAY_SIZE(infos) * 2]; | |
40b17508 | 86 | static size_t ncolumns; |
21abf83d | 87 | |
3dc02ef4 | 88 | static pid_t pid = 0; |
3dc02ef4 | 89 | |
07c916cf KZ |
90 | static struct libmnt_table *tab; /* /proc/self/mountinfo */ |
91 | ||
ba1bf716 OO |
92 | /* basic output flags */ |
93 | static int no_headings; | |
f29bc6e1 | 94 | static int no_inaccessible; |
ba1bf716 | 95 | static int raw; |
b3a2e889 | 96 | static int json; |
51b0bcf0 | 97 | static int bytes; |
ba1bf716 | 98 | |
3dc02ef4 DB |
99 | struct lock { |
100 | struct list_head locks; | |
101 | ||
102 | char *cmdname; | |
103 | pid_t pid; | |
104 | char *path; | |
105 | char *type; | |
74fa8244 | 106 | char *mode; |
3dc02ef4 DB |
107 | off_t start; |
108 | off_t end; | |
55c0d16b KZ |
109 | unsigned int mandatory :1, |
110 | blocked :1; | |
51b0bcf0 | 111 | uint64_t size; |
7badb909 | 112 | int id; |
3dc02ef4 DB |
113 | }; |
114 | ||
f29bc6e1 KZ |
115 | static void rem_lock(struct lock *lock) |
116 | { | |
117 | if (!lock) | |
118 | return; | |
119 | ||
120 | free(lock->path); | |
f29bc6e1 KZ |
121 | free(lock->mode); |
122 | free(lock->cmdname); | |
123 | free(lock->type); | |
124 | list_del(&lock->locks); | |
125 | free(lock); | |
126 | } | |
127 | ||
c4137d39 KZ |
128 | static void disable_columns_truncate(void) |
129 | { | |
130 | size_t i; | |
131 | ||
21abf83d | 132 | for (i = 0; i < ARRAY_SIZE(infos); i++) |
ba1bf716 | 133 | infos[i].flags &= ~SCOLS_FL_TRUNC; |
c4137d39 KZ |
134 | } |
135 | ||
3dc02ef4 DB |
136 | /* |
137 | * Associate the device's mountpoint for a filename | |
138 | */ | |
139 | static char *get_fallback_filename(dev_t dev) | |
140 | { | |
07c916cf | 141 | struct libmnt_fs *fs; |
f29bc6e1 | 142 | char *res = NULL; |
3dc02ef4 | 143 | |
07c916cf KZ |
144 | if (!tab) { |
145 | tab = mnt_new_table_from_file(_PATH_PROC_MOUNTINFO); | |
146 | if (!tab) | |
147 | return NULL; | |
148 | } | |
3dc02ef4 | 149 | |
07c916cf KZ |
150 | fs = mnt_table_find_devno(tab, dev, MNT_ITER_BACKWARD); |
151 | if (!fs) | |
152 | return NULL; | |
3dc02ef4 | 153 | |
f29bc6e1 KZ |
154 | xasprintf(&res, "%s...", mnt_fs_get_target(fs)); |
155 | return res; | |
3dc02ef4 DB |
156 | } |
157 | ||
158 | /* | |
159 | * Return the absolute path of a file from | |
160 | * a given inode number (and its size) | |
161 | */ | |
7ee26cbf | 162 | static char *get_filename_sz(ino_t inode, pid_t lock_pid, size_t *size) |
3dc02ef4 DB |
163 | { |
164 | struct stat sb; | |
165 | struct dirent *dp; | |
166 | DIR *dirp; | |
167 | size_t len; | |
168 | int fd; | |
169 | char path[PATH_MAX], sym[PATH_MAX], *ret = NULL; | |
170 | ||
171 | *size = 0; | |
172 | memset(path, 0, sizeof(path)); | |
173 | memset(sym, 0, sizeof(sym)); | |
174 | ||
175 | /* | |
176 | * We know the pid so we don't have to | |
177 | * iterate the *entire* filesystem searching | |
178 | * for the damn file. | |
179 | */ | |
09eee8b4 | 180 | snprintf(path, sizeof(path), "/proc/%d/fd/", lock_pid); |
3dc02ef4 DB |
181 | if (!(dirp = opendir(path))) |
182 | return NULL; | |
183 | ||
184 | if ((len = strlen(path)) >= (sizeof(path) - 2)) | |
185 | goto out; | |
186 | ||
187 | if ((fd = dirfd(dirp)) < 0 ) | |
188 | goto out; | |
189 | ||
190 | while ((dp = readdir(dirp))) { | |
191 | if (!strcmp(dp->d_name, ".") || | |
192 | !strcmp(dp->d_name, "..")) | |
193 | continue; | |
194 | ||
93d01a62 KZ |
195 | errno = 0; |
196 | ||
3dc02ef4 | 197 | /* care only for numerical descriptors */ |
93d01a62 | 198 | if (!strtol(dp->d_name, (char **) NULL, 10) || errno) |
3dc02ef4 DB |
199 | continue; |
200 | ||
2208b3cc | 201 | if (!fstatat(fd, dp->d_name, &sb, 0) |
3dc02ef4 DB |
202 | && inode != sb.st_ino) |
203 | continue; | |
204 | ||
2208b3cc | 205 | if ((len = readlinkat(fd, dp->d_name, sym, sizeof(sym) - 1)) < 1) |
3dc02ef4 DB |
206 | goto out; |
207 | ||
208 | *size = sb.st_size; | |
209 | sym[len] = '\0'; | |
210 | ||
211 | ret = xstrdup(sym); | |
212 | break; | |
fbf9034e | 213 | } |
3dc02ef4 DB |
214 | out: |
215 | closedir(dirp); | |
216 | return ret; | |
217 | } | |
218 | ||
219 | /* | |
220 | * Return the inode number from a string | |
221 | */ | |
222 | static ino_t get_dev_inode(char *str, dev_t *dev) | |
223 | { | |
64a265bf | 224 | unsigned int maj = 0, min = 0; |
3dc02ef4 DB |
225 | ino_t inum = 0; |
226 | ||
46d4ce56 | 227 | sscanf(str, "%02x:%02x:%ju", &maj, &min, &inum); |
3dc02ef4 DB |
228 | |
229 | *dev = (dev_t) makedev(maj, min); | |
230 | return inum; | |
231 | } | |
232 | ||
233 | static int get_local_locks(struct list_head *locks) | |
234 | { | |
235 | int i; | |
236 | ino_t inode = 0; | |
237 | FILE *fp; | |
f29bc6e1 | 238 | char buf[PATH_MAX], *tok = NULL; |
3dc02ef4 DB |
239 | size_t sz; |
240 | struct lock *l; | |
241 | dev_t dev = 0; | |
242 | ||
243 | if (!(fp = fopen(_PATH_PROC_LOCKS, "r"))) | |
244 | return -1; | |
245 | ||
246 | while (fgets(buf, sizeof(buf), fp)) { | |
247 | ||
248 | l = xcalloc(1, sizeof(*l)); | |
249 | INIT_LIST_HEAD(&l->locks); | |
250 | ||
251 | for (tok = strtok(buf, " "), i = 0; tok; | |
252 | tok = strtok(NULL, " "), i++) { | |
253 | ||
254 | /* | |
74fa8244 | 255 | * /proc/locks has *exactly* 8 "blocks" of text |
3dc02ef4 DB |
256 | * separated by ' ' - check <kernel>/fs/locks.c |
257 | */ | |
258 | switch (i) { | |
7badb909 KZ |
259 | case 0: /* ID: */ |
260 | tok[strlen(tok) - 1] = '\0'; | |
261 | l->id = strtos32_or_err(tok, _("failed to parse ID")); | |
74fa8244 DB |
262 | break; |
263 | case 1: /* posix, flock, etc */ | |
55c0d16b KZ |
264 | if (strcmp(tok, "->") == 0) { /* optional field */ |
265 | l->blocked = 1; | |
266 | i--; | |
267 | } else | |
268 | l->type = xstrdup(tok); | |
3dc02ef4 DB |
269 | break; |
270 | ||
271 | case 2: /* is this a mandatory lock? other values are advisory or noinode */ | |
55c0d16b | 272 | l->mandatory = *tok == 'M' ? 1 : 0; |
3dc02ef4 | 273 | break; |
74fa8244 DB |
274 | case 3: /* lock mode */ |
275 | l->mode = xstrdup(tok); | |
3dc02ef4 DB |
276 | break; |
277 | ||
278 | case 4: /* PID */ | |
279 | /* | |
280 | * If user passed a pid we filter it later when adding | |
2aaf56c4 | 281 | * to the list, no need to worry now. OFD locks use -1 PID. |
3dc02ef4 | 282 | */ |
db41a429 | 283 | l->pid = strtos32_or_err(tok, _("failed to parse pid")); |
2aaf56c4 KZ |
284 | if (l->pid > 0) { |
285 | l->cmdname = proc_get_command_name(l->pid); | |
286 | if (!l->cmdname) | |
287 | l->cmdname = xstrdup(_("(unknown)")); | |
288 | } else | |
289 | l->cmdname = xstrdup(_("(undefined)")); | |
3dc02ef4 DB |
290 | break; |
291 | ||
292 | case 5: /* device major:minor and inode number */ | |
293 | inode = get_dev_inode(tok, &dev); | |
294 | break; | |
295 | ||
296 | case 6: /* start */ | |
297 | l->start = !strcmp(tok, "EOF") ? 0 : | |
db41a429 | 298 | strtou64_or_err(tok, _("failed to parse start")); |
3dc02ef4 DB |
299 | break; |
300 | ||
301 | case 7: /* end */ | |
302 | /* replace '\n' character */ | |
303 | tok[strlen(tok)-1] = '\0'; | |
304 | l->end = !strcmp(tok, "EOF") ? 0 : | |
db41a429 | 305 | strtou64_or_err(tok, _("failed to parse end")); |
3dc02ef4 DB |
306 | break; |
307 | default: | |
308 | break; | |
309 | } | |
f29bc6e1 | 310 | } |
3dc02ef4 | 311 | |
f29bc6e1 | 312 | l->path = get_filename_sz(inode, l->pid, &sz); |
3dc02ef4 | 313 | |
f29bc6e1 KZ |
314 | /* no permissions -- ignore */ |
315 | if (!l->path && no_inaccessible) { | |
316 | rem_lock(l); | |
317 | continue; | |
3dc02ef4 DB |
318 | } |
319 | ||
f29bc6e1 KZ |
320 | if (!l->path) { |
321 | /* probably no permission to peek into l->pid's path */ | |
322 | l->path = get_fallback_filename(dev); | |
51b0bcf0 | 323 | l->size = 0; |
f29bc6e1 | 324 | } else |
51b0bcf0 | 325 | l->size = sz; |
f29bc6e1 | 326 | |
3dc02ef4 DB |
327 | list_add(&l->locks, locks); |
328 | } | |
329 | ||
330 | fclose(fp); | |
331 | return 0; | |
332 | } | |
333 | ||
334 | static int column_name_to_id(const char *name, size_t namesz) | |
335 | { | |
336 | size_t i; | |
337 | ||
338 | assert(name); | |
339 | ||
21abf83d | 340 | for (i = 0; i < ARRAY_SIZE(infos); i++) { |
3dc02ef4 DB |
341 | const char *cn = infos[i].name; |
342 | ||
343 | if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) | |
344 | return i; | |
345 | } | |
346 | warnx(_("unknown column: %s"), name); | |
347 | return -1; | |
348 | } | |
349 | ||
350 | static inline int get_column_id(int num) | |
351 | { | |
40b17508 KZ |
352 | assert(num >= 0); |
353 | assert((size_t) num < ncolumns); | |
21abf83d | 354 | assert(columns[num] < (int) ARRAY_SIZE(infos)); |
3dc02ef4 DB |
355 | |
356 | return columns[num]; | |
357 | } | |
358 | ||
359 | ||
360 | static inline struct colinfo *get_column_info(unsigned num) | |
361 | { | |
362 | return &infos[ get_column_id(num) ]; | |
363 | } | |
364 | ||
7badb909 KZ |
365 | static pid_t get_blocker(int id, struct list_head *locks) |
366 | { | |
367 | struct list_head *p; | |
368 | ||
369 | list_for_each(p, locks) { | |
370 | struct lock *l = list_entry(p, struct lock, locks); | |
371 | ||
372 | if (l->id == id && !l->blocked) | |
373 | return l->pid; | |
374 | } | |
375 | ||
376 | return 0; | |
377 | } | |
378 | ||
ba1bf716 | 379 | static void add_scols_line(struct libscols_table *table, struct lock *l, struct list_head *locks) |
3dc02ef4 | 380 | { |
40b17508 | 381 | size_t i; |
ba1bf716 | 382 | struct libscols_line *line; |
3dc02ef4 DB |
383 | /* |
384 | * Whenever cmdname or filename is NULL it is most | |
385 | * likely because there's no read permissions | |
386 | * for the specified process. | |
387 | */ | |
388 | const char *notfnd = ""; | |
389 | ||
390 | assert(l); | |
ba1bf716 | 391 | assert(table); |
3dc02ef4 | 392 | |
ba1bf716 | 393 | line = scols_table_new_line(table, NULL); |
780ce22c KZ |
394 | if (!line) |
395 | err(EXIT_FAILURE, _("failed to allocate output line")); | |
3dc02ef4 DB |
396 | |
397 | for (i = 0; i < ncolumns; i++) { | |
398 | char *str = NULL; | |
3dc02ef4 DB |
399 | |
400 | switch (get_column_id(i)) { | |
401 | case COL_SRC: | |
0d7ebfc4 | 402 | xasprintf(&str, "%s", l->cmdname ? l->cmdname : notfnd); |
3dc02ef4 DB |
403 | break; |
404 | case COL_PID: | |
0d7ebfc4 | 405 | xasprintf(&str, "%d", l->pid); |
3dc02ef4 | 406 | break; |
74fa8244 | 407 | case COL_TYPE: |
0d7ebfc4 | 408 | xasprintf(&str, "%s", l->type); |
74fa8244 | 409 | break; |
3dc02ef4 | 410 | case COL_SIZE: |
51b0bcf0 KZ |
411 | if (!l->size) |
412 | break; | |
413 | if (bytes) | |
414 | xasprintf(&str, "%ju", l->size); | |
415 | else | |
416 | str = size_to_human_string(SIZE_SUFFIX_1LETTER, l->size); | |
3dc02ef4 | 417 | break; |
74fa8244 | 418 | case COL_MODE: |
55c0d16b | 419 | xasprintf(&str, "%s%s", l->mode, l->blocked ? "*" : ""); |
3dc02ef4 DB |
420 | break; |
421 | case COL_M: | |
55c0d16b | 422 | xasprintf(&str, "%d", l->mandatory ? 1 : 0); |
3dc02ef4 DB |
423 | break; |
424 | case COL_START: | |
46d4ce56 | 425 | xasprintf(&str, "%jd", l->start); |
3dc02ef4 DB |
426 | break; |
427 | case COL_END: | |
46d4ce56 | 428 | xasprintf(&str, "%jd", l->end); |
3dc02ef4 DB |
429 | break; |
430 | case COL_PATH: | |
0d7ebfc4 | 431 | xasprintf(&str, "%s", l->path ? l->path : notfnd); |
3dc02ef4 | 432 | break; |
7badb909 KZ |
433 | case COL_BLOCKER: |
434 | { | |
435 | pid_t bl = l->blocked && l->id ? | |
436 | get_blocker(l->id, locks) : 0; | |
437 | if (bl) | |
438 | xasprintf(&str, "%d", (int) bl); | |
439 | } | |
3dc02ef4 DB |
440 | default: |
441 | break; | |
442 | } | |
443 | ||
6ceb17c8 | 444 | if (str && scols_line_refer_data(line, i, str)) |
780ce22c | 445 | err(EXIT_FAILURE, _("failed to add output data")); |
3dc02ef4 DB |
446 | } |
447 | } | |
448 | ||
ba1bf716 | 449 | static int show_locks(struct list_head *locks) |
3dc02ef4 | 450 | { |
40b17508 KZ |
451 | int rc = 0; |
452 | size_t i; | |
3dc02ef4 | 453 | struct list_head *p, *pnext; |
ba1bf716 | 454 | struct libscols_table *table; |
3dc02ef4 | 455 | |
0925a9dd | 456 | table = scols_new_table(); |
780ce22c KZ |
457 | if (!table) |
458 | err(EXIT_FAILURE, _("failed to allocate output table")); | |
459 | ||
0925a9dd | 460 | scols_table_enable_raw(table, raw); |
b3a2e889 | 461 | scols_table_enable_json(table, json); |
0925a9dd | 462 | scols_table_enable_noheadings(table, no_headings); |
3dc02ef4 | 463 | |
b3a2e889 KZ |
464 | if (json) |
465 | scols_table_set_name(table, "locks"); | |
466 | ||
3dc02ef4 | 467 | for (i = 0; i < ncolumns; i++) { |
fffdff1e | 468 | struct libscols_column *cl; |
3dc02ef4 DB |
469 | struct colinfo *col = get_column_info(i); |
470 | ||
fffdff1e KZ |
471 | cl = scols_table_new_column(table, col->name, col->whint, col->flags); |
472 | if (!cl) | |
780ce22c | 473 | err(EXIT_FAILURE, _("failed to allocate output column")); |
fffdff1e KZ |
474 | |
475 | if (json) { | |
476 | int id = get_column_id(i); | |
477 | ||
478 | switch (id) { | |
479 | case COL_SIZE: | |
480 | if (!bytes) | |
481 | break; | |
482 | /* fallthrough */ | |
483 | case COL_PID: | |
484 | case COL_START: | |
485 | case COL_END: | |
486 | case COL_BLOCKER: | |
487 | scols_column_set_json_type(cl, SCOLS_JSON_NUMBER); | |
488 | break; | |
489 | case COL_M: | |
490 | scols_column_set_json_type(cl, SCOLS_JSON_BOOLEAN); | |
491 | break; | |
492 | default: | |
493 | scols_column_set_json_type(cl, SCOLS_JSON_STRING); | |
494 | break; | |
495 | } | |
496 | } | |
497 | ||
3dc02ef4 DB |
498 | } |
499 | ||
7badb909 KZ |
500 | /* prepare data for output */ |
501 | list_for_each(p, locks) { | |
502 | struct lock *l = list_entry(p, struct lock, locks); | |
503 | ||
504 | if (pid && pid != l->pid) | |
505 | continue; | |
506 | ||
ba1bf716 | 507 | add_scols_line(table, l, locks); |
7badb909 KZ |
508 | } |
509 | ||
510 | /* destroy the list */ | |
3dc02ef4 | 511 | list_for_each_safe(p, pnext, locks) { |
7badb909 KZ |
512 | struct lock *l = list_entry(p, struct lock, locks); |
513 | rem_lock(l); | |
3dc02ef4 DB |
514 | } |
515 | ||
ba1bf716 | 516 | scols_print_table(table); |
ba1bf716 | 517 | scols_unref_table(table); |
3dc02ef4 DB |
518 | return rc; |
519 | } | |
520 | ||
521 | ||
86be6a32 | 522 | static void __attribute__((__noreturn__)) usage(void) |
3dc02ef4 | 523 | { |
86be6a32 | 524 | FILE *out = stdout; |
3dc02ef4 DB |
525 | size_t i; |
526 | ||
527 | fputs(USAGE_HEADER, out); | |
528 | ||
529 | fprintf(out, | |
530 | _(" %s [options]\n"), program_invocation_short_name); | |
531 | ||
451dbcfa BS |
532 | fputs(USAGE_SEPARATOR, out); |
533 | fputs(_("List local system locks.\n"), out); | |
534 | ||
e9b46759 | 535 | fputs(USAGE_OPTIONS, out); |
51b0bcf0 | 536 | fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out); |
315be52f | 537 | fputs(_(" -J, --json use JSON output format\n"), out); |
f29bc6e1 | 538 | fputs(_(" -i, --noinaccessible ignore locks without read permissions\n"), out); |
315be52f BS |
539 | fputs(_(" -n, --noheadings don't print headings\n"), out); |
540 | fputs(_(" -o, --output <list> define which output columns to use\n"), out); | |
25d3c405 | 541 | fputs(_(" --output-all output all columns\n"), out); |
315be52f BS |
542 | fputs(_(" -p, --pid <pid> display only locks held by this process\n"), out); |
543 | fputs(_(" -r, --raw use the raw output format\n"), out); | |
544 | fputs(_(" -u, --notruncate don't truncate text in columns\n"), out); | |
545 | ||
546 | fputs(USAGE_SEPARATOR, out); | |
f45f3ec3 | 547 | printf(USAGE_HELP_OPTIONS(24)); |
3dc02ef4 | 548 | |
c3a4cfc5 | 549 | fputs(USAGE_COLUMNS, out); |
3dc02ef4 | 550 | |
21abf83d | 551 | for (i = 0; i < ARRAY_SIZE(infos); i++) |
3dc02ef4 DB |
552 | fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help)); |
553 | ||
f45f3ec3 | 554 | printf(USAGE_MAN_TAIL("lslocks(8)")); |
3dc02ef4 | 555 | |
86be6a32 | 556 | exit(EXIT_SUCCESS); |
3dc02ef4 DB |
557 | } |
558 | ||
559 | int main(int argc, char *argv[]) | |
560 | { | |
ba1bf716 | 561 | int c, rc = 0; |
3dc02ef4 | 562 | struct list_head locks; |
e68948e1 | 563 | char *outarg = NULL; |
25d3c405 SK |
564 | enum { |
565 | OPT_OUTPUT_ALL = CHAR_MAX + 1 | |
566 | }; | |
3dc02ef4 | 567 | static const struct option long_opts[] = { |
51b0bcf0 | 568 | { "bytes", no_argument, NULL, 'b' }, |
b3a2e889 | 569 | { "json", no_argument, NULL, 'J' }, |
3dc02ef4 DB |
570 | { "pid", required_argument, NULL, 'p' }, |
571 | { "help", no_argument, NULL, 'h' }, | |
572 | { "output", required_argument, NULL, 'o' }, | |
25d3c405 | 573 | { "output-all", no_argument, NULL, OPT_OUTPUT_ALL }, |
c4137d39 | 574 | { "notruncate", no_argument, NULL, 'u' }, |
3dc02ef4 DB |
575 | { "version", no_argument, NULL, 'V' }, |
576 | { "noheadings", no_argument, NULL, 'n' }, | |
577 | { "raw", no_argument, NULL, 'r' }, | |
f29bc6e1 | 578 | { "noinaccessible", no_argument, NULL, 'i' }, |
3dc02ef4 DB |
579 | { NULL, 0, NULL, 0 } |
580 | }; | |
581 | ||
b3a2e889 KZ |
582 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
583 | { 'J','r' }, | |
584 | { 0 } | |
585 | }; | |
586 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
3dc02ef4 DB |
587 | setlocale(LC_ALL, ""); |
588 | bindtextdomain(PACKAGE, LOCALEDIR); | |
589 | textdomain(PACKAGE); | |
2c308875 | 590 | close_stdout_atexit(); |
3dc02ef4 DB |
591 | |
592 | while ((c = getopt_long(argc, argv, | |
51b0bcf0 | 593 | "biJp:o:nruhV", long_opts, NULL)) != -1) { |
b3a2e889 KZ |
594 | |
595 | err_exclusive_options(c, long_opts, excl, excl_st); | |
3dc02ef4 DB |
596 | |
597 | switch(c) { | |
51b0bcf0 KZ |
598 | case 'b': |
599 | bytes = 1; | |
600 | break; | |
f29bc6e1 KZ |
601 | case 'i': |
602 | no_inaccessible = 1; | |
603 | break; | |
b3a2e889 KZ |
604 | case 'J': |
605 | json = 1; | |
606 | break; | |
3dc02ef4 | 607 | case 'p': |
db41a429 | 608 | pid = strtos32_or_err(optarg, _("invalid PID argument")); |
3dc02ef4 DB |
609 | break; |
610 | case 'o': | |
e68948e1 | 611 | outarg = optarg; |
3dc02ef4 | 612 | break; |
25d3c405 SK |
613 | case OPT_OUTPUT_ALL: |
614 | for (ncolumns = 0; ncolumns < ARRAY_SIZE(infos); ncolumns++) | |
615 | columns[ncolumns] = ncolumns; | |
616 | break; | |
3dc02ef4 | 617 | case 'n': |
ba1bf716 | 618 | no_headings = 1; |
3dc02ef4 DB |
619 | break; |
620 | case 'r': | |
ba1bf716 | 621 | raw = 1; |
3dc02ef4 | 622 | break; |
c4137d39 KZ |
623 | case 'u': |
624 | disable_columns_truncate(); | |
625 | break; | |
2c308875 KZ |
626 | |
627 | case 'V': | |
628 | print_version(EXIT_SUCCESS); | |
629 | case 'h': | |
630 | usage(); | |
3dc02ef4 | 631 | default: |
677ec86c | 632 | errtryhelp(EXIT_FAILURE); |
3dc02ef4 DB |
633 | } |
634 | } | |
635 | ||
636 | INIT_LIST_HEAD(&locks); | |
637 | ||
638 | if (!ncolumns) { | |
639 | /* default columns */ | |
640 | columns[ncolumns++] = COL_SRC; | |
641 | columns[ncolumns++] = COL_PID; | |
74fa8244 | 642 | columns[ncolumns++] = COL_TYPE; |
3dc02ef4 | 643 | columns[ncolumns++] = COL_SIZE; |
74fa8244 | 644 | columns[ncolumns++] = COL_MODE; |
3dc02ef4 DB |
645 | columns[ncolumns++] = COL_M; |
646 | columns[ncolumns++] = COL_START; | |
647 | columns[ncolumns++] = COL_END; | |
648 | columns[ncolumns++] = COL_PATH; | |
649 | } | |
650 | ||
e68948e1 KZ |
651 | if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns), |
652 | &ncolumns, column_name_to_id) < 0) | |
653 | return EXIT_FAILURE; | |
654 | ||
710ed55d KZ |
655 | scols_init_debug(0); |
656 | ||
3dc02ef4 DB |
657 | rc = get_local_locks(&locks); |
658 | ||
659 | if (!rc && !list_empty(&locks)) | |
ba1bf716 | 660 | rc = show_locks(&locks); |
3dc02ef4 | 661 | |
50fccba1 | 662 | mnt_unref_table(tab); |
3dc02ef4 DB |
663 | return rc; |
664 | } |