sysfs.cpp revision 4d0d9097df43f55a27ac86bdd587e8c217493e62
/* $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 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 va The format args.
*/
const char *pszPrefix,
{
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;
}
const char *pszBasePath,
{
RTDIRENTRYEX entry = { {0} };
int vrc = VINF_SUCCESS;
if (RT_SUCCESS(vrc))
while (true)
{
if (RT_FAILURE(vrc))
{
: EIO;
rc = -1;
break;
}
continue;
&& ( ( (fMode == RTFS_TYPE_DEV_CHAR)
|| ( (fMode == RTFS_TYPE_DEV_BLOCK)
{
break;
}
continue;
continue;
char szPath[RTPATH_MAX];
/** @todo this is a temporary hack, as RTDirReadEx currently
* doesn't know about symbolic links */
if (rc < 0)
break;
continue;
/* @todo ends here */
if (rc < 0)
break;
break;
}
else
{
rc = -1;
}
return rc;
}
{
|| (fMode == RTFS_TYPE_DEV_BLOCK),
if (!pszSuggestion)
/*
* Construct the filename and read the link.
*/
char szFilename[RTPATH_MAX];
pszSuggestion, va);
if (rc == -1)
return -1;
/*
* Check whether the caller's suggestion was right.
*/
/** @todo Should we just be using POSIX stat here? */
RTFSOBJINFO info = {0};
if (RT_FAILURE(vrc))
{
: EIO;
return -1;
}
&& ( ( (fMode == RTFS_TYPE_DEV_CHAR)
|| ( (fMode == RTFS_TYPE_DEV_BLOCK)
{
{
return -1;
}
return cchPath;
}
/* If the suggestion was wrong, try the brute force method */
}
const char *pszSuggestion, ...)
{
pszSuggestion, va);
return rc;
}