sysfs.cpp revision c4d14c17e737e0456e9db8d937a9a6475fffd0a3
/* $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>
/** @todo r=bird: This whole API should be rewritten to use IPRT status codes.
* using errno was a mistake and only results in horrible code. */
/**
* Constructs the path of a sysfs file from the format paramaters passed,
* prepending a prefix if the path is relative.
*
* @returns The number of characters returned, or -1 and errno set to ERANGE on
* failure.
*
* @param pszPrefix The prefix to prepend if the path is relative. Must end
* in '/'.
* @param pszBuf Where to write the path. Must be at least
* sizeof(@a pszPrefix) characters long
* @param cchBuf The size of the buffer pointed to by @a pszBuf.
* @param pszFormat The name format, either absolute or relative to the
* prefix specified by @a pszPrefix.
* @param va The format args.
*/
const char *pszPrefix,
{
/** @todo While RTStrPrintfV prevents overflows, it doesn't make it easy to
* check for truncations. RTPath should provide some formatters and
* joiners which can take over this rather common task that is
* performed here. */
if (*pszBuf != '/')
{
}
return cch;
}
/**
* Constructs the path of a sysfs file from the format paramaters passed,
* prepending a prefix if the path is relative.
*
* @returns The number of characters returned, or -1 and errno set to ERANGE on
* failure.
*
* @param pszPrefix The prefix to prepend if the path is relative. Must end
* in '/'.
* @param pszBuf Where to write the path. Must be at least
* sizeof(@a pszPrefix) 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 ... The format args.
*/
const char *pszPrefix,
const char *pszFormat, ...)
{
return rc;
}
/**
* Constructs the path of a sysfs file from the format paramaters passed,
* prepending "/sys/" if the path is relative.
*
* @returns The number of characters returned, or -1 and errno set to ERANGE 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.
*/
static ssize_t rtLinuxSysFsConstructPath(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va)
{
}
{
int iSavedErrno = errno;
/*
* Construct the filename and call stat.
*/
bool fRet = false;
char szFilename[RTPATH_MAX];
if (rc != -1)
{
}
errno = iSavedErrno;
return fRet;
}
{
return fRet;
}
{
/*
* Construct the filename and call open.
*/
char szFilename[RTPATH_MAX];
if (rc != -1)
return rc;
}
{
return fd;
}
{
int iSavedErrno = errno;
errno = iSavedErrno;
}
{
return cchRead;
}
{
if (fd == -1)
return -1;
char szNum[128];
if (cchNum > 0)
{
if (RT_FAILURE(rc))
{
i64Ret = -1;
}
}
else if (cchNum == 0)
return i64Ret;
}
{
return i64Ret;
}
{
if (fd == -1)
return 0;
char szNum[128];
if (cchNum > 0)
{
else
{
if ( rc != VINF_SUCCESS
&& rc != VWRN_TRAILING_CHARS
&& rc != VWRN_TRAILING_SPACES)
else
{
errno = 0;
}
}
}
else if (cchNum == 0)
return DevNum;
}
{
return DevNum;
}
RTDECL(ssize_t) RTLinuxSysFsReadStrFileV(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va)
{
if (fd == -1)
return -1;
if (cchRet > 0)
{
if (pchNewLine)
*pchNewLine = '\0';
}
return cchRet;
}
{
return cchRet;
}
RTDECL(ssize_t) RTLinuxSysFsGetLinkDestV(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va)
{
/*
* Construct the filename and read the link.
*/
char szFilename[RTPATH_MAX];
if (rc == -1)
return -1;
char szLink[RTPATH_MAX];
if (rc == -1)
return -1;
{
return -1;
}
/*
* Extract the file name component and copy it into the return buffer.
*/
if (pszName)
{
{
return -1;
}
}
else
{
*pszBuf = '\0';
cchName = 0;
}
return cchName;
}
{
return rc;
}
{
/*
* Check assumptions made by the code below.
*/
if (RT_SUCCESS(rc))
{
char szPath[RTPATH_MAX]; /** @todo 4K per recursion - can easily be optimized away by passing it along pszBasePath
and only remember the length. */
for (;;)
{
if (RT_FAILURE(rc))
{
? ENOENT
: rc == VERR_BUFFER_OVERFLOW
: EIO;
rcRet = -1;
break;
}
if (RTFS_IS_SYMLINK(Entry.Info.Attr.fMode)) /* paranoia. @todo RTDirReadEx now returns symlinks, see also #if 1 below. */
continue;
/* Do the matching. */
{
break;
}
/* Recurse into subdirectories. */
continue;
continue;
if (RT_FAILURE(rc))
{
rcRet = -1;
break;
}
#if 1 /** @todo This is a temporary hack, as RTDirReadEx in 3.0 doesn't know about symbolic links. */
continue;
#endif
break;
}
}
else
{
rcRet = -1;
}
return rcRet;
}
{
|| fMode == RTFS_TYPE_DEV_BLOCK,
if (pszSuggestion)
{
/*
* Construct the filename and read the link.
*/
char szFilename[RTPATH_MAX];
if (rc == -1)
return -1;
/*
* Check whether the caller's suggestion was right.
*/
if ( RT_SUCCESS(rc)
{
{
return -1;
}
return cchPath;
}
/* The suggestion was wrong, fall back on the brute force attack. */
}
}
const char *pszSuggestion, ...)
{
return rc;
}