1219820Sjeff/*
2219820Sjeff * Copyright (c) 2006 Cisco Systems, Inc.  All rights reserved.
3219820Sjeff *
4219820Sjeff * This software is available to you under a choice of one of two
5219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
6219820Sjeff * General Public License (GPL) Version 2, available from the file
7219820Sjeff * COPYING in the main directory of this source tree, or the
8219820Sjeff * OpenIB.org BSD license below:
9219820Sjeff *
10219820Sjeff *     Redistribution and use in source and binary forms, with or
11219820Sjeff *     without modification, are permitted provided that the following
12219820Sjeff *     conditions are met:
13219820Sjeff *
14219820Sjeff *      - Redistributions of source code must retain the above
15219820Sjeff *        copyright notice, this list of conditions and the following
16219820Sjeff *        disclaimer.
17219820Sjeff *
18219820Sjeff *      - Redistributions in binary form must reproduce the above
19219820Sjeff *        copyright notice, this list of conditions and the following
20219820Sjeff *        disclaimer in the documentation and/or other materials
21219820Sjeff *        provided with the distribution.
22219820Sjeff *
23219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30219820Sjeff * SOFTWARE.
31219820Sjeff */
32219820Sjeff
33219820Sjeff#if HAVE_CONFIG_H
34219820Sjeff#  include <config.h>
35219820Sjeff#endif /* HAVE_CONFIG_H */
36219820Sjeff
37219820Sjeff#include <stdio.h>
38219820Sjeff#include <stdlib.h>
39219820Sjeff#include <unistd.h>
40219820Sjeff#include <sys/types.h>
41219820Sjeff#include <sys/stat.h>
42219820Sjeff#include <fcntl.h>
43219820Sjeff#include <string.h>
44219820Sjeff
45219820Sjeff#include <sys/sysctl.h>
46219820Sjeff
47219820Sjeff#include "ibverbs.h"
48219820Sjeff
49219820Sjeffstatic char *sysfs_path;
50219820Sjeff
51219820Sjeffconst char *ibv_get_sysfs_path(void)
52219820Sjeff{
53219820Sjeff	char *env = NULL;
54219820Sjeff
55219820Sjeff	if (sysfs_path)
56219820Sjeff		return sysfs_path;
57219820Sjeff
58219820Sjeff	/*
59219820Sjeff	 * Only follow use path passed in through the calling user's
60219820Sjeff	 * environment if we're not running SUID.
61219820Sjeff	 */
62219820Sjeff	if (getuid() == geteuid())
63219820Sjeff		env = getenv("SYSFS_PATH");
64219820Sjeff
65219820Sjeff	if (env) {
66219820Sjeff		int len;
67219820Sjeff
68219820Sjeff		sysfs_path = strndup(env, IBV_SYSFS_PATH_MAX);
69219820Sjeff		len = strlen(sysfs_path);
70219820Sjeff		while (len > 0 && sysfs_path[len - 1] == '/') {
71219820Sjeff			--len;
72219820Sjeff			sysfs_path[len] = '\0';
73219820Sjeff		}
74219820Sjeff	} else
75219820Sjeff		sysfs_path = "/sys";
76219820Sjeff
77219820Sjeff	return sysfs_path;
78219820Sjeff}
79219820Sjeff
80219820Sjeffint ibv_read_sysfs_file(const char *dir, const char *file,
81219820Sjeff			char *buf, size_t size)
82219820Sjeff{
83219820Sjeff	char *path, *s;
84219820Sjeff	int fd;
85219820Sjeff	size_t len;
86219820Sjeff
87219820Sjeff	if (asprintf(&path, "%s/%s", dir, file) < 0)
88219820Sjeff		return -1;
89219820Sjeff
90219820Sjeff	for (s = &path[0]; *s != '\0'; s++)
91219820Sjeff		if (*s == '/')
92219820Sjeff			*s = '.';
93219820Sjeff
94219820Sjeff        len = size;
95219820Sjeff        if (sysctlbyname(&path[1], buf, &len, NULL, 0) == -1)
96219820Sjeff		return -1;
97219820Sjeff
98219820Sjeff	free(path);
99219820Sjeff
100219820Sjeff	if (len > 0 && buf[len - 1] == '\n')
101219820Sjeff		buf[--len] = '\0';
102219820Sjeff
103219820Sjeff	return len;
104219820Sjeff}
105