* \author Felix Kuehling
*/
+#include <limits.h>
#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <expat.h>
#include <math.h>
#include <unistd.h>
#include <errno.h>
+#include <dirent.h>
+#include <fnmatch.h>
+#include <regex.h>
+#include "strndup.h"
#include "xmlconfig.h"
+#include "u_process.h"
+#include "os_file.h"
-#undef GET_PROGRAM_NAME
-
-#if (defined(__GNU_LIBRARY__) || defined(__GLIBC__)) && !defined(__UCLIBC__)
-# if !defined(__GLIBC__) || (__GLIBC__ < 2)
-/* These aren't declared in any libc5 header */
-extern char *program_invocation_name, *program_invocation_short_name;
-# endif
-# define GET_PROGRAM_NAME() program_invocation_short_name
-#elif defined(__CYGWIN__)
-# define GET_PROGRAM_NAME() program_invocation_short_name
-#elif defined(__FreeBSD__) && (__FreeBSD__ >= 2)
-# include <osreldate.h>
-# if (__FreeBSD_version >= 440000)
-# include <stdlib.h>
-# define GET_PROGRAM_NAME() getprogname()
-# endif
-#elif defined(__NetBSD__) && defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 106000100)
-# include <stdlib.h>
-# define GET_PROGRAM_NAME() getprogname()
-#elif defined(__DragonFly__)
-# include <stdlib.h>
-# define GET_PROGRAM_NAME() getprogname()
-#elif defined(__APPLE__)
-# include <stdlib.h>
-# define GET_PROGRAM_NAME() getprogname()
-#elif defined(__sun)
-/* Solaris has getexecname() which returns the full path - return just
- the basename to match BSD getprogname() */
-# include <stdlib.h>
-# include <libgen.h>
-
-static const char *
-__getProgramName()
-{
- static const char *progname;
-
- if (progname == NULL) {
- const char *e = getexecname();
- if (e != NULL) {
- /* Have to make a copy since getexecname can return a readonly
- string, but basename expects to be able to modify its arg. */
- char *n = strdup(e);
- if (n != NULL) {
- progname = basename(n);
- }
- }
- }
- return progname;
-}
-
-# define GET_PROGRAM_NAME() __getProgramName()
+/* For systems like Hurd */
+#ifndef PATH_MAX
+#define PATH_MAX 4096
#endif
-#if !defined(GET_PROGRAM_NAME)
-# if defined(__OpenBSD__) || defined(NetBSD) || defined(__UCLIBC__) || defined(ANDROID)
-/* This is a hack. It's said to work on OpenBSD, NetBSD and GNU.
- * Rogelio M.Serrano Jr. reported it's also working with UCLIBC. It's
- * used as a last resort, if there is no documented facility available. */
-static const char *
-__getProgramName()
+static bool
+be_verbose(void)
{
- extern const char *__progname;
- char * arg = strrchr(__progname, '/');
- if (arg)
- return arg+1;
- else
- return __progname;
+ const char *s = getenv("MESA_DEBUG");
+ if (!s)
+ return true;
+
+ return strstr(s, "silent") == NULL;
}
-# define GET_PROGRAM_NAME() __getProgramName()
-# else
-# define GET_PROGRAM_NAME() ""
-# warning "Per application configuration won't work with your OS version."
-# endif
-#endif
/** \brief Find an option in an option cache with the name as key */
static uint32_t
(int) XML_GetCurrentLineNumber(data->parser), \
(int) XML_GetCurrentColumnNumber(data->parser)); \
} while (0)
-#define XML_WARNING(msg,args...) do { \
+#define XML_WARNING(msg, ...) do { \
__driUtilMessage ("Warning in %s line %d, column %d: "msg, data->name, \
(int) XML_GetCurrentLineNumber(data->parser), \
(int) XML_GetCurrentColumnNumber(data->parser), \
- args); \
+ ##__VA_ARGS__); \
} while (0)
/** \brief Output an error message. */
#define XML_ERROR1(msg) do { \
(int) XML_GetCurrentLineNumber(data->parser), \
(int) XML_GetCurrentColumnNumber(data->parser)); \
} while (0)
-#define XML_ERROR(msg,args...) do { \
+#define XML_ERROR(msg, ...) do { \
__driUtilMessage ("Error in %s line %d, column %d: "msg, data->name, \
(int) XML_GetCurrentLineNumber(data->parser), \
(int) XML_GetCurrentColumnNumber(data->parser), \
- args); \
+ ##__VA_ARGS__); \
} while (0)
/** \brief Output a fatal error message and abort. */
#define XML_FATAL1(msg) do { \
(int) XML_GetCurrentColumnNumber(data->parser)); \
abort();\
} while (0)
-#define XML_FATAL(msg,args...) do { \
+#define XML_FATAL(msg, ...) do { \
fprintf (stderr, "Fatal error in %s line %d, column %d: "msg"\n", \
data->name, \
(int) XML_GetCurrentLineNumber(data->parser), \
(int) XML_GetCurrentColumnNumber(data->parser), \
- args); \
+ ##__VA_ARGS__); \
abort();\
} while (0)
defaultVal = getenv (cache->info[opt].name);
if (defaultVal != NULL) {
/* don't use XML_WARNING, we want the user to see this! */
- fprintf (stderr,
- "ATTENTION: default value of option %s overridden by environment.\n",
- cache->info[opt].name);
+ if (be_verbose()) {
+ fprintf(stderr,
+ "ATTENTION: default value of option %s overridden by environment.\n",
+ cache->info[opt].name);
+ }
} else
defaultVal = attrVal[OA_DEFAULT];
if (!parseValue (&cache->values[opt], cache->info[opt].type, defaultVal))
driOptionCache *cache;
int screenNum;
const char *driverName, *execName;
+ const char *kernelDriverName;
+ const char *engineName;
+ uint32_t engineVersion;
uint32_t ignoringDevice;
uint32_t ignoringApp;
uint32_t inDriConf;
/** \brief Elements in configuration files. */
enum OptConfElem {
- OC_APPLICATION = 0, OC_DEVICE, OC_DRICONF, OC_OPTION, OC_COUNT
+ OC_APPLICATION = 0, OC_DEVICE, OC_DRICONF, OC_ENGINE, OC_OPTION, OC_COUNT
};
static const XML_Char *OptConfElems[] = {
[OC_APPLICATION] = "application",
[OC_DEVICE] = "device",
[OC_DRICONF] = "driconf",
+ [OC_ENGINE] = "engine",
[OC_OPTION] = "option",
};
parseDeviceAttr(struct OptConfData *data, const XML_Char **attr)
{
uint32_t i;
- const XML_Char *driver = NULL, *screen = NULL;
+ const XML_Char *driver = NULL, *screen = NULL, *kernel = NULL;
for (i = 0; attr[i]; i += 2) {
if (!strcmp (attr[i], "driver")) driver = attr[i+1];
else if (!strcmp (attr[i], "screen")) screen = attr[i+1];
+ else if (!strcmp (attr[i], "kernel_driver")) kernel = attr[i+1];
else XML_WARNING("unknown device attribute: %s.", attr[i]);
}
if (driver && strcmp (driver, data->driverName))
data->ignoringDevice = data->inDevice;
+ else if (kernel && (!data->kernelDriverName || strcmp (kernel, data->kernelDriverName)))
+ data->ignoringDevice = data->inDevice;
else if (screen) {
driOptionValue screenNum;
if (!parseValue (&screenNum, DRI_INT, screen))
}
}
+static bool
+valueInRanges(const driOptionInfo *info, uint32_t value)
+{
+ uint32_t i;
+
+ for (i = 0; i < info->nRanges; i++) {
+ if (info->ranges[i].start._int <= value &&
+ info->ranges[i].end._int >= value)
+ return true;
+ }
+
+ return false;
+}
+
/** \brief Parse attributes of an application element. */
static void
parseAppAttr(struct OptConfData *data, const XML_Char **attr)
{
uint32_t i;
const XML_Char *exec = NULL;
+ const XML_Char *sha1 = NULL;
for (i = 0; attr[i]; i += 2) {
if (!strcmp (attr[i], "name")) /* not needed here */;
else if (!strcmp (attr[i], "executable")) exec = attr[i+1];
+ else if (!strcmp (attr[i], "sha1")) sha1 = attr[i+1];
else XML_WARNING("unknown application attribute: %s.", attr[i]);
}
- if (exec && strcmp (exec, data->execName))
+ if (exec && strcmp (exec, data->execName)) {
data->ignoringApp = data->inApp;
+ } else if (sha1) {
+ /* SHA1_DIGEST_STRING_LENGTH includes terminating null byte */
+ if (strlen(sha1) != (SHA1_DIGEST_STRING_LENGTH - 1)) {
+ XML_WARNING("Incorrect sha1 application attribute");
+ data->ignoringApp = data->inApp;
+ } else {
+ size_t len;
+ char* content;
+ char path[PATH_MAX];
+ if (util_get_process_exec_path(path, ARRAY_SIZE(path)) > 0 &&
+ (content = os_read_file(path, &len))) {
+ uint8_t sha1x[SHA1_DIGEST_LENGTH];
+ char sha1s[SHA1_DIGEST_STRING_LENGTH];
+ _mesa_sha1_compute(content, len, sha1x);
+ _mesa_sha1_format((char*) sha1s, sha1x);
+ free(content);
+
+ if (strcmp(sha1, sha1s)) {
+ data->ignoringApp = data->inApp;
+ }
+ } else {
+ data->ignoringApp = data->inApp;
+ }
+ }
+ }
+}
+
+/** \brief Parse attributes of an application element. */
+static void
+parseEngineAttr(struct OptConfData *data, const XML_Char **attr)
+{
+ uint32_t i;
+ const XML_Char *engine_name_match = NULL, *engine_versions = NULL;
+ driOptionInfo version_ranges = {
+ .type = DRI_INT,
+ };
+ for (i = 0; attr[i]; i += 2) {
+ if (!strcmp (attr[i], "name")) /* not needed here */;
+ else if (!strcmp (attr[i], "engine_name_match")) engine_name_match = attr[i+1];
+ else if (!strcmp (attr[i], "engine_versions")) engine_versions = attr[i+1];
+ else XML_WARNING("unknown application attribute: %s.", attr[i]);
+ }
+ if (engine_name_match) {
+ regex_t re;
+
+ if (regcomp (&re, engine_name_match, REG_EXTENDED|REG_NOSUB) == 0) {
+ if (regexec (&re, data->engineName, 0, NULL, 0) == REG_NOMATCH)
+ data->ignoringApp = data->inApp;
+ regfree (&re);
+ } else
+ XML_WARNING ("Invalid engine_name_match=\"%s\".", engine_name_match);
+ }
+ if (engine_versions) {
+ if (parseRanges (&version_ranges, engine_versions) &&
+ !valueInRanges (&version_ranges, data->engineVersion))
+ data->ignoringApp = data->inApp;
+ }
+
+ free(version_ranges.ranges);
}
/** \brief Parse attributes of an option element. */
/* don't use XML_WARNING, drirc defines options for all drivers,
* but not all drivers support them */
return;
- else if (getenv (cache->info[opt].name))
+ else if (getenv (cache->info[opt].name)) {
/* don't use XML_WARNING, we want the user to see this! */
- fprintf (stderr, "ATTENTION: option value of option %s ignored.\n",
- cache->info[opt].name);
- else if (!parseValue (&cache->values[opt], cache->info[opt].type, value))
+ if (be_verbose()) {
+ fprintf(stderr,
+ "ATTENTION: option value of option %s ignored.\n",
+ cache->info[opt].name);
+ }
+ } else if (!parseValue (&cache->values[opt], cache->info[opt].type, value))
XML_WARNING ("illegal option value: %s.", value);
}
}
if (!data->inDevice)
XML_WARNING1 ("<application> should be inside <device>.");
if (data->inApp)
- XML_WARNING1 ("nested <application> elements.");
+ XML_WARNING1 ("nested <application> or <engine> elements.");
data->inApp++;
if (!data->ignoringDevice && !data->ignoringApp)
parseAppAttr (data, attr);
break;
+ case OC_ENGINE:
+ if (!data->inDevice)
+ XML_WARNING1 ("<engine> should be inside <device>.");
+ if (data->inApp)
+ XML_WARNING1 ("nested <application> or <engine> elements.");
+ data->inApp++;
+ if (!data->ignoringDevice && !data->ignoringApp)
+ parseEngineAttr (data, attr);
+ break;
case OC_OPTION:
if (!data->inApp)
XML_WARNING1 ("<option> should be inside <application>.");
data->ignoringDevice = 0;
break;
case OC_APPLICATION:
+ case OC_ENGINE:
if (data->inApp-- == data->ignoringApp)
data->ignoringApp = 0;
break;
}
}
-/** \brief Parse the named configuration file */
static void
-parseOneConfigFile(XML_Parser p)
+_parseOneConfigFile(XML_Parser p)
{
#define BUF_SIZE 0x1000
struct OptConfData *data = (struct OptConfData *)XML_GetUserData (p);
#undef BUF_SIZE
}
+/** \brief Parse the named configuration file */
+static void
+parseOneConfigFile(struct OptConfData *data, const char *filename)
+{
+ XML_Parser p;
+
+ p = XML_ParserCreate (NULL); /* use encoding specified by file */
+ XML_SetElementHandler (p, optConfStartElem, optConfEndElem);
+ XML_SetUserData (p, data);
+ data->parser = p;
+ data->name = filename;
+ data->ignoringDevice = 0;
+ data->ignoringApp = 0;
+ data->inDriConf = 0;
+ data->inDevice = 0;
+ data->inApp = 0;
+ data->inOption = 0;
+
+ _parseOneConfigFile (p);
+ XML_ParserFree (p);
+}
+
+static int
+scandir_filter(const struct dirent *ent)
+{
+#ifndef DT_REG /* systems without d_type in dirent results */
+ struct stat st;
+
+ if ((lstat(ent->d_name, &st) != 0) ||
+ (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)))
+ return 0;
+#else
+ if (ent->d_type != DT_REG && ent->d_type != DT_LNK)
+ return 0;
+#endif
+
+ if (fnmatch("*.conf", ent->d_name, 0))
+ return 0;
+
+ return 1;
+}
+
+/** \brief Parse configuration files in a directory */
+static void
+parseConfigDir(struct OptConfData *data, const char *dirname)
+{
+ int i, count;
+ struct dirent **entries = NULL;
+
+ count = scandir(dirname, &entries, scandir_filter, alphasort);
+ if (count < 0)
+ return;
+
+ for (i = 0; i < count; i++) {
+ char filename[PATH_MAX];
+
+ snprintf(filename, PATH_MAX, "%s/%s", dirname, entries[i]->d_name);
+ free(entries[i]);
+
+ parseOneConfigFile(data, filename);
+ }
+
+ free(entries);
+}
+
#ifndef SYSCONFDIR
#define SYSCONFDIR "/etc"
#endif
+#ifndef DATADIR
+#define DATADIR "/usr/share"
+#endif
+
void
driParseConfigFiles(driOptionCache *cache, const driOptionCache *info,
- int screenNum, const char *driverName)
+ int screenNum, const char *driverName,
+ const char *kernelDriverName,
+ const char *engineName, uint32_t engineVersion)
{
- char *filenames[2] = { SYSCONFDIR "/drirc", NULL};
char *home;
- uint32_t i;
struct OptConfData userData;
initOptionCache (cache, info);
userData.cache = cache;
userData.screenNum = screenNum;
userData.driverName = driverName;
- userData.execName = GET_PROGRAM_NAME();
+ userData.kernelDriverName = kernelDriverName;
+ userData.engineName = engineName ? engineName : "";
+ userData.engineVersion = engineVersion;
+ userData.execName = util_get_process_name();
+
+ parseConfigDir(&userData, DATADIR "/drirc.d");
+ parseOneConfigFile(&userData, SYSCONFDIR "/drirc");
if ((home = getenv ("HOME"))) {
- uint32_t len = strlen (home);
- filenames[1] = malloc(len + 7+1);
- if (filenames[1] == NULL)
- __driUtilMessage ("Can't allocate memory for %s/.drirc.", home);
- else {
- memcpy (filenames[1], home, len);
- memcpy (filenames[1] + len, "/.drirc", 7+1);
- }
- }
+ char filename[PATH_MAX];
- for (i = 0; i < 2; ++i) {
- XML_Parser p;
- if (filenames[i] == NULL)
- continue;
-
- p = XML_ParserCreate (NULL); /* use encoding specified by file */
- XML_SetElementHandler (p, optConfStartElem, optConfEndElem);
- XML_SetUserData (p, &userData);
- userData.parser = p;
- userData.name = filenames[i];
- userData.ignoringDevice = 0;
- userData.ignoringApp = 0;
- userData.inDriConf = 0;
- userData.inDevice = 0;
- userData.inApp = 0;
- userData.inOption = 0;
-
- parseOneConfigFile (p);
- XML_ParserFree (p);
+ snprintf(filename, PATH_MAX, "%s/.drirc", home);
+ parseOneConfigFile(&userData, filename);
}
-
- free(filenames[1]);
}
void