Write `content` to " / ". Creates parent subdir if needed. * Returns 0 on success, -1 on any failure. */
| 145 | /* Write `content` to "<root>/<relpath>". Creates parent subdir if needed. |
| 146 | * Returns 0 on success, -1 on any failure. */ |
| 147 | static int cgroup_test_write(const char *root, const char *relpath, const char *content) { |
| 148 | char path[1024]; |
| 149 | const char *slash = strchr(relpath, '/'); |
| 150 | if (slash != NULL) { |
| 151 | char subdir[1024]; |
| 152 | size_t n = (size_t)(slash - relpath); |
| 153 | if (n >= sizeof(subdir)) { |
| 154 | return -1; |
| 155 | } |
| 156 | memcpy(subdir, relpath, n); |
| 157 | subdir[n] = '\0'; |
| 158 | snprintf(path, sizeof(path), "%s/%s", root, subdir); |
| 159 | (void)mkdir(path, S_IRWXU); |
| 160 | } |
| 161 | snprintf(path, sizeof(path), "%s/%s", root, relpath); |
| 162 | FILE *fp = fopen(path, "we"); |
| 163 | if (fp == NULL) { |
| 164 | return -1; |
| 165 | } |
| 166 | size_t n = strlen(content); |
| 167 | int rc = (fwrite(content, 1, n, fp) == n) ? 0 : -1; |
| 168 | fclose(fp); |
| 169 | return rc; |
| 170 | } |
| 171 | |
| 172 | /* Recursively remove a tmp dir created by cgroup_test_setup. Best-effort. |
| 173 | * Uses opendir/unlink/rmdir rather than system("rm -rf ...") to avoid |