xref: /linux/tools/bpf/bpftool/common.c (revision 83a37b3292f4aca799b355179ad6fbdd78a08e10)
1 /*
2  * Copyright (C) 2017 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 /* Author: Jakub Kicinski <kubakici@wp.pl> */
35 
36 #include <errno.h>
37 #include <libgen.h>
38 #include <stdbool.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <linux/limits.h>
44 #include <linux/magic.h>
45 #include <sys/types.h>
46 #include <sys/vfs.h>
47 
48 #include <bpf.h>
49 
50 #include "main.h"
51 
52 static bool is_bpffs(char *path)
53 {
54 	struct statfs st_fs;
55 
56 	if (statfs(path, &st_fs) < 0)
57 		return false;
58 
59 	return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
60 }
61 
62 int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
63 {
64 	enum bpf_obj_type type;
65 	int fd;
66 
67 	fd = bpf_obj_get(path);
68 	if (fd < 0) {
69 		err("bpf obj get (%s): %s\n", path,
70 		    errno == EACCES && !is_bpffs(dirname(path)) ?
71 		    "directory not in bpf file system (bpffs)" :
72 		    strerror(errno));
73 		return -1;
74 	}
75 
76 	type = get_fd_type(fd);
77 	if (type < 0) {
78 		close(fd);
79 		return type;
80 	}
81 	if (type != exp_type) {
82 		err("incorrect object type: %s\n", get_fd_type_name(type));
83 		close(fd);
84 		return -1;
85 	}
86 
87 	return fd;
88 }
89 
90 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
91 {
92 	unsigned int id;
93 	char *endptr;
94 	int err;
95 	int fd;
96 
97 	if (!is_prefix(*argv, "id")) {
98 		err("expected 'id' got %s\n", *argv);
99 		return -1;
100 	}
101 	NEXT_ARG();
102 
103 	id = strtoul(*argv, &endptr, 0);
104 	if (*endptr) {
105 		err("can't parse %s as ID\n", *argv);
106 		return -1;
107 	}
108 	NEXT_ARG();
109 
110 	if (argc != 1)
111 		usage();
112 
113 	fd = get_fd_by_id(id);
114 	if (fd < 0) {
115 		err("can't get prog by id (%u): %s\n", id, strerror(errno));
116 		return -1;
117 	}
118 
119 	err = bpf_obj_pin(fd, *argv);
120 	close(fd);
121 	if (err) {
122 		err("can't pin the object (%s): %s\n", *argv,
123 		    errno == EACCES && !is_bpffs(dirname(*argv)) ?
124 		    "directory not in bpf file system (bpffs)" :
125 		    strerror(errno));
126 		return -1;
127 	}
128 
129 	return 0;
130 }
131 
132 const char *get_fd_type_name(enum bpf_obj_type type)
133 {
134 	static const char * const names[] = {
135 		[BPF_OBJ_UNKNOWN]	= "unknown",
136 		[BPF_OBJ_PROG]		= "prog",
137 		[BPF_OBJ_MAP]		= "map",
138 	};
139 
140 	if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
141 		return names[BPF_OBJ_UNKNOWN];
142 
143 	return names[type];
144 }
145 
146 int get_fd_type(int fd)
147 {
148 	char path[PATH_MAX];
149 	char buf[512];
150 	ssize_t n;
151 
152 	snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd);
153 
154 	n = readlink(path, buf, sizeof(buf));
155 	if (n < 0) {
156 		err("can't read link type: %s\n", strerror(errno));
157 		return -1;
158 	}
159 	if (n == sizeof(path)) {
160 		err("can't read link type: path too long!\n");
161 		return -1;
162 	}
163 
164 	if (strstr(buf, "bpf-map"))
165 		return BPF_OBJ_MAP;
166 	else if (strstr(buf, "bpf-prog"))
167 		return BPF_OBJ_PROG;
168 
169 	return BPF_OBJ_UNKNOWN;
170 }
171 
172 char *get_fdinfo(int fd, const char *key)
173 {
174 	char path[PATH_MAX];
175 	char *line = NULL;
176 	size_t line_n = 0;
177 	ssize_t n;
178 	FILE *fdi;
179 
180 	snprintf(path, sizeof(path), "/proc/%d/fdinfo/%d", getpid(), fd);
181 
182 	fdi = fopen(path, "r");
183 	if (!fdi) {
184 		err("can't open fdinfo: %s\n", strerror(errno));
185 		return NULL;
186 	}
187 
188 	while ((n = getline(&line, &line_n, fdi))) {
189 		char *value;
190 		int len;
191 
192 		if (!strstr(line, key))
193 			continue;
194 
195 		fclose(fdi);
196 
197 		value = strchr(line, '\t');
198 		if (!value || !value[1]) {
199 			err("malformed fdinfo!?\n");
200 			free(line);
201 			return NULL;
202 		}
203 		value++;
204 
205 		len = strlen(value);
206 		memmove(line, value, len);
207 		line[len - 1] = '\0';
208 
209 		return line;
210 	}
211 
212 	err("key '%s' not found in fdinfo\n", key);
213 	free(line);
214 	fclose(fdi);
215 	return NULL;
216 }
217