forked from jbenc/plotnetcfg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsysfs.c
86 lines (69 loc) · 1.73 KB
/
sysfs.c
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
/*
* This file is a part of plotnetcfg, a tool to visualize network config.
* Copyright (C) 2015 Red Hat, Inc. -- Ondrej Hlavaty <ohlavaty@redhat.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include "sysfs.h"
#define PATH "/tmp/plotnetcfg-sys-XXXXXX"
#define LEN sizeof(PATH)
static char sysfs_mountpoint[LEN];
void sysfs_clean()
{
struct stat st;
if (stat(sysfs_mountpoint, &st))
return;
if (S_ISDIR(st.st_mode))
sysfs_umount();
rmdir(sysfs_mountpoint);
}
int sysfs_init()
{
strcpy(sysfs_mountpoint, PATH);
if (!mkdtemp(sysfs_mountpoint))
return errno;
atexit(sysfs_clean);
return 0;
}
int sysfs_mount(const char *name)
{
sysfs_umount();
if (mount(name, sysfs_mountpoint, "sysfs", 0, NULL) < 0)
return errno;
return 0;
}
void sysfs_umount()
{
umount2(sysfs_mountpoint, MNT_DETACH);
}
char *sysfs_realpath(const char *sys_path)
{
char *resolved;
char *path;
if (asprintf(&path, "%s/%s", sysfs_mountpoint, sys_path) < 0)
return NULL;
resolved = realpath(path, NULL);
free(path);
return resolved ? resolved + LEN : NULL;
}
void sysfs_free(char *path)
{
free(path - LEN);
}