sysfs.cpp revision 70d78cc23a9585983d71fec4e7df3dce0de2b713
/* $Id$ */
/** @file
* IPRT - Linux sysfs access.
*/
/*
* Copyright (C) 2006-2008 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_SYSTEM
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
/**
* Constructs the path of a sysfs file from the format paramaters passed,
* prepending "/sys/" if the path is relative.
*
* @returns The number of characters written, or -1 and errno on failure.
* @param pszBuf Where to write the path. Must be at least
* sizeof("/sys/") characters long
* @param cchBuf The size of the buffer pointed to by @a pszBuf
* @param pszFormat The name format, either absolute or relative to "/sys/".
* @param va The format args.
*/
{
char szFilename[RTPATH_MAX];
const char szPrefix[] = "/sys/";
if (szFilename[0] == '/')
*pszBuf = '\0';
else
}
/**
* Checks if a sysfs file (or directory, device, symlink, whatever) exists.
*
* @returns true / false, errno is preserved.
* @param pszFormat The name format, either absolute or relative to "/sys/".
* @param va The format args.
*/
{
int iSavedErrno = errno;
/*
* Construct the filename and call stat.
*/
char szFilename[RTPATH_MAX];
bool fRet = false;
{
}
errno = iSavedErrno;
return fRet;
}
/**
* Checks if a sysfs file (or directory, device, symlink, whatever) exists.
*
* @returns true / false, errno is preserved.
* @param pszFormat The name format, either absolute or relative to "/sys/".
* @param ... The format args.
*/
bool RTLinuxSysFsExists(const char *pszFormat, ...)
{
return fRet;
}
/**
* Opens a sysfs file.
*
* @returns The file descriptor. -1 and errno on failure.
* @param pszFormat The name format, either absolute or relative to "/sys/".
* @param va The format args.
*/
{
/*
* Construct the filename and call open.
*/
char szFilename[RTPATH_MAX];
return rc;
}
/**
* Opens a sysfs file.
*
* @returns The file descriptor. -1 and errno on failure.
* @param pszFormat The name format, either absolute or relative to "/sys/".
* @param ... The format args.
*/
int RTLinuxSysFsOpen(const char *pszFormat, ...)
{
return fd;
}
/**
* Closes a file opened with RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
*
* @param fd
*/
void RTLinuxSysFsClose(int fd)
{
int iSavedErrno = errno;
errno = iSavedErrno;
}
/**
* Reads a string from a file opened with RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
*
* @returns The number of bytes read. -1 and errno on failure.
* @param fd The file descriptor returned by RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
* @param pszBuf Where to store the string.
* @param cchBuf The size of the buffer. Must be at least 2 bytes.
*/
{
return cchRead;
}
/**
* Reads a number from a sysfs file.
*
* @returns 64-bit signed value on success, -1 and errno on failure.
* @param uBase The number base, 0 for autodetect.
* @param pszFormat The filename format, either absolute or relative to "/sys/".
* @param va Format args.
*/
{
if (fd == -1)
return -1;
char szNum[128];
if (cchNum > 0)
{
if (RT_FAILURE(rc))
{
i64Ret = -1;
}
}
else if (cchNum == 0)
return i64Ret;
}
/**
* Reads a number from a sysfs file.
*
* @returns 64-bit signed value on success, -1 and errno on failure.
* @param uBase The number base, 0 for autodetect.
* @param pszFormat The filename format, either absolute or relative to "/sys/".
* @param ... Format args.
*/
{
return i64Ret;
}
/**
* Reads a string from a sysfs file. If the file contains a newline, we only
* return the text up until there.
*
* @returns number of characters read on success, -1 and errno on failure.
* @param pszBuf Where to store the path element. Must be at least two
* characters, but a longer buffer would be advisable.
* @param cchBuf The size of the buffer pointed to by @a pszBuf.
* @param pszFormat The filename format, either absolute or relative to "/sys/".
* @param va Format args.
*/
{
if (fd == -1)
return -1;
char *pchNewLine = NULL;
if (cchRet > 0)
if (pchNewLine != NULL)
*pchNewLine = '\0';
return cchRet;
}
/**
* Reads a string from a sysfs file. If the file contains a newline, we only
* return the text up until there.
*
* @returns number of characters read on success, -1 and errno on failure.
* @param pszBuf Where to store the path element. Must be at least two
* characters, but a longer buffer would be advisable.
* @param cchBuf The size of the buffer pointed to by @a pszBuf.
* @param pszFormat The filename format, either absolute or relative to "/sys/".
* @param ... Format args.
*/
{
return cchRet;
}
/**
* Reads the last element of the path of the file pointed to by the symbolic
* link specified. This is needed at least to get the name of the driver
* associated with a device, where pszFormat should be the "driver" link in the
* devices sysfs directory.
*
* @returns The number of characters written on success, -1 and errno on failure.
* @param pszBuf Where to store the path element. Must be at least two
* characters, but a longer buffer would be advisable.
* @param cchBuf The size of the buffer pointed to by @a pszBuf.
* @param pszFormat The filename format, either absolute or relative to "/sys/".
* @param ... Format args.
*/
{
if (cchBuf < 2)
if (rc == sizeof(szFilename))
{
rc = -1;
}
if (rc > 0)
{
rc = -1;
}
if (rc >= 0)
{
* starting with '/'. */
*pszBuf = '\0';
}
return rc;
}