Made Util::streamName and exitReason thread-local

This commit is contained in:
Thulinma 2020-08-28 19:23:48 +02:00
parent 7c6da9d455
commit 7423868de4
11 changed files with 40 additions and 32 deletions

View file

@ -30,6 +30,7 @@
#include <iostream> #include <iostream>
#include <pwd.h> #include <pwd.h>
#include <signal.h> #include <signal.h>
#include <string.h>
#include <stdarg.h> // for va_list #include <stdarg.h> // for va_list
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
@ -39,8 +40,13 @@ bool Util::Config::is_active = false;
bool Util::Config::is_restarting = false; bool Util::Config::is_restarting = false;
static Socket::Server *serv_sock_pointer = 0; static Socket::Server *serv_sock_pointer = 0;
uint32_t Util::printDebugLevel = DEBUG; uint32_t Util::printDebugLevel = DEBUG;
std::string Util::streamName; __thread char Util::streamName[256] = {0};
char Util::exitReason[256] ={0}; __thread char Util::exitReason[256] ={0};
void Util::setStreamName(const std::string & sn){
strncpy(Util::streamName, sn.c_str(), 256);
}
void Util::logExitReason(const char *format, ...){ void Util::logExitReason(const char *format, ...){
if (exitReason[0]){return;} if (exitReason[0]){return;}

View file

@ -14,8 +14,9 @@
/// Contains utility code, not directly related to streaming media /// Contains utility code, not directly related to streaming media
namespace Util{ namespace Util{
extern uint32_t printDebugLevel; extern uint32_t printDebugLevel;
extern std::string streamName; ///< Used by debug messages to identify the stream name extern __thread char streamName[256]; ///< Used by debug messages to identify the stream name
extern char exitReason[256]; void setStreamName(const std::string & sn);
extern __thread char exitReason[256];
void logExitReason(const char *format, ...); void logExitReason(const char *format, ...);
/// Deals with parsing configuration from commandline options. /// Deals with parsing configuration from commandline options.

View file

@ -32,7 +32,7 @@
//Declare as extern so we don't have to include the whole config.h header //Declare as extern so we don't have to include the whole config.h header
namespace Util{ namespace Util{
extern uint32_t printDebugLevel; extern uint32_t printDebugLevel;
extern std::string streamName; extern __thread char streamName[256];
} }
static const char *DBG_LVL_LIST[] ={"NONE", "FAIL", "ERROR", "WARN", "INFO", "MEDIUM", static const char *DBG_LVL_LIST[] ={"NONE", "FAIL", "ERROR", "WARN", "INFO", "MEDIUM",
@ -53,13 +53,13 @@ static const char *DBG_LVL_LIST[] ={"NONE", "FAIL", "ERROR", "WARN", "IN
#define DEBUG_MSG(lvl, msg, ...) \ #define DEBUG_MSG(lvl, msg, ...) \
if (Util::printDebugLevel >= lvl){\ if (Util::printDebugLevel >= lvl){\
fprintf(stderr, "%s|%s|%d|%s:%d|%s|" msg "\n", DBG_LVL_LIST[lvl], program_invocation_short_name, \ fprintf(stderr, "%s|%s|%d|%s:%d|%s|" msg "\n", DBG_LVL_LIST[lvl], program_invocation_short_name, \
getpid(), __FILE__, __LINE__, Util::streamName.c_str(), ##__VA_ARGS__); \ getpid(), __FILE__, __LINE__, Util::streamName, ##__VA_ARGS__); \
} }
#else #else
#define DEBUG_MSG(lvl, msg, ...) \ #define DEBUG_MSG(lvl, msg, ...) \
if (Util::printDebugLevel >= lvl){\ if (Util::printDebugLevel >= lvl){\
fprintf(stderr, "%s|%s|%d||%s|" msg "\n", DBG_LVL_LIST[lvl], program_invocation_short_name, \ fprintf(stderr, "%s|%s|%d||%s|" msg "\n", DBG_LVL_LIST[lvl], program_invocation_short_name, \
getpid(), Util::streamName.c_str(), ##__VA_ARGS__); \ getpid(), Util::streamName, ##__VA_ARGS__); \
} }
#endif #endif
#else #else
@ -67,13 +67,13 @@ static const char *DBG_LVL_LIST[] ={"NONE", "FAIL", "ERROR", "WARN", "IN
#define DEBUG_MSG(lvl, msg, ...) \ #define DEBUG_MSG(lvl, msg, ...) \
if (Util::printDebugLevel >= lvl){\ if (Util::printDebugLevel >= lvl){\
fprintf(stderr, "%s|MistProcess|%d|%s:%d|%s|" msg "\n", DBG_LVL_LIST[lvl], getpid(), __FILE__, \ fprintf(stderr, "%s|MistProcess|%d|%s:%d|%s|" msg "\n", DBG_LVL_LIST[lvl], getpid(), __FILE__, \
__LINE__, Util::streamName.c_str(), ##__VA_ARGS__); \ __LINE__, Util::streamName, ##__VA_ARGS__); \
} }
#else #else
#define DEBUG_MSG(lvl, msg, ...) \ #define DEBUG_MSG(lvl, msg, ...) \
if (Util::printDebugLevel >= lvl){\ if (Util::printDebugLevel >= lvl){\
fprintf(stderr, "%s|MistProcess|%d||%s|" msg "\n", DBG_LVL_LIST[lvl], getpid(), \ fprintf(stderr, "%s|MistProcess|%d||%s|" msg "\n", DBG_LVL_LIST[lvl], getpid(), \
Util::streamName.c_str(), ##__VA_ARGS__); \ Util::streamName, ##__VA_ARGS__); \
} }
#endif #endif
#endif #endif

View file

@ -310,7 +310,7 @@ namespace Mist{
int Input::boot(int argc, char *argv[]){ int Input::boot(int argc, char *argv[]){
if (!(config->parseArgs(argc, argv))){return 1;} if (!(config->parseArgs(argc, argv))){return 1;}
streamName = config->getString("streamname"); streamName = config->getString("streamname");
Util::streamName = streamName; Util::setStreamName(streamName);
if (config->getBool("json")){ if (config->getBool("json")){
capa["version"] = PACKAGE_VERSION; capa["version"] = PACKAGE_VERSION;

View file

@ -87,7 +87,7 @@ namespace Mist{
// If we have a streamname option, set internal streamname to that option // If we have a streamname option, set internal streamname to that option
if (!streamName.size() && config->hasOption("streamname")){ if (!streamName.size() && config->hasOption("streamname")){
streamName = config->getString("streamname"); streamName = config->getString("streamname");
Util::streamName = streamName; Util::setStreamName(streamName);
} }
/*LTS-START*/ /*LTS-START*/
@ -327,7 +327,7 @@ namespace Mist{
JSON::Value strCnf = Util::getStreamConfig(streamName); JSON::Value strCnf = Util::getStreamConfig(streamName);
if (strCnf && strCnf["fallback_stream"].asStringRef().size()){ if (strCnf && strCnf["fallback_stream"].asStringRef().size()){
streamName = strCnf["fallback_stream"].asStringRef(); streamName = strCnf["fallback_stream"].asStringRef();
Util::streamName = streamName; Util::setStreamName(streamName);
INFO_MSG("Switching to configured fallback stream '%s'", streamName.c_str()); INFO_MSG("Switching to configured fallback stream '%s'", streamName.c_str());
reconnect(); reconnect();
return; return;
@ -358,7 +358,7 @@ namespace Mist{
newStrm.c_str()); newStrm.c_str());
std::string origStream = streamName; std::string origStream = streamName;
streamName = newStrm; streamName = newStrm;
Util::streamName = streamName; Util::setStreamName(streamName);
if (!Util::startInput(streamName, "", true, isPushing())){ if (!Util::startInput(streamName, "", true, isPushing())){
onFail("Stream open failed (fallback stream for '" + origStream + "')", true); onFail("Stream open failed (fallback stream for '" + origStream + "')", true);
return; return;

View file

@ -450,7 +450,7 @@ namespace Mist{
INFO_MSG("Falling back to default stream '%s' -> '%s'", defStrm.c_str(), newStrm.c_str()); INFO_MSG("Falling back to default stream '%s' -> '%s'", defStrm.c_str(), newStrm.c_str());
origStreamName = streamName; origStreamName = streamName;
streamName = newStrm; streamName = newStrm;
Util::streamName = streamName; Util::setStreamName(streamName);
reconnect(); reconnect();
return getStatusJSON(reqHost, useragent); return getStatusJSON(reqHost, useragent);
} }

View file

@ -828,11 +828,11 @@ namespace Mist{
if (streamName.find('?') != std::string::npos){ if (streamName.find('?') != std::string::npos){
std::string tmpVars = streamName.substr(streamName.find('?') + 1); std::string tmpVars = streamName.substr(streamName.find('?') + 1);
streamName = streamName.substr(0, streamName.find('?')); streamName = streamName.substr(0, streamName.find('?'));
Util::streamName = streamName; Util::setStreamName(streamName);
HTTP::parseVars(tmpVars, targetParams); HTTP::parseVars(tmpVars, targetParams);
} }
Util::streamName = streamName; Util::setStreamName(streamName);
reqUrl += "/" + streamName; // LTS reqUrl += "/" + streamName; // LTS
/*LTS-START*/ /*LTS-START*/
@ -850,17 +850,17 @@ namespace Mist{
size_t lSlash = newUrl.rfind('/'); size_t lSlash = newUrl.rfind('/');
if (lSlash != std::string::npos){ if (lSlash != std::string::npos){
streamName = newUrl.substr(lSlash + 1); streamName = newUrl.substr(lSlash + 1);
Util::streamName = streamName; Util::setStreamName(streamName);
}else{ }else{
streamName = newUrl; streamName = newUrl;
Util::streamName = streamName; Util::setStreamName(streamName);
} }
} }
/*LTS-END*/ /*LTS-END*/
if (streamName.find('/')){ if (streamName.find('/')){
streamName = streamName.substr(0, streamName.find('/')); streamName = streamName.substr(0, streamName.find('/'));
Util::streamName = streamName; Util::setStreamName(streamName);
} }
size_t colonPos = streamName.find(':'); size_t colonPos = streamName.find(':');
@ -871,7 +871,7 @@ namespace Mist{
}else{ }else{
streamName = oldName.substr(colonPos + 1) + std::string(".") + oldName.substr(0, colonPos); streamName = oldName.substr(colonPos + 1) + std::string(".") + oldName.substr(0, colonPos);
} }
Util::streamName = streamName; Util::setStreamName(streamName);
} }
Util::sanitizeName(streamName); Util::sanitizeName(streamName);
@ -941,14 +941,14 @@ namespace Mist{
int8_t playMessageType = messageType; int8_t playMessageType = messageType;
int32_t playStreamId = streamId; int32_t playStreamId = streamId;
streamName = Encodings::URL::decode(amfData.getContentP(3)->StrValue()); streamName = Encodings::URL::decode(amfData.getContentP(3)->StrValue());
Util::streamName = streamName; Util::setStreamName(streamName);
reqUrl += "/" + streamName; // LTS reqUrl += "/" + streamName; // LTS
// handle variables // handle variables
if (streamName.find('?') != std::string::npos){ if (streamName.find('?') != std::string::npos){
std::string tmpVars = streamName.substr(streamName.find('?') + 1); std::string tmpVars = streamName.substr(streamName.find('?') + 1);
streamName = streamName.substr(0, streamName.find('?')); streamName = streamName.substr(0, streamName.find('?'));
Util::streamName = streamName; Util::setStreamName(streamName);
HTTP::parseVars(tmpVars, targetParams); HTTP::parseVars(tmpVars, targetParams);
} }
@ -960,7 +960,7 @@ namespace Mist{
}else{ }else{
streamName = oldName.substr(colonPos + 1) + std::string(".") + oldName.substr(0, colonPos); streamName = oldName.substr(colonPos + 1) + std::string(".") + oldName.substr(0, colonPos);
} }
Util::streamName = streamName; Util::setStreamName(streamName);
} }
Util::sanitizeName(streamName); Util::sanitizeName(streamName);

View file

@ -46,7 +46,7 @@ namespace Mist{
streamName = opt["sink"].asString(); streamName = opt["sink"].asString();
if (!streamName.size()){streamName = opt["source"].asString();} if (!streamName.size()){streamName = opt["source"].asString();}
Util::streamVariables(streamName, opt["source"].asString()); Util::streamVariables(streamName, opt["source"].asString());
Util::streamName = opt["source"].asString() + "" + streamName; Util::setStreamName(opt["source"].asString() + "" + streamName);
} }
bool needsLock(){return false;} bool needsLock(){return false;}
bool isSingular(){return false;} bool isSingular(){return false;}

View file

@ -389,7 +389,7 @@ namespace Mist{
streamName = opt["sink"].asString(); streamName = opt["sink"].asString();
if (!streamName.size()){streamName = opt["source"].asString();} if (!streamName.size()){streamName = opt["source"].asString();}
Util::streamVariables(streamName, opt["source"].asString()); Util::streamVariables(streamName, opt["source"].asString());
Util::streamName = opt["source"].asString() + "" + streamName; Util::setStreamName(opt["source"].asString() + "" + streamName);
} }
std::string EncodeOutputEBML::getTrackType(int tid){return M.getType(tid);} std::string EncodeOutputEBML::getTrackType(int tid){return M.getType(tid);}

View file

@ -227,7 +227,7 @@ namespace Mist{
streamName = opt["sink"].asString(); streamName = opt["sink"].asString();
if (!streamName.size()){streamName = opt["source"].asString();} if (!streamName.size()){streamName = opt["source"].asString();}
Util::streamVariables(streamName, opt["source"].asString()); Util::streamVariables(streamName, opt["source"].asString());
Util::streamName = opt["source"].asString() + "" + streamName; Util::setStreamName(opt["source"].asString() + "" + streamName);
preRun(); preRun();
}; };
virtual bool needsLock(){return false;} virtual bool needsLock(){return false;}

View file

@ -4,6 +4,7 @@
#include <mist/stream.h> #include <mist/stream.h>
#include <mist/procs.h> #include <mist/procs.h>
#include <mist/comms.h> #include <mist/comms.h>
#include <mist/config.h>
const char * getStateString(uint8_t state){ const char * getStateString(uint8_t state){
switch (state){ switch (state){
@ -21,7 +22,7 @@ const char * getStateString(uint8_t state){
/// Gets a PID from a shared memory page, if it exists /// Gets a PID from a shared memory page, if it exists
uint64_t getPidFromPage(const char * pagePattern){ uint64_t getPidFromPage(const char * pagePattern){
char pageName[NAME_BUFFER_SIZE]; char pageName[NAME_BUFFER_SIZE];
snprintf(pageName, NAME_BUFFER_SIZE, pagePattern, Util::streamName.c_str()); snprintf(pageName, NAME_BUFFER_SIZE, pagePattern, Util::streamName);
IPC::sharedPage pidPage(pageName, 8, false, false); IPC::sharedPage pidPage(pageName, 8, false, false);
if (pidPage){ if (pidPage){
return *(uint64_t*)(pidPage.mapped); return *(uint64_t*)(pidPage.mapped);
@ -32,7 +33,7 @@ uint64_t getPidFromPage(const char * pagePattern){
/// Deletes a shared memory page, if it exists /// Deletes a shared memory page, if it exists
void nukePage(const char * pagePattern){ void nukePage(const char * pagePattern){
char pageName[NAME_BUFFER_SIZE]; char pageName[NAME_BUFFER_SIZE];
snprintf(pageName, NAME_BUFFER_SIZE, pagePattern, Util::streamName.c_str()); snprintf(pageName, NAME_BUFFER_SIZE, pagePattern, Util::streamName);
IPC::sharedPage page(pageName, 0, false, false); IPC::sharedPage page(pageName, 0, false, false);
page.master = true; page.master = true;
} }
@ -40,7 +41,7 @@ void nukePage(const char * pagePattern){
/// Deletes a semaphore, if it exists /// Deletes a semaphore, if it exists
void nukeSem(const char * pagePattern){ void nukeSem(const char * pagePattern){
char pageName[NAME_BUFFER_SIZE]; char pageName[NAME_BUFFER_SIZE];
snprintf(pageName, NAME_BUFFER_SIZE, pagePattern, Util::streamName.c_str()); snprintf(pageName, NAME_BUFFER_SIZE, pagePattern, Util::streamName);
IPC::semaphore sem(pageName, O_RDWR, ACCESSPERMS, 0, true); IPC::semaphore sem(pageName, O_RDWR, ACCESSPERMS, 0, true);
if (sem){sem.unlink();} if (sem){sem.unlink();}
} }
@ -51,7 +52,7 @@ int main(int argc, char **argv){
FAIL_MSG("Usage: %s STREAM_NAME", argv[0]); FAIL_MSG("Usage: %s STREAM_NAME", argv[0]);
return 1; return 1;
} }
Util::streamName = argv[1]; Util::setStreamName(argv[1]);
uint8_t state = Util::getStreamStatus(Util::streamName); uint8_t state = Util::getStreamStatus(Util::streamName);
INFO_MSG("Current stream status: %s", getStateString(state)); INFO_MSG("Current stream status: %s", getStateString(state));
size_t loops = 0; size_t loops = 0;
@ -73,7 +74,7 @@ int main(int argc, char **argv){
// Scoping to clear up metadata and track providers // Scoping to clear up metadata and track providers
{ {
char pageName[NAME_BUFFER_SIZE]; char pageName[NAME_BUFFER_SIZE];
snprintf(pageName, NAME_BUFFER_SIZE, SHM_STREAM_META, Util::streamName.c_str()); snprintf(pageName, NAME_BUFFER_SIZE, SHM_STREAM_META, Util::streamName);
IPC::sharedPage streamPage(pageName, 0, false, false); IPC::sharedPage streamPage(pageName, 0, false, false);
if (streamPage.mapped){ if (streamPage.mapped){
streamPage.master = true; streamPage.master = true;
@ -98,7 +99,7 @@ int main(int argc, char **argv){
for (uint64_t j = pages.getDeleted(); j < pages.getEndPos(); j++){ for (uint64_t j = pages.getDeleted(); j < pages.getEndPos(); j++){
char thisPageName[NAME_BUFFER_SIZE]; char thisPageName[NAME_BUFFER_SIZE];
snprintf(thisPageName, NAME_BUFFER_SIZE, SHM_TRACK_DATA, snprintf(thisPageName, NAME_BUFFER_SIZE, SHM_TRACK_DATA,
Util::streamName.c_str(), i, pages.getInt("firstkey", j)); Util::streamName, i, pages.getInt("firstkey", j));
IPC::sharedPage p(thisPageName, 0); IPC::sharedPage p(thisPageName, 0);
p.master = true; p.master = true;
} }