File: igt_configfs.c

package info (click to toggle)
intel-gpu-tools 2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 63,360 kB
  • sloc: xml: 781,458; ansic: 360,567; python: 8,336; yacc: 2,781; perl: 1,196; sh: 1,177; lex: 487; asm: 227; lisp: 35; makefile: 30
file content (86 lines) | stat: -rw-r--r-- 1,780 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
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2023 Google LLC.
 * Copyright © 2023 Collabora, Ltd.
 * Copyright © 2024 Red Hat, Inc.
 * Copyright © 2025 Intel Corporation
 */

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/mount.h>

#include "igt_aux.h"
#include "igt_configfs.h"

/**
 * SECTION:igt_configfs
 * @short_description: Support code for configfs features
 * @title: configfs
 * @include: igt_configfs.h
 *
 * This library provides helpers to access configfs features.
 */

/*
 * General configfs helpers
 */

static const char *__igt_configfs_mount(void)
{
	if (igt_is_mountpoint("/sys/kernel/config"))
		return "/sys/kernel/config";

	if (igt_is_mountpoint("/config"))
		return "/config";

	if (mount("config", "/sys/kernel/config", "configfs", 0, 0))
		return NULL;

	return "/sys/kernel/config";
}

/**
 * igt_configfs_mount:
 *
 * This attempts to locate where configfs is mounted on the filesystem,
 * and if not found, will then try to mount configfs at /sys/kernel/config.
 *
 * Returns:
 * The path to the configfs mount point (e.g. /sys/kernel/config)
 */
const char *igt_configfs_mount(void)
{
	static const char *path;

	if (!path)
		path = __igt_configfs_mount();

	return path;
}

/**
 * igt_configfs_open: open configfs path
 * @name: name of the configfs directory
 *
 * Opens the configfs directory corresponding to the name
 *
 * Returns:
 * The directory fd, or -1 on failure.
 */
int igt_configfs_open(const char *name)
{
	char path[PATH_MAX];
	const char *configfs_path;

	configfs_path = igt_configfs_mount();
	if (!configfs_path) {
		igt_debug("configfs not mounted, errno=%d\n", errno);
		return -1;
	}

	snprintf(path, sizeof(path), "%s/%s", configfs_path, name);

	return open(path, O_DIRECTORY | O_RDONLY);
}