]> git.proxmox.com Git - mirror_iproute2.git/blob - lib/fs.c
Use libbsd for strlcpy if available
[mirror_iproute2.git] / lib / fs.c
1 /*
2 * fs.c filesystem APIs
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: David Ahern <dsa@cumulusnetworks.com>
10 *
11 */
12
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <sys/socket.h>
16 #include <sys/mount.h>
17 #include <ctype.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #ifdef HAVE_LIBBSD
24 #include <bsd/string.h>
25 #endif
26 #include <errno.h>
27 #include <limits.h>
28
29 #include "utils.h"
30
31 #define CGROUP2_FS_NAME "cgroup2"
32
33 /* if not already mounted cgroup2 is mounted here for iproute2's use */
34 #define MNT_CGRP2_PATH "/var/run/cgroup2"
35
36 /* return mount path of first occurrence of given fstype */
37 static char *find_fs_mount(const char *fs_to_find)
38 {
39 char path[4096];
40 char fstype[128]; /* max length of any filesystem name */
41 char *mnt = NULL;
42 FILE *fp;
43
44 fp = fopen("/proc/mounts", "r");
45 if (!fp) {
46 fprintf(stderr,
47 "Failed to open mounts file: %s\n", strerror(errno));
48 return NULL;
49 }
50
51 while (fscanf(fp, "%*s %4095s %127s %*s %*d %*d\n",
52 path, fstype) == 2) {
53 if (strcmp(fstype, fs_to_find) == 0) {
54 mnt = strdup(path);
55 break;
56 }
57 }
58
59 fclose(fp);
60
61 return mnt;
62 }
63
64 /* caller needs to free string returned */
65 char *find_cgroup2_mount(void)
66 {
67 char *mnt = find_fs_mount(CGROUP2_FS_NAME);
68
69 if (mnt)
70 return mnt;
71
72 mnt = strdup(MNT_CGRP2_PATH);
73 if (!mnt) {
74 fprintf(stderr, "Failed to allocate memory for cgroup2 path\n");
75 return NULL;
76
77 }
78
79 if (make_path(mnt, 0755)) {
80 fprintf(stderr, "Failed to setup vrf cgroup2 directory\n");
81 free(mnt);
82 return NULL;
83 }
84
85 if (mount("none", mnt, CGROUP2_FS_NAME, 0, NULL)) {
86 /* EBUSY means already mounted */
87 if (errno == EBUSY)
88 goto out;
89
90 if (errno == ENODEV) {
91 fprintf(stderr,
92 "Failed to mount cgroup2. Are CGROUPS enabled in your kernel?\n");
93 } else {
94 fprintf(stderr,
95 "Failed to mount cgroup2: %s\n",
96 strerror(errno));
97 }
98 free(mnt);
99 return NULL;
100 }
101 out:
102 return mnt;
103 }
104
105 int make_path(const char *path, mode_t mode)
106 {
107 char *dir, *delim;
108 int rc = -1;
109
110 delim = dir = strdup(path);
111 if (dir == NULL) {
112 fprintf(stderr, "strdup failed copying path");
113 return -1;
114 }
115
116 /* skip '/' -- it had better exist */
117 if (*delim == '/')
118 delim++;
119
120 while (1) {
121 delim = strchr(delim, '/');
122 if (delim)
123 *delim = '\0';
124
125 rc = mkdir(dir, mode);
126 if (mkdir(dir, mode) != 0 && errno != EEXIST) {
127 fprintf(stderr, "mkdir failed for %s: %s\n",
128 dir, strerror(errno));
129 goto out;
130 }
131
132 if (delim == NULL)
133 break;
134
135 *delim = '/';
136 delim++;
137 if (*delim == '\0')
138 break;
139 }
140 rc = 0;
141 out:
142 free(dir);
143
144 return rc;
145 }
146
147 int get_command_name(const char *pid, char *comm, size_t len)
148 {
149 char path[PATH_MAX];
150 char line[128];
151 FILE *fp;
152
153 if (snprintf(path, sizeof(path),
154 "/proc/%s/status", pid) >= sizeof(path)) {
155 return -1;
156 }
157
158 fp = fopen(path, "r");
159 if (!fp)
160 return -1;
161
162 comm[0] = '\0';
163 while (fgets(line, sizeof(line), fp)) {
164 char *nl, *name;
165
166 name = strstr(line, "Name:");
167 if (!name)
168 continue;
169
170 name += 5;
171 while (isspace(*name))
172 name++;
173
174 nl = strchr(name, '\n');
175 if (nl)
176 *nl = '\0';
177
178 strlcpy(comm, name, len);
179 break;
180 }
181
182 fclose(fp);
183
184 return 0;
185 }