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