]> git.proxmox.com Git - mirror_iproute2.git/blob - lib/fs.c
lib/fs: Fix format string in find_fs_mount()
[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 #include <errno.h>
24 #include <limits.h>
25
26 #include "utils.h"
27
28 #define CGROUP2_FS_NAME "cgroup2"
29
30 /* if not already mounted cgroup2 is mounted here for iproute2's use */
31 #define MNT_CGRP2_PATH "/var/run/cgroup2"
32
33 /* return mount path of first occurrence of given fstype */
34 static char *find_fs_mount(const char *fs_to_find)
35 {
36 char path[4096];
37 char fstype[128]; /* max length of any filesystem name */
38 char *mnt = NULL;
39 FILE *fp;
40
41 fp = fopen("/proc/mounts", "r");
42 if (!fp) {
43 fprintf(stderr,
44 "Failed to open mounts file: %s\n", strerror(errno));
45 return NULL;
46 }
47
48 while (fscanf(fp, "%*s %4095s %127s %*s %*d %*d\n",
49 path, fstype) == 2) {
50 if (strcmp(fstype, fs_to_find) == 0) {
51 mnt = strdup(path);
52 break;
53 }
54 }
55
56 fclose(fp);
57
58 return mnt;
59 }
60
61 /* caller needs to free string returned */
62 char *find_cgroup2_mount(void)
63 {
64 char *mnt = find_fs_mount(CGROUP2_FS_NAME);
65
66 if (mnt)
67 return mnt;
68
69 mnt = strdup(MNT_CGRP2_PATH);
70 if (!mnt) {
71 fprintf(stderr, "Failed to allocate memory for cgroup2 path\n");
72 return NULL;
73
74 }
75
76 if (make_path(mnt, 0755)) {
77 fprintf(stderr, "Failed to setup vrf cgroup2 directory\n");
78 free(mnt);
79 return NULL;
80 }
81
82 if (mount("none", mnt, CGROUP2_FS_NAME, 0, NULL)) {
83 /* EBUSY means already mounted */
84 if (errno == EBUSY)
85 goto out;
86
87 if (errno == ENODEV) {
88 fprintf(stderr,
89 "Failed to mount cgroup2. Are CGROUPS enabled in your kernel?\n");
90 } else {
91 fprintf(stderr,
92 "Failed to mount cgroup2: %s\n",
93 strerror(errno));
94 }
95 free(mnt);
96 return NULL;
97 }
98 out:
99 return mnt;
100 }
101
102 int make_path(const char *path, mode_t mode)
103 {
104 char *dir, *delim;
105 struct stat sbuf;
106 int rc = -1;
107
108 delim = dir = strdup(path);
109 if (dir == NULL) {
110 fprintf(stderr, "strdup failed copying path");
111 return -1;
112 }
113
114 /* skip '/' -- it had better exist */
115 if (*delim == '/')
116 delim++;
117
118 while (1) {
119 delim = strchr(delim, '/');
120 if (delim)
121 *delim = '\0';
122
123 if (stat(dir, &sbuf) != 0) {
124 if (errno != ENOENT) {
125 fprintf(stderr,
126 "stat failed for %s: %s\n",
127 dir, strerror(errno));
128 goto out;
129 }
130
131 if (mkdir(dir, mode) != 0) {
132 fprintf(stderr,
133 "mkdir failed for %s: %s\n",
134 dir, strerror(errno));
135 goto out;
136 }
137 }
138
139 if (delim == NULL)
140 break;
141
142 *delim = '/';
143 delim++;
144 if (*delim == '\0')
145 break;
146 }
147 rc = 0;
148 out:
149 free(dir);
150
151 return rc;
152 }
153
154 int get_command_name(const char *pid, char *comm, size_t len)
155 {
156 char path[PATH_MAX];
157 char line[128];
158 FILE *fp;
159
160 if (snprintf(path, sizeof(path),
161 "/proc/%s/status", pid) >= sizeof(path)) {
162 return -1;
163 }
164
165 fp = fopen(path, "r");
166 if (!fp)
167 return -1;
168
169 comm[0] = '\0';
170 while (fgets(line, sizeof(line), fp)) {
171 char *nl, *name;
172
173 name = strstr(line, "Name:");
174 if (!name)
175 continue;
176
177 name += 5;
178 while (isspace(*name))
179 name++;
180
181 nl = strchr(name, '\n');
182 if (nl)
183 *nl = '\0';
184
185 strncpy(comm, name, len - 1);
186 comm[len - 1] = '\0';
187 break;
188 }
189
190 fclose(fp);
191
192 return 0;
193 }