chiark / gitweb /
a2410abe69003dc40414be63161093697b13830c
[elogind.git] / libsysfs / sysfs_utils.c
1 /*
2  * syfs_utils.c
3  *
4  * System utility functions for libsysfs
5  *
6  * Copyright (C) 2003 International Business Machines, Inc.
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Lesser General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2.1 of the License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public
19  *  License along with this library; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23 #include "libsysfs.h"
24 #include "sysfs.h"
25
26 /**
27  * sysfs_get_mnt_path: Gets the mount point for specified filesystem.
28  * @fs_type: filesystem type to retrieve mount point
29  * @mnt_path: place to put the retrieved mount path
30  * @len: size of mnt_path
31  * returns 0 with success and -1 with error.
32  */
33 static int sysfs_get_fs_mnt_path(const char *fs_type, char *mnt_path, 
34                                  size_t len)
35 {
36         FILE *mnt;
37         struct mntent *mntent;
38         int ret = 0;
39         size_t dirlen = 0;
40
41         /* check arg */
42         if (fs_type == NULL || mnt_path == NULL) {
43                 errno = EINVAL;
44                 return -1;
45         }
46
47         if ((mnt = setmntent(SYSFS_PROC_MNTS, "r")) == NULL) {
48                 dprintf(stderr, "Error getting mount information\n");
49                 return -1;
50         }
51         while (ret == 0 && dirlen == 0 && (mntent = getmntent(mnt)) != NULL) {
52                 if (strcmp(mntent->mnt_type, fs_type) == 0) {
53                         dirlen = strlen(mntent->mnt_dir);
54                         if (dirlen <= (len - 1)) {
55                                 strcpy(mnt_path, mntent->mnt_dir);
56                         } else {
57                                 dprintf(stderr, 
58                                         "Error - mount path too long\n");
59                                 ret = -1;
60                         }
61                 }
62         }
63         endmntent(mnt);
64         if (dirlen == 0 && ret == 0) {
65                 dprintf(stderr, "Filesystem %s not found!\n", fs_type);
66                 errno = EINVAL;
67                 ret = -1;
68         }
69         return ret;
70 }
71
72 /*
73  * sysfs_get_mnt_path: Gets the sysfs mount point.
74  * @mnt_path: place to put "sysfs" mount point
75  * @len: size of mnt_path
76  * returns 0 with success and -1 with error.
77  */
78 int sysfs_get_mnt_path(char *mnt_path, size_t len)
79 {
80         int ret = -1;
81
82         if (mnt_path != NULL)
83                 ret = sysfs_get_fs_mnt_path(SYSFS_FSTYPE_NAME, mnt_path, len);
84         else
85                 errno = EINVAL;
86
87         return ret;
88 }
89
90 /**
91  * sysfs_get_name_from_path: returns last name from a "/" delimited path
92  * @path: path to get name from
93  * @name: where to put name
94  * @len: size of name
95  */
96 int sysfs_get_name_from_path(const char *path, char *name, size_t len)
97 {
98         char *n = NULL;
99                                                                                 
100         if (path == NULL || name == NULL) {
101                 errno = EINVAL;
102                 return -1;
103         }
104         n = strrchr(path, '/');
105         if (n == NULL) {
106                 errno = EINVAL;
107                 return -1;
108         }
109         n++;
110         strncpy(name, n, len);
111
112         return 0;
113 }
114
115 /**
116  * sysfs_get_link: returns link source
117  * @path: symbolic link's path
118  * @target: where to put name
119  * @len: size of name
120  */
121 int sysfs_get_link(const char *path, char *target, size_t len)
122 {
123         char devdir[SYSFS_PATH_MAX];
124         char linkpath[SYSFS_PATH_MAX];
125         char *d = NULL;
126
127         if (path == NULL || target == NULL) {
128                 errno = EINVAL;
129                 return -1;
130         }
131
132         memset(devdir, 0, SYSFS_PATH_MAX);
133         memset(linkpath, 0, SYSFS_PATH_MAX);
134
135         if ((sysfs_get_mnt_path(devdir, SYSFS_PATH_MAX)) != 0) {
136                 dprintf(stderr, "Sysfs not supported on this system\n");
137                 return -1;
138         }
139                                                                         
140         if ((readlink(path, linkpath, SYSFS_PATH_MAX)) < 0) {
141                 return -1;
142         }
143                                                                                 
144         d = linkpath;
145
146         /* getting rid of leading "../.." */    
147         while (*d == '/' || *d == '.')
148                 d++;
149
150         d--;
151         
152         strcat(devdir, d);
153         strncpy(target, devdir, len);
154
155         return 0;
156 }