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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
/*
* libuio - UserspaceIO helper library
*
* Copyright (C) 2011 Benedikt Spranger
* based on libUIO by Hans J. Koch
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include "libuio_internal.h"
/**
* @defgroup libuio_helper generic libuio helper functions
* @ingroup libuio
* @brief helper functions
* @{
*/
/**
* read a line from a file
* @param filename file name
* @returns first line or NULL on failure
*/
char *first_line_from_file (char *filename)
{
char c, *out;
int fd, len;
fd = open (filename, O_RDONLY);
if (fd < 0)
{
g_warning (_("open: %s: %s"), filename, g_strerror (errno));
return NULL;
}
for (len = 0; ((read (fd, &c, 1) == 1) && (c != '\n')); len++);
lseek (fd, 0, SEEK_SET);
out = malloc (len + 1);
if (!out)
{
errno = ENOMEM;
g_warning (_("malloc: %s"), g_strerror (errno));
goto out;
}
len = read (fd, out, len);
if (len < 0)
{
g_warning (_("read: %s"), g_strerror (errno));
free (out);
out = NULL;
goto out;
}
out [len] = 0;
out:
close (fd);
return out;
}
/**
* read device id from file
* @param filename file name
* @returns device id or 0 on failure
*/
dev_t devid_from_file (char *filename)
{
FILE *fhan;
int major, minor;
dev_t devid = 0;
fhan = fopen (filename, "r");
if (!fhan)
{
g_warning (_("fopen: %s"), g_strerror (errno));
goto out;
}
if (fscanf (fhan, "%d:%d", &major, &minor) == 2)
devid = makedev (major, minor);
out:
if (fhan)
fclose (fhan);
return devid;
}
/**
* read a line from a file
* @param dir map directory
* @param maxmap available maps
* @returns maps or NULL on failure/no maps
*/
static struct uio_map_t *scan_maps (char *dir, int *maxmap)
{
struct uio_map_t *map = NULL;
struct dirent **namelist;
char *tmp, name [PATH_MAX];
int nr, i, t = 0;
int spfret;
*maxmap = 0;
nr = scandir (dir, &namelist, 0, alphasort);
if (nr < 0)
return NULL;
map = calloc (nr, sizeof (struct uio_map_t));
if (!map)
{
errno = ENOMEM;
g_warning (_("calloc: %s"), g_strerror (errno));
goto err_nomem1;
}
for (i = 0; i < nr; i++)
{
if (!strcmp (namelist [i]->d_name, ".") ||
!strcmp (namelist [i]->d_name, ".."))
{
free (namelist [i]);
namelist [i] = NULL;
continue;
}
spfret = snprintf (name, sizeof (name), "%s/%s/addr",
dir, namelist [i]->d_name);
if (spfret < 0 || spfret >= (int)sizeof (name))
goto err_inv_name;
tmp = first_line_from_file (name);
map [t].addr = strtoul (tmp, NULL, 0);
free (tmp);
spfret = snprintf (name, sizeof (name), "%s/%s/name",
dir, namelist [i]->d_name);
if (spfret < 0 || spfret >= (int)sizeof (name))
goto err_inv_name;
map [t].name = first_line_from_file (name);
spfret = snprintf (name, sizeof (name), "%s/%s/size",
dir, namelist [i]->d_name);
if (spfret < 0 || spfret >= (int)sizeof (name))
goto err_inv_name;
tmp = first_line_from_file (name);
map [t].size = strtoul (tmp, NULL, 0);
free (tmp);
map [t].offset = map [t].addr & (getpagesize () - 1);
map [t].map = MAP_FAILED;
*maxmap = ++t;
free (namelist [i]);
namelist [i] = NULL;
}
free (namelist);
return map;
err_inv_name:
g_warning (_("invalid name"));
free(map);
err_nomem1:
for (i = 0; i < nr; i++)
{
if (namelist [i])
{
free (namelist [i]);
}
}
free (namelist);
return NULL;
}
/**
* search device node name by major/minor
* @param dir start in directory dir
* @param devid major/minor
* @param devname first matching device node name
* @returns -1 on error, 0 on not found and 1 on success
*/
static int search_major_minor (const char *dir, dev_t devid, char **devname)
{
struct dirent **namelist;
struct stat stat;
char name [PATH_MAX];
int i, nr, ret = 0;
if (!devname)
{
errno = EINVAL;
g_warning (_("search_major_minor: %s"), g_strerror (errno));
return -1;
}
nr = scandir (dir, &namelist, 0, alphasort);
if (nr < 0)
{
g_warning (_("scandir: %s"), g_strerror (errno));
return nr;
}
for (i = 0; i < nr; i++)
{
if (!strcmp (namelist [i]->d_name, ".") ||
!strcmp (namelist [i]->d_name, ".."))
continue;
snprintf (name, sizeof (name), "%s/%s",
dir, namelist [i]->d_name);
ret = lstat (name, &stat);
if (ret < 0)
{
g_warning (_("lstat: %s"), g_strerror (errno));
goto out;
}
if (S_ISDIR (stat.st_mode))
{
ret = search_major_minor (name, devid, devname);
if (ret != 0)
goto out;
}
if (S_ISCHR (stat.st_mode))
{
if (stat.st_rdev != devid)
continue;
*devname = strdup (name);
if (!*devname)
{
errno = ENOMEM;
g_warning (_("strdup: %s"), g_strerror (errno));
ret = -1;
}
else
ret = 1;
goto out;
}
}
out:
for (i = 0; i < nr; i++)
free (namelist [i]);
free (namelist);
return ret;
}
/**
* create UIO device info struct
* @param dir sysfs directory
* @param name uio device entry
* @returns UIO device info struct or NULL on failure
*/
struct uio_info_t *create_uio_info (char *dir, char *name)
{
struct uio_info_t *info;
char filename [PATH_MAX];
info = calloc (1, sizeof (struct uio_info_t));
if (!info)
return NULL;
snprintf (filename, PATH_MAX, "%s/%s", dir, name);
info->path = strdup (filename);
snprintf (filename, PATH_MAX, "%s/%s/name", dir, name);
info->name = first_line_from_file (filename);
snprintf (filename, PATH_MAX, "%s/%s/version", dir, name);
info->version = first_line_from_file (filename);
snprintf (filename, PATH_MAX, "%s/%s/dev", dir, name);
info->devid = devid_from_file (filename);
search_major_minor ("/dev", info->devid, &info->devname);
snprintf (filename, PATH_MAX, "%s/%s/maps", dir, name);
info->maps = scan_maps (filename, &info->maxmap);
info->fd = -1;
return info;
}
/** @} */
|