File: error.c

package info (click to toggle)
util-linux 2.42~rc1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 101,864 kB
  • sloc: ansic: 185,733; sh: 24,450; yacc: 1,288; makefile: 534; xml: 422; python: 316; lex: 89; ruby: 75; csh: 37; exp: 19; sed: 16; perl: 15; sql: 9
file content (141 lines) | stat: -rw-r--r-- 3,523 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * lsfd(1) - list file descriptors
 *
 * Copyright (C) 2021-2026 Red Hat, Inc. All rights reserved.
 * Written by Masatake YAMATO <yamato@redhat.com>
 *
 * Very generally based on lsof(8) by Victor A. Abell <abe@purdue.edu>
 * It supports multiple OSes. lsfd specializes to Linux.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "lsfd.h"

/*
 * Error classes
 */

/* get_errno_name() --- the private replacement of strerrorname_np(3).
 * Some platforms don't have strerrorname_np.
 *
 * Mainly copied from misc-utils/enosys.c.
 */
struct errno_s {
	const char *const name;
	long number;
};

static const struct errno_s errnos[] = {
#define UL_ERRNO(name, nr) { name, nr },
#include "errnos.h"
#undef UL_ERRNO
};

static const char *get_errno_name(int ern)
{
	if (!ARRAY_SIZE(errnos))
		return NULL;

	for (size_t i = 0; i < ARRAY_SIZE(errnos); i ++) {
		if (errnos[i].number == ern)
			return errnos[i].name;
	}
	return NULL;
}

static bool error_fill_column(struct proc *proc __attribute__((__unused__)),
			      struct file *file,
			      struct libscols_line *ln,
			      int column_id,
			      size_t column_index)
{
	char *str = NULL;
	const char *ename;

	switch(column_id) {
	case COL_TYPE:
		if (scols_line_set_data(ln, column_index, "ERROR"))
			err(EXIT_FAILURE, _("failed to add output data"));
		return true;
	case COL_SOURCE:
		ename = get_errno_name(file->error.number);
		if (ename)
			xasprintf(&str, "%s:%s",
				  file->error.syscall, ename);
		else
			xasprintf(&str, "%s:unknown(%d)",
				  file->error.syscall, file->error.number);
		if (scols_line_refer_data(ln, column_index, str))
			err(EXIT_FAILURE, _("failed to add output data"));
		return true;
	default:
		return false;
	}
}

static void error_file_free_content(struct file *file)
{
	free(file->name);	/* NULL is acceptable.  */
}

static const struct file_class error_class = {
	.super = &abst_class,
	.size = sizeof(struct file),
	.free_content = error_file_free_content,
	.fill_column = error_fill_column,
};

static bool readlink_error_fill_column(struct proc *proc __attribute__((__unused__)),
				       struct file *file __attribute__((__unused__)),
				       struct libscols_line *ln __attribute__((__unused__)),
				       int column_id,
				       size_t column_index __attribute__((__unused__)))
{
	switch(column_id) {
	case COL_NAME:
	case COL_KNAME:
		return true;
	default:
		return false;
	}
}

const struct file_class readlink_error_class = {
	.super = &error_class,
	.size = sizeof(struct file),
	.fill_column = readlink_error_fill_column,
};

const struct file_class stat_error_class = {
	.super = &error_class,
	.size = sizeof(struct file),
};

bool is_error_object(struct file *f)
{
	const struct file_class *c;

	assert(f);
	c = f->class;

	while (c) {
		if (c == &error_class)
			return true;
		c = c->super;
	}

	return false;
}