zynaddsubfx

ZynAddSubFX open source synthesizer
Log | Files | Refs | Submodules | LICENSE

commit 6e7dd4146d2f4bc7a2618000337e7ba35373232d
parent 1b004be9e189fe3b958e198703ce902a1318717c
Author: Johannes Lorenz <[email protected]>
Date:   Sat, 28 Mar 2015 15:07:20 +0100

Appended PID to jack client names.

Diffstat:
Msrc/Misc/Util.cpp | 43+++++++++++++++++++++++++++++++++++++++++--
Msrc/Misc/Util.h | 3+++
Msrc/Nio/JackEngine.cpp | 5+++++
Msrc/Nio/JackMultiEngine.cpp | 2++
4 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/src/Misc/Util.cpp b/src/Misc/Util.cpp @@ -23,8 +23,9 @@ #include "Util.h" #include <vector> #include <cassert> -#include <math.h> -#include <stdio.h> +#include <cmath> +#include <cstdio> +#include <fstream> #include <err.h> #include <sys/types.h> @@ -136,6 +137,43 @@ void os_sleep(long length) usleep(length); } +//!< maximum lenght a pid has on any POSIX system +//!< this is an estimation, but more than 12 looks insane +constexpr std::size_t max_pid_len = 12; + +//!< safe pid lenght guess, posix conform +std::size_t os_guess_pid_length() +{ + const char* pid_max_file = "/proc/sys/kernel/pid_max"; + if(-1 == access(pid_max_file, R_OK)) { + return max_pid_len; + } + else { + std::ifstream is(pid_max_file); + if(!is.good()) + return max_pid_len; + else { + std::string s; + is >> s; + for(const auto& c : s) + if(c < '0' || c > '9') + return max_pid_len; + return std::min(s.length(), max_pid_len); + } + } +} + +//!< returns pid padded, posix conform +std::string os_pid_as_padded_string() +{ + char result_str[max_pid_len << 1]; + std::fill_n(result_str, max_pid_len, '0'); + std::size_t written = snprintf(result_str + max_pid_len, max_pid_len, + "%d", (int)getpid()); + // the below pointer should never cause segfaults: + return result_str + max_pid_len + written - os_guess_pid_length(); +} + std::string legalizeFilename(std::string filename) { for(int i = 0; i < (int) filename.size(); ++i) { @@ -179,3 +217,4 @@ const char *message_snip(const char *m) while(*m && *m!='/')++m; return *m?m+1:m; } + diff --git a/src/Misc/Util.h b/src/Misc/Util.h @@ -52,6 +52,9 @@ void set_realtime(); /**Os independent sleep in microsecond*/ void os_sleep(long length); +//! returns pid padded to maximum pid lenght, posix conform +std::string os_pid_as_padded_string(); + std::string legalizeFilename(std::string filename); extern float *denormalkillbuf; /**<the buffer to add noise in order to avoid denormalisation*/ diff --git a/src/Nio/JackEngine.cpp b/src/Nio/JackEngine.cpp @@ -30,10 +30,13 @@ #include <sys/stat.h> #include <cassert> #include <cstring> +#include <unistd.h> // access() +#include <fstream> // std::istream #include "Nio.h" #include "OutMgr.h" #include "InMgr.h" +#include "Misc/Util.h" #include "JackEngine.h" @@ -65,6 +68,8 @@ bool JackEngine::connectServer(string server) string postfix = Nio::getPostfix(); if(!postfix.empty()) clientname += "_" + postfix; + clientname += "_" + os_pid_as_padded_string(); + jack_status_t jackstatus; bool use_server_name = server.size() && server.compare("default") != 0; jack_options_t jopts = (jack_options_t) diff --git a/src/Nio/JackMultiEngine.cpp b/src/Nio/JackMultiEngine.cpp @@ -28,6 +28,7 @@ #include <cassert> #include "Nio.h" +#include "Misc/Util.h" #include "../Misc/Master.h" #include "../Misc/Part.h" #include "../Misc/MiddleWare.h" @@ -82,6 +83,7 @@ bool JackMultiEngine::Start(void) string postfix = Nio::getPostfix(); if(!postfix.empty()) clientname += "_" + postfix; + clientname += "_" + os_pid_as_padded_string(); jack_status_t jackstatus; impl->client = jack_client_open(clientname.c_str(), JackNullOption, &jackstatus);