1321936Shselasky/* 2321936Shselasky * Copyright (c) 2006 Cisco Systems, Inc. All rights reserved. 3321936Shselasky * 4321936Shselasky * This software is available to you under a choice of one of two 5321936Shselasky * licenses. You may choose to be licensed under the terms of the GNU 6321936Shselasky * General Public License (GPL) Version 2, available from the file 7321936Shselasky * COPYING in the main directory of this source tree, or the 8321936Shselasky * OpenIB.org BSD license below: 9321936Shselasky * 10321936Shselasky * Redistribution and use in source and binary forms, with or 11321936Shselasky * without modification, are permitted provided that the following 12321936Shselasky * conditions are met: 13321936Shselasky * 14321936Shselasky * - Redistributions of source code must retain the above 15321936Shselasky * copyright notice, this list of conditions and the following 16321936Shselasky * disclaimer. 17321936Shselasky * 18321936Shselasky * - Redistributions in binary form must reproduce the above 19321936Shselasky * copyright notice, this list of conditions and the following 20321936Shselasky * disclaimer in the documentation and/or other materials 21321936Shselasky * provided with the distribution. 22321936Shselasky * 23321936Shselasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24321936Shselasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25321936Shselasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26321936Shselasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27321936Shselasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28321936Shselasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29321936Shselasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30321936Shselasky * SOFTWARE. 31321936Shselasky */ 32321936Shselasky#define _GNU_SOURCE 33321936Shselasky#include <config.h> 34321936Shselasky 35321936Shselasky#include <stdio.h> 36321936Shselasky#include <stdlib.h> 37321936Shselasky#include <unistd.h> 38321936Shselasky#include <sys/types.h> 39321936Shselasky#include <sys/stat.h> 40321936Shselasky#include <fcntl.h> 41321936Shselasky#include <string.h> 42321936Shselasky#include <sys/sysctl.h> 43321936Shselasky 44321936Shselasky#include "ibverbs.h" 45321936Shselasky 46321936Shselaskystatic const char *sysfs_path; 47321936Shselasky 48321936Shselaskyconst char *ibv_get_sysfs_path(void) 49321936Shselasky{ 50321936Shselasky const char *env = NULL; 51321936Shselasky 52321936Shselasky if (sysfs_path) 53321936Shselasky return sysfs_path; 54321936Shselasky 55321936Shselasky /* 56321936Shselasky * Only follow use path passed in through the calling user's 57321936Shselasky * environment if we're not running SUID. 58321936Shselasky */ 59321936Shselasky if (getuid() == geteuid()) 60321936Shselasky env = getenv("SYSFS_PATH"); 61321936Shselasky 62321936Shselasky if (env) { 63321936Shselasky int len; 64321936Shselasky char *dup; 65321936Shselasky 66321936Shselasky sysfs_path = dup = strndup(env, IBV_SYSFS_PATH_MAX); 67321936Shselasky len = strlen(dup); 68321936Shselasky while (len > 0 && dup[len - 1] == '/') { 69321936Shselasky --len; 70321936Shselasky dup[len] = '\0'; 71321936Shselasky } 72321936Shselasky } else 73321936Shselasky sysfs_path = "/sys"; 74321936Shselasky 75321936Shselasky return sysfs_path; 76321936Shselasky} 77321936Shselasky 78321936Shselaskyint ibv_read_sysfs_file(const char *dir, const char *file, 79321936Shselasky char *buf, size_t size) 80321936Shselasky{ 81321936Shselasky char *path, *s; 82341895Shselasky int ret; 83321936Shselasky size_t len; 84321936Shselasky 85321936Shselasky if (asprintf(&path, "%s/%s", dir, file) < 0) 86321936Shselasky return -1; 87321936Shselasky 88321936Shselasky for (s = &path[0]; *s != '\0'; s++) 89321936Shselasky if (*s == '/') 90321936Shselasky *s = '.'; 91321936Shselasky 92341895Shselasky len = size; 93341895Shselasky ret = sysctlbyname(&path[1], buf, &len, NULL, 0); 94341895Shselasky free(path); 95341895Shselasky 96341895Shselasky if (ret == -1) 97321936Shselasky return -1; 98321936Shselasky 99321936Shselasky if (len > 0 && buf[len - 1] == '\n') 100321936Shselasky buf[--len] = '\0'; 101321936Shselasky 102321936Shselasky return len; 103321936Shselasky} 104