2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2020-08-07 23:23:21 +03:00
|
|
|
/* ex: set tabstop=8 softtabstop=2 shiftwidth=2 expandtab: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2004-09-07 21:55:27 +04:00
|
|
|
|
|
|
|
#include "nsDebug.h"
|
2005-05-08 19:01:20 +04:00
|
|
|
#include "nsString.h"
|
2004-09-07 21:55:27 +04:00
|
|
|
#include "nsCUPSShim.h"
|
2014-01-30 22:26:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2021-03-29 13:08:44 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2004-09-07 21:55:27 +04:00
|
|
|
#include "prlink.h"
|
|
|
|
|
2020-08-11 03:25:18 +03:00
|
|
|
#ifdef CUPS_SHIM_RUNTIME_LINK
|
|
|
|
|
2021-03-29 13:08:44 +03:00
|
|
|
mozilla::LazyLogModule gCupsLinkLog("CupsLink");
|
|
|
|
|
|
|
|
# define DEBUG_LOG(...) \
|
|
|
|
MOZ_LOG(gCupsLinkLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
|
|
|
|
|
2020-08-11 03:25:18 +03:00
|
|
|
// TODO: This is currently pointless as we always use the compile-time linked
|
|
|
|
// version of CUPS, but in the future this may become a configure option.
|
|
|
|
// We also cannot use NSPR's library suffix support, since that cannot handle
|
|
|
|
// version number suffixes.
|
|
|
|
# ifdef XP_MACOSX
|
2020-07-17 20:34:20 +03:00
|
|
|
static const char gCUPSLibraryName[] = "libcups.2.dylib";
|
2020-08-11 03:25:18 +03:00
|
|
|
# else
|
2020-07-17 20:34:20 +03:00
|
|
|
static const char gCUPSLibraryName[] = "libcups.so.2";
|
2020-08-11 03:25:18 +03:00
|
|
|
# endif
|
2020-07-17 20:34:20 +03:00
|
|
|
|
2021-03-29 13:08:44 +03:00
|
|
|
static bool LoadCupsFunc(PRLibrary* aLib, void** aDest, const char* const aName,
|
|
|
|
nsCUPSShim::Optional aOptional) {
|
|
|
|
*aDest = PR_FindSymbol(aLib, aName);
|
|
|
|
if (!*aDest) {
|
|
|
|
DEBUG_LOG("%s not found in CUPS library", aName);
|
|
|
|
return bool(aOptional);
|
2020-07-20 23:07:18 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-04 21:56:13 +03:00
|
|
|
nsCUPSShim::nsCUPSShim() {
|
2020-07-20 23:07:18 +03:00
|
|
|
mCupsLib = PR_LoadLibrary(gCUPSLibraryName);
|
|
|
|
if (!mCupsLib) {
|
2021-03-29 13:08:44 +03:00
|
|
|
DEBUG_LOG("CUPS library not found");
|
2020-11-04 21:56:13 +03:00
|
|
|
return;
|
2020-07-20 23:07:18 +03:00
|
|
|
}
|
2020-02-04 17:38:49 +03:00
|
|
|
|
2021-03-29 13:08:44 +03:00
|
|
|
bool success = true;
|
|
|
|
|
2020-11-04 21:56:13 +03:00
|
|
|
// This is a macro so that it could also load from libcups if we are
|
|
|
|
// configured to use it as a compile-time dependency.
|
2021-03-29 13:08:44 +03:00
|
|
|
//
|
|
|
|
// We try to load all functions even if some fail so that we get the debug log
|
|
|
|
// unconditionally.
|
|
|
|
# define CUPS_SHIM_LOAD(opt_, fn_) \
|
|
|
|
success |= \
|
|
|
|
LoadCupsFunc(mCupsLib, reinterpret_cast<void**>(&fn_), #fn_, opt_);
|
2020-08-06 20:01:21 +03:00
|
|
|
CUPS_SHIM_ALL_FUNCS(CUPS_SHIM_LOAD)
|
2020-08-11 03:25:18 +03:00
|
|
|
# undef CUPS_SHIM_LOAD
|
|
|
|
|
2021-03-29 13:08:44 +03:00
|
|
|
if (!success) {
|
|
|
|
# ifndef MOZ_TSAN
|
|
|
|
// With TSan, we cannot unload libcups once we have loaded it because
|
|
|
|
// TSan does not support unloading libraries that are matched from its
|
|
|
|
// suppression list. Hence we just keep the library loaded in TSan builds.
|
|
|
|
PR_UnloadLibrary(mCupsLib);
|
|
|
|
# endif
|
|
|
|
mCupsLib = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-04 21:56:13 +03:00
|
|
|
// Set mInitOkay only if all cups functions are loaded successfully.
|
|
|
|
mInitOkay = true;
|
2004-09-07 21:55:27 +04:00
|
|
|
}
|
2020-08-11 03:25:18 +03:00
|
|
|
|
|
|
|
#endif
|