|
Packit |
534379 |
// Copyright(c) 2017-2020, Intel Corporation
|
|
Packit |
534379 |
//
|
|
Packit |
534379 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
534379 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
534379 |
//
|
|
Packit |
534379 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
534379 |
// this list of conditions and the following disclaimer.
|
|
Packit |
534379 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
534379 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
534379 |
// and/or other materials provided with the distribution.
|
|
Packit |
534379 |
// * Neither the name of Intel Corporation nor the names of its contributors
|
|
Packit |
534379 |
// may be used to endorse or promote products derived from this software
|
|
Packit |
534379 |
// without specific prior written permission.
|
|
Packit |
534379 |
//
|
|
Packit |
534379 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
534379 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
534379 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
534379 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
534379 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
534379 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
534379 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
534379 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
534379 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
534379 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
534379 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
534379 |
#include <config.h>
|
|
Packit |
534379 |
#endif // HAVE_CONFIG_H
|
|
Packit |
534379 |
#define _GNU_SOURCE
|
|
Packit |
534379 |
#include <stdio.h>
|
|
Packit |
534379 |
#include <stdarg.h>
|
|
Packit |
534379 |
#include <stdlib.h>
|
|
Packit |
534379 |
#include <string.h>
|
|
Packit |
534379 |
#include <errno.h>
|
|
Packit |
534379 |
#include <linux/limits.h>
|
|
Packit |
534379 |
#include <pwd.h>
|
|
Packit |
534379 |
#include <unistd.h>
|
|
Packit |
534379 |
#ifndef __USE_GNU
|
|
Packit |
534379 |
#define __USE_GNU
|
|
Packit |
534379 |
#endif // __USE_GNU
|
|
Packit |
534379 |
#include <pthread.h>
|
|
Packit |
534379 |
|
|
Packit |
534379 |
#include <opae/init.h>
|
|
Packit |
534379 |
#include <opae/utils.h>
|
|
Packit |
534379 |
#include "pluginmgr.h"
|
|
Packit |
534379 |
#include "opae_int.h"
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/* global loglevel */
|
|
Packit |
534379 |
static int g_loglevel = OPAE_DEFAULT_LOGLEVEL;
|
|
Packit |
534379 |
static FILE *g_logfile;
|
|
Packit |
534379 |
/* mutex to protect against garbled log output */
|
|
Packit |
534379 |
static pthread_mutex_t log_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
#define CFG_PATH_MAX 64
|
|
Packit |
534379 |
#define HOME_CFG_PATHS 3
|
|
Packit |
534379 |
STATIC const char _ase_home_cfg_files[HOME_CFG_PATHS][CFG_PATH_MAX] = {
|
|
Packit |
534379 |
{ "/.local/opae_ase.cfg" },
|
|
Packit |
534379 |
{ "/.local/opae/opae_ase.cfg" },
|
|
Packit |
534379 |
{ "/.config/opae/opae_ase.cfg" },
|
|
Packit |
534379 |
};
|
|
Packit |
534379 |
#define SYS_CFG_PATHS 2
|
|
Packit |
534379 |
STATIC const char _ase_sys_cfg_files[SYS_CFG_PATHS][CFG_PATH_MAX] = {
|
|
Packit |
534379 |
{ "/usr/local/etc/opae/opae_ase.cfg" },
|
|
Packit |
534379 |
{ "/etc/opae/opae_ase.cfg" },
|
|
Packit |
534379 |
};
|
|
Packit |
534379 |
|
|
Packit |
534379 |
void opae_print(int loglevel, const char *fmt, ...)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
FILE *fp;
|
|
Packit |
534379 |
int err;
|
|
Packit |
534379 |
va_list argp;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (loglevel > g_loglevel)
|
|
Packit |
534379 |
return;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (loglevel == OPAE_LOG_ERROR)
|
|
Packit |
534379 |
fp = stderr;
|
|
Packit |
534379 |
else
|
|
Packit |
534379 |
fp = g_logfile == NULL ? stdout : g_logfile;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
va_start(argp, fmt);
|
|
Packit |
534379 |
err = pthread_mutex_lock(
|
|
Packit |
534379 |
&log_lock); /* ignore failure and print anyway */
|
|
Packit |
534379 |
if (err)
|
|
Packit |
534379 |
fprintf(stderr, "pthread_mutex_lock() failed: %s",
|
|
Packit |
534379 |
strerror(err));
|
|
Packit |
534379 |
vfprintf(fp, fmt, argp);
|
|
Packit |
534379 |
err = pthread_mutex_unlock(&log_lock);
|
|
Packit |
534379 |
if (err)
|
|
Packit |
534379 |
fprintf(stderr, "pthread_mutex_unlock() failed: %s",
|
|
Packit |
534379 |
strerror(err));
|
|
Packit |
534379 |
va_end(argp);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/* Find the canonicalized configuration file opae_ase.cfg. If null, the file
|
|
Packit |
534379 |
was not found. Otherwise, it's the first configuration file found from a
|
|
Packit |
534379 |
list of possible paths. Note: The char * returned is allocated here, caller
|
|
Packit |
534379 |
must free. */
|
|
Packit |
534379 |
STATIC char *find_ase_cfg(void)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
int i = 0;
|
|
Packit |
534379 |
char *file_name = NULL;
|
|
Packit |
534379 |
char *opae_path = NULL;
|
|
Packit |
534379 |
char cfg_path[PATH_MAX] = { 0, };
|
|
Packit |
534379 |
char home_cfg[PATH_MAX] = { 0, };
|
|
Packit |
534379 |
size_t len;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// get the user's home directory
|
|
Packit |
534379 |
struct passwd *user_passwd = getpwuid(getuid());
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// first look in the OPAE source directory
|
|
Packit |
534379 |
file_name = canonicalize_file_name(OPAE_ASE_CFG_SRC_PATH);
|
|
Packit |
534379 |
if (file_name)
|
|
Packit |
534379 |
return file_name;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// second look in OPAE installation directory
|
|
Packit |
534379 |
file_name = canonicalize_file_name(OPAE_ASE_CFG_INST_PATH);
|
|
Packit |
534379 |
if (file_name)
|
|
Packit |
534379 |
return file_name;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// third look in the release directory
|
|
Packit |
534379 |
opae_path = getenv("OPAE_PLATFORM_ROOT");
|
|
Packit |
534379 |
if (opae_path) {
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (snprintf(cfg_path, sizeof(cfg_path),
|
|
Packit |
534379 |
"%s/share/opae/ase/opae_ase.cfg", opae_path) < 0) {
|
|
Packit |
534379 |
OPAE_ERR("snprintf buffer overflow");
|
|
Packit |
534379 |
} else {
|
|
Packit |
534379 |
file_name = canonicalize_file_name(cfg_path);
|
|
Packit |
534379 |
if (file_name)
|
|
Packit |
534379 |
return file_name;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// fourth look in possible paths in the users home directory
|
|
Packit |
534379 |
if (user_passwd != NULL) {
|
|
Packit |
534379 |
for (i = 0; i < HOME_CFG_PATHS; ++i) {
|
|
Packit |
534379 |
if (snprintf(home_cfg, sizeof(home_cfg),
|
|
Packit |
534379 |
"%s%s", user_passwd->pw_dir,
|
|
Packit |
534379 |
_ase_home_cfg_files[i]) < 0) {
|
|
Packit |
534379 |
OPAE_ERR("snprintf buffer overflow");
|
|
Packit |
534379 |
} else {
|
|
Packit |
534379 |
file_name = canonicalize_file_name(home_cfg);
|
|
Packit |
534379 |
if (file_name)
|
|
Packit |
534379 |
return file_name;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// now look in possible system paths
|
|
Packit |
534379 |
for (i = 0; i < SYS_CFG_PATHS; ++i) {
|
|
Packit |
534379 |
len = strnlen(_ase_sys_cfg_files[i], CFG_PATH_MAX - 1);
|
|
Packit |
534379 |
memcpy(home_cfg, _ase_sys_cfg_files[i], len);
|
|
Packit |
534379 |
home_cfg[len] = '\0';
|
|
Packit |
534379 |
file_name = canonicalize_file_name(home_cfg);
|
|
Packit |
534379 |
if (file_name)
|
|
Packit |
534379 |
return file_name;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
return NULL;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
__attribute__((constructor)) STATIC void opae_init(void)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
fpga_result res;
|
|
Packit |
534379 |
g_logfile = NULL;
|
|
Packit |
534379 |
char *cfg_path = NULL;
|
|
Packit |
534379 |
char *with_ase = NULL;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
/* try to read loglevel from environment */
|
|
Packit |
534379 |
char *s = getenv("LIBOPAE_LOG");
|
|
Packit |
534379 |
if (s) {
|
|
Packit |
534379 |
g_loglevel = atoi(s);
|
|
Packit |
534379 |
#ifndef LIBOPAE_DEBUG
|
|
Packit |
534379 |
if (g_loglevel >= OPAE_LOG_DEBUG)
|
|
Packit |
534379 |
fprintf(stderr,
|
|
Packit |
534379 |
"WARNING: Environment variable LIBOPAE_LOG is "
|
|
Packit |
534379 |
"set to output debug\nmessages, "
|
|
Packit |
534379 |
"but libopae-c was not built with debug "
|
|
Packit |
534379 |
"information.\n");
|
|
Packit |
534379 |
#endif
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
s = getenv("LIBOPAE_LOGFILE");
|
|
Packit |
534379 |
if (s) {
|
|
Packit |
534379 |
if (s[0] != '/' || !strncmp(s, "/tmp/", 5)) {
|
|
Packit |
534379 |
g_logfile = fopen(s, "w");
|
|
Packit |
534379 |
if (g_logfile == NULL) {
|
|
Packit |
534379 |
fprintf(stderr,
|
|
Packit |
534379 |
"Could not open log file for writing: %s. ", s);
|
|
Packit |
534379 |
fprintf(stderr, "Error is: %s\n", strerror(errno));
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (g_logfile == NULL)
|
|
Packit |
534379 |
g_logfile = stdout;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
with_ase = getenv("WITH_ASE");
|
|
Packit |
534379 |
if (with_ase) {
|
|
Packit |
534379 |
cfg_path = find_ase_cfg();
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (cfg_path == NULL) {
|
|
Packit |
534379 |
OPAE_ERR("WITH_ASE was set, but could not find opae_ase.cfg file");
|
|
Packit |
534379 |
return;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
res = fpgaInitialize(cfg_path);
|
|
Packit |
534379 |
if (res != FPGA_OK)
|
|
Packit |
534379 |
OPAE_ERR("fpgaInitialize: %s", fpgaErrStr(res));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
free(cfg_path);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
// If the environment hasn't requested explicit initialization,
|
|
Packit |
534379 |
// perform the initialization implicitly here.
|
|
Packit |
534379 |
else if (getenv("OPAE_EXPLICIT_INITIALIZE") == NULL)
|
|
Packit |
534379 |
fpgaInitialize(NULL);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
__attribute__((destructor)) STATIC void opae_release(void)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
fpga_result res;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
res = fpgaFinalize();
|
|
Packit |
534379 |
if (res != FPGA_OK)
|
|
Packit |
534379 |
OPAE_ERR("fpgaFinalize: %s", fpgaErrStr(res));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (g_logfile != NULL && g_logfile != stdout) {
|
|
Packit |
534379 |
fclose(g_logfile);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
g_logfile = NULL;
|
|
Packit |
534379 |
}
|