From 56c05c3ad27ec4e2682b16d5dc2e042794fb02da Mon Sep 17 00:00:00 2001 From: falkTX Date: Mon, 18 Mar 2019 02:08:56 +0100 Subject: Rework uuids to never be int, more cleanup Signed-off-by: falkTX --- common/JackAPI.cpp | 4 +-- common/JackClient.cpp | 6 ++-- common/JackClientControl.h | 6 ++-- common/JackConstants.h | 1 + common/JackDriver.cpp | 5 ++-- common/JackEngine.cpp | 43 +++++++++++++++++------------ common/JackMetadata.cpp | 2 +- common/JackRequest.h | 68 +++++++++++++++++++++++----------------------- common/varargs.h | 10 ++++--- example-clients/property.c | 12 ++++---- 10 files changed, 85 insertions(+), 72 deletions(-) diff --git a/common/JackAPI.cpp b/common/JackAPI.cpp index 98d53dfd..60142df9 100644 --- a/common/JackAPI.cpp +++ b/common/JackAPI.cpp @@ -1982,8 +1982,8 @@ LIB_EXPORT char *jack_client_get_uuid(jack_client_t* ext_client) jack_error("jack_client_get_uuid called with a NULL client"); return NULL; } else { - char retval[16]; - snprintf(retval, sizeof(retval), "%" PRIu64, client->GetClientControl()->fSessionID); + char retval[JACK_UUID_STRING_SIZE]; + jack_uuid_unparse(client->GetClientControl()->fSessionID, retval); return strdup(retval); } } diff --git a/common/JackClient.cpp b/common/JackClient.cpp index cdd99af0..5fcab377 100644 --- a/common/JackClient.cpp +++ b/common/JackClient.cpp @@ -304,12 +304,12 @@ int JackClient::ClientNotify(int refnum, const char* name, int notify, int sync, jack_log("JackClient::kSessionCallback"); if (fSession) { jack_session_event_t* event = (jack_session_event_t*)malloc( sizeof(jack_session_event_t)); - char uuid_buf[JACK_UUID_SIZE]; + char uuid_buf[JACK_UUID_STRING_SIZE]; event->type = (jack_session_event_type_t)value1; event->session_dir = strdup(message); event->command_line = NULL; event->flags = (jack_session_flags_t)0; - snprintf(uuid_buf, sizeof(uuid_buf), "%" PRIu64, GetClientControl()->fSessionID); + jack_uuid_unparse(GetClientControl()->fSessionID, uuid_buf); event->client_uuid = strdup(uuid_buf); fSessionReply = kPendingSessionReply; // Session callback may change fSessionReply by directly using jack_session_reply @@ -1306,7 +1306,7 @@ int JackClient::SessionReply(jack_session_event_t* ev) char* JackClient::GetUUIDForClientName(const char* client_name) { - char uuid_res[JACK_UUID_SIZE]; + char uuid_res[JACK_UUID_STRING_SIZE]; int result = -1; fChannel->GetUUIDForClientName(GetClientControl()->fRefNum, client_name, uuid_res, &result); return (result) ? NULL : strdup(uuid_res); diff --git a/common/JackClientControl.h b/common/JackClientControl.h index 107345fa..f84ed648 100644 --- a/common/JackClientControl.h +++ b/common/JackClientControl.h @@ -55,14 +55,14 @@ struct JackClientControl : public JackShmMemAble Init(name, pid, refnum, uuid); } - JackClientControl(const char* name) + JackClientControl(const char* name, jack_uuid_t uuid) { - Init(name, 0, -1, 0); + Init(name, 0, -1, uuid); } JackClientControl() { - Init("", 0, -1, 0); + Init("", 0, -1, JACK_UUID_EMPTY_INITIALIZER); } void Init(const char* name, int pid, int refnum, jack_uuid_t uuid) diff --git a/common/JackConstants.h b/common/JackConstants.h index 0cf34045..e6c53327 100644 --- a/common/JackConstants.h +++ b/common/JackConstants.h @@ -36,6 +36,7 @@ #define JACK_MESSAGE_SIZE 256 #define JACK_UUID_SIZE 36 // to match jack1 and uuid.h #define JACK_UUID_STRING_SIZE (JACK_UUID_SIZE+1) /* includes trailing null */ +#define JACK_UUID_EMPTY_INITIALIZER 0 #define JACK_SESSION_COMMAND_SIZE 256 #define SYNC_MAX_NAME_SIZE 256 diff --git a/common/JackDriver.cpp b/common/JackDriver.cpp index aae7ee7b..8700b1c9 100644 --- a/common/JackDriver.cpp +++ b/common/JackDriver.cpp @@ -40,8 +40,9 @@ namespace Jack JackDriver::JackDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table) :fCaptureChannels(0), fPlaybackChannels(0), - fClientControl(name), - fWithMonitorPorts(false){ + fClientControl(name, jack_client_uuid_generate()), + fWithMonitorPorts(false) +{ assert(strlen(name) < JACK_CLIENT_NAME_SIZE); fSynchroTable = table; strcpy(fAliasName, alias); diff --git a/common/JackEngine.cpp b/common/JackEngine.cpp index 85dc245e..be87c977 100644 --- a/common/JackEngine.cpp +++ b/common/JackEngine.cpp @@ -43,7 +43,7 @@ JackEngine::JackEngine(JackGraphManager* manager, char self_connect_mode) : JackLockAble(control->fServerName), fSignal(control->fServerName), - fMetadata(control->fServerName) + fMetadata(NULL) // FIXME use control->fServerName? { fGraphManager = manager; fSynchroTable = table; @@ -572,7 +572,9 @@ void JackEngine::EnsureUUID(jack_uuid_t uuid) for (int i = 0; i < CLIENT_NUM; i++) { JackClientInterface* client = fClientTable[i]; if (client && jack_uuid_compare(client->GetClientControl()->fSessionID, uuid) == 0) { + // FIXME? this code does nothing, but jack1 has it like this too.. jack_uuid_clear (&uuid); + // client->GetClientControl()->fSessionID = jack_client_uuid_generate(); } } } @@ -606,7 +608,7 @@ int JackEngine::ClientExternalOpen(const char* name, int pid, jack_uuid_t uuid, { char real_name[JACK_CLIENT_NAME_SIZE + 1]; - if (uuid < 0) { + if (jack_uuid_empty(uuid)) { uuid = jack_client_uuid_generate(); strncpy(real_name, name, JACK_CLIENT_NAME_SIZE); } else { @@ -1085,7 +1087,7 @@ void JackEngine::SessionNotify(int refnum, const char *target, jack_session_even for (int i = 0; i < CLIENT_NUM; i++) { JackClientInterface* client = fClientTable[i]; - if (client && (client->GetClientControl()->fSessionID < 0)) { + if (client && jack_uuid_empty(client->GetClientControl()->fSessionID)) { client->GetClientControl()->fSessionID = jack_client_uuid_generate(); } } @@ -1117,8 +1119,8 @@ void JackEngine::SessionNotify(int refnum, const char *target, jack_session_even if (result == kPendingSessionReply) { fSessionPendingReplies += 1; } else if (result == kImmediateSessionReply) { - char uuid_buf[JACK_UUID_SIZE]; - snprintf(uuid_buf, sizeof(uuid_buf), "%" PRIu64, client->GetClientControl()->fSessionID); + char uuid_buf[JACK_UUID_STRING_SIZE]; + jack_uuid_unparse(client->GetClientControl()->fSessionID, uuid_buf); fSessionResult->fCommandList.push_back(JackSessionCommand(uuid_buf, client->GetClientControl()->fName, client->GetClientControl()->fSessionCommand, @@ -1142,8 +1144,8 @@ int JackEngine::SessionReply(int refnum) { JackClientInterface* client = fClientTable[refnum]; assert(client); - char uuid_buf[JACK_UUID_SIZE]; - snprintf(uuid_buf, sizeof(uuid_buf), "%" PRIu64, client->GetClientControl()->fSessionID); + char uuid_buf[JACK_UUID_STRING_SIZE]; + jack_uuid_unparse(client->GetClientControl()->fSessionID, uuid_buf); fSessionResult->fCommandList.push_back(JackSessionCommand(uuid_buf, client->GetClientControl()->fName, client->GetClientControl()->fSessionCommand, @@ -1167,7 +1169,7 @@ int JackEngine::GetUUIDForClientName(const char *client_name, char *uuid_res) JackClientInterface* client = fClientTable[i]; if (client && (strcmp(client_name, client->GetClientControl()->fName) == 0)) { - snprintf(uuid_res, JACK_UUID_SIZE, "%" PRIu64, client->GetClientControl()->fSessionID); + jack_uuid_unparse(client->GetClientControl()->fSessionID, uuid_res); return 0; } } @@ -1175,8 +1177,12 @@ int JackEngine::GetUUIDForClientName(const char *client_name, char *uuid_res) return -1; } -int JackEngine::GetClientNameForUUID(const char *uuid, char *name_res) +int JackEngine::GetClientNameForUUID(const char *uuid_buf, char *name_res) { + jack_uuid_t uuid; + if (jack_uuid_parse(uuid_buf, &uuid) != 0) + return -1; + for (int i = 0; i < CLIENT_NUM; i++) { JackClientInterface* client = fClientTable[i]; @@ -1184,10 +1190,7 @@ int JackEngine::GetClientNameForUUID(const char *uuid, char *name_res) continue; } - char uuid_buf[JACK_UUID_SIZE]; - snprintf(uuid_buf, JACK_UUID_SIZE, "%" PRIu64, client->GetClientControl()->fSessionID); - - if (strcmp(uuid,uuid_buf) == 0) { + if (jack_uuid_compare(client->GetClientControl()->fSessionID, uuid) == 0) { strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE); return 0; } @@ -1196,17 +1199,23 @@ int JackEngine::GetClientNameForUUID(const char *uuid, char *name_res) return -1; } -int JackEngine::ReserveClientName(const char *name, const char *uuid) +int JackEngine::ReserveClientName(const char *name, const char *uuidstr) { - jack_log("JackEngine::ReserveClientName ( name = %s, uuid = %s )", name, uuid); + jack_log("JackEngine::ReserveClientName ( name = %s, uuid = %s )", name, uuidstr); if (ClientCheckName(name)) { jack_log("name already taken"); return -1; } - EnsureUUID(atoi(uuid)); - fReservationMap[atoi(uuid)] = name; + jack_uuid_t uuid; + if (jack_uuid_parse(uuidstr, &uuid) != 0) { + jack_error("JackEngine::ReserveClientName invalid uuid %s", uuidstr); + return -1; + } + + EnsureUUID(uuid); + fReservationMap[uuid] = name; return 0; } diff --git a/common/JackMetadata.cpp b/common/JackMetadata.cpp index 43efc1a3..b690a01a 100644 --- a/common/JackMetadata.cpp +++ b/common/JackMetadata.cpp @@ -690,7 +690,7 @@ int JackMetadata::RemoveProperties(JackClient* client, jack_uuid_t subject) cursor->close (cursor); - if (cnt && client) { + if (cnt) { PropertyChangeNotify(client, subject, NULL, PropertyDeleted); } diff --git a/common/JackRequest.h b/common/JackRequest.h index e39f2097..8665ff87 100644 --- a/common/JackRequest.h +++ b/common/JackRequest.h @@ -161,15 +161,15 @@ struct JackClientCheckRequest : public JackRequest char fName[JACK_CLIENT_NAME_SIZE+1]; int fProtocol; int fOptions; - int fUUID; int fOpen; + jack_uuid_t fUUID; - JackClientCheckRequest() : fProtocol(0), fOptions(0), fUUID(0), fOpen(0) + JackClientCheckRequest() : fProtocol(0), fOptions(0), fOpen(0), fUUID(JACK_UUID_EMPTY_INITIALIZER) { memset(fName, 0, sizeof(fName)); } JackClientCheckRequest(const char* name, int protocol, int options, jack_uuid_t uuid, int open = false) - : JackRequest(JackRequest::kClientCheck), fProtocol(protocol), fOptions(options), fUUID(uuid), fOpen(open) + : JackRequest(JackRequest::kClientCheck), fProtocol(protocol), fOptions(options), fOpen(open), fUUID(uuid) { memset(fName, 0, sizeof(fName)); snprintf(fName, sizeof(fName), "%s", name); @@ -181,7 +181,7 @@ struct JackClientCheckRequest : public JackRequest CheckRes(trans->Read(&fName, sizeof(fName))); CheckRes(trans->Read(&fProtocol, sizeof(int))); CheckRes(trans->Read(&fOptions, sizeof(int))); - CheckRes(trans->Read(&fUUID, sizeof(int))); + CheckRes(trans->Read(&fUUID, sizeof(jack_uuid_t))); return trans->Read(&fOpen, sizeof(int)); } @@ -191,11 +191,11 @@ struct JackClientCheckRequest : public JackRequest CheckRes(trans->Write(&fName, sizeof(fName))); CheckRes(trans->Write(&fProtocol, sizeof(int))); CheckRes(trans->Write(&fOptions, sizeof(int))); - CheckRes(trans->Write(&fUUID, sizeof(int))); + CheckRes(trans->Write(&fUUID, sizeof(jack_uuid_t))); return trans->Write(&fOpen, sizeof(int)); } - int Size() { return sizeof(fName) + 4 * sizeof(int); } + int Size() { return sizeof(fName) + 3 * sizeof(int) + sizeof(jack_uuid_t); } }; @@ -246,10 +246,10 @@ struct JackClientOpenRequest : public JackRequest { int fPID; - int fUUID; + jack_uuid_t fUUID; char fName[JACK_CLIENT_NAME_SIZE+1]; - JackClientOpenRequest() : fPID(0), fUUID(0) + JackClientOpenRequest() : fPID(0), fUUID(JACK_UUID_EMPTY_INITIALIZER) { memset(fName, 0, sizeof(fName)); } @@ -265,7 +265,7 @@ struct JackClientOpenRequest : public JackRequest { CheckSize(); CheckRes(trans->Read(&fPID, sizeof(int))); - CheckRes(trans->Read(&fUUID, sizeof(int))); + CheckRes(trans->Read(&fUUID, sizeof(jack_uuid_t))); return trans->Read(&fName, sizeof(fName)); } @@ -273,11 +273,11 @@ struct JackClientOpenRequest : public JackRequest { CheckRes(JackRequest::Write(trans, Size())); CheckRes(trans->Write(&fPID, sizeof(int))); - CheckRes(trans->Write(&fUUID, sizeof(int))); + CheckRes(trans->Write(&fUUID, sizeof(jack_uuid_t))); return trans->Write(&fName, sizeof(fName)); } - int Size() { return 2 * sizeof(int) + sizeof(fName); } + int Size() { return sizeof(int) + sizeof(jack_uuid_t) + sizeof(fName); } }; @@ -1048,9 +1048,9 @@ struct JackInternalClientLoadRequest : public JackRequest char fDllName[MAX_PATH+1]; char fLoadInitName[JACK_LOAD_INIT_LIMIT+1]; int fOptions; - int fUUID; + jack_uuid_t fUUID; - JackInternalClientLoadRequest() : fRefNum(0), fOptions(0), fUUID(0) + JackInternalClientLoadRequest() : fRefNum(0), fOptions(0), fUUID(JACK_UUID_EMPTY_INITIALIZER) { memset(fName, 0, sizeof(fName)); memset(fDllName, 0, sizeof(fDllName)); @@ -1062,9 +1062,9 @@ struct JackInternalClientLoadRequest : public JackRequest memset(fName, 0, sizeof(fName)); memset(fDllName, 0, sizeof(fDllName)); memset(fLoadInitName, 0, sizeof(fLoadInitName)); - snprintf(fName, sizeof(fName), "%s", client_name); - snprintf(fDllName, sizeof(fDllName), "%s", so_name); - snprintf(fLoadInitName, sizeof(fLoadInitName), "%s", objet_data); + strncpy(fName, client_name, sizeof(fName)-1); + strncpy(fDllName, so_name, sizeof(fDllName)-1); + strncpy(fLoadInitName, objet_data, sizeof(fLoadInitName)-1); } int Read(detail::JackChannelTransactionInterface* trans) @@ -1074,7 +1074,7 @@ struct JackInternalClientLoadRequest : public JackRequest CheckRes(trans->Read(&fName, sizeof(fName))); CheckRes(trans->Read(&fDllName, sizeof(fDllName))); CheckRes(trans->Read(&fLoadInitName, sizeof(fLoadInitName))); - CheckRes(trans->Read(&fUUID, sizeof(int))); + CheckRes(trans->Read(&fUUID, sizeof(jack_uuid_t))); return trans->Read(&fOptions, sizeof(int)); } @@ -1085,11 +1085,11 @@ struct JackInternalClientLoadRequest : public JackRequest CheckRes(trans->Write(&fName, sizeof(fName))); CheckRes(trans->Write(&fDllName, sizeof(fDllName))); CheckRes(trans->Write(&fLoadInitName, sizeof(fLoadInitName))); - CheckRes(trans->Write(&fUUID, sizeof(int))); + CheckRes(trans->Write(&fUUID, sizeof(jack_uuid_t))); return trans->Write(&fOptions, sizeof(int)); } - int Size() { return sizeof(int) + sizeof(fName) + sizeof(fDllName) + sizeof(fLoadInitName) + 2 * sizeof(int); } + int Size() { return sizeof(int) + sizeof(fName) + sizeof(fDllName) + sizeof(fLoadInitName) + sizeof(int) + sizeof(jack_uuid_t); } }; /*! @@ -1233,9 +1233,9 @@ struct JackClientNotificationRequest : public JackRequest struct JackSessionCommand { - char fUUID[JACK_UUID_SIZE]; + char fUUID[JACK_UUID_STRING_SIZE]; char fClientName[JACK_CLIENT_NAME_SIZE+1]; - char fCommand[JACK_SESSION_COMMAND_SIZE]; + char fCommand[JACK_SESSION_COMMAND_SIZE+1]; jack_session_flags_t fFlags; JackSessionCommand() : fFlags(JackSessionSaveError) @@ -1303,8 +1303,8 @@ struct JackSessionNotifyResult : public JackResult return 0; } - char terminator[JACK_UUID_SIZE]; - terminator[0] = '\0'; + char terminator[JACK_UUID_STRING_SIZE]; + memset(terminator, 0, sizeof(terminator)); CheckRes(JackResult::Write(trans)); for (std::list::iterator i = fCommandList.begin(); i != fCommandList.end(); i++) { @@ -1363,11 +1363,9 @@ struct JackSessionNotifyRequest : public JackRequest { memset(fPath, 0, sizeof(fPath)); memset(fDst, 0, sizeof(fDst)); - snprintf(fPath, sizeof(fPath), "%s", path); - fPath[JACK_MESSAGE_SIZE] = 0; + strncpy(fPath, path, sizeof(fPath)-1); if (dst) { - snprintf(fDst, sizeof(fDst), "%s", dst); - fDst[JACK_CLIENT_NAME_SIZE] = 0; + strncpy(fDst, dst, sizeof(fDst)-1); } } @@ -1435,7 +1433,7 @@ struct JackClientNameResult : public JackResult : JackResult(result) { memset(fName, 0, sizeof(fName)); - snprintf(fName, sizeof(fName), "%s", name); + strncpy(fName, name, sizeof(fName)-1); } int Read(detail::JackChannelTransactionInterface* trans) @@ -1456,7 +1454,7 @@ struct JackClientNameResult : public JackResult struct JackUUIDResult : public JackResult { - char fUUID[JACK_UUID_SIZE]; + char fUUID[JACK_UUID_STRING_SIZE]; JackUUIDResult(): JackResult() { @@ -1466,7 +1464,7 @@ struct JackUUIDResult : public JackResult : JackResult(result) { memset(fUUID, 0, sizeof(fUUID)); - snprintf(fUUID, sizeof(fUUID), "%s", uuid); + strncpy(fUUID, uuid, sizeof(fUUID)-1); } int Read(detail::JackChannelTransactionInterface* trans) @@ -1521,7 +1519,7 @@ struct JackGetUUIDRequest : public JackRequest struct JackGetClientNameRequest : public JackRequest { - char fUUID[JACK_UUID_SIZE]; + char fUUID[JACK_UUID_STRING_SIZE]; JackGetClientNameRequest() { @@ -1557,7 +1555,7 @@ struct JackReserveNameRequest : public JackRequest { int fRefNum; char fName[JACK_CLIENT_NAME_SIZE+1]; - char fUUID[JACK_UUID_SIZE]; + char fUUID[JACK_UUID_STRING_SIZE]; JackReserveNameRequest() : fRefNum(0) { @@ -1697,8 +1695,10 @@ struct JackClientNotification { memset(fName, 0, sizeof(fName)); memset(fMessage, 0, sizeof(fMessage)); - snprintf(fName, sizeof(fName), "%s", name); - snprintf(fMessage, sizeof(fMessage), "%s", message); + strncpy(fName, name, sizeof(fName)-1); + if (message) { + strncpy(fMessage, message, sizeof(fMessage)-1); + } fSize = Size(); } diff --git a/common/varargs.h b/common/varargs.h index f4a50948..24fa7d6e 100644 --- a/common/varargs.h +++ b/common/varargs.h @@ -35,7 +35,7 @@ extern "C" char *server_name; /* server name */ char *load_name; /* load module name */ char *load_init; /* initialization string */ - int session_id; /* requested session_id */ + jack_uuid_t session_id; /* requested session_id */ } jack_varargs_t; @@ -51,7 +51,6 @@ extern "C" { memset (va, 0, sizeof(jack_varargs_t)); va->server_name = (char*)jack_default_server_name(); - va->session_id = -1; } static inline void jack_varargs_parse (jack_options_t options, va_list ap, jack_varargs_t *va) @@ -70,8 +69,11 @@ extern "C" va->load_init = va_arg(ap, char *); if ((options & JackSessionID)) { char *sid = va_arg(ap, char *); - if (sid) - va->session_id = atoi( sid ); + if (sid) { + const long long id = atoll( sid ); + if (id > 0) + va->session_id = id; + } } } diff --git a/example-clients/property.c b/example-clients/property.c index a7e35349..e0278b46 100644 --- a/example-clients/property.c +++ b/example-clients/property.c @@ -11,8 +11,8 @@ static int subject_is_client = 0; static int subject_is_port = 0; -static jack_uuid_t uuid; -static char* subject; +static jack_uuid_t uuid = JACK_UUID_EMPTY_INITIALIZER; +static char* subject = NULL; static void show_usage (void) @@ -46,7 +46,7 @@ get_subject (jack_client_t* client, char* argv[], int* optind) } if (jack_uuid_parse (ustr, &uuid)) { - fprintf (stderr, "cannot parse client UUID as UUID\n"); + fprintf (stderr, "cannot parse client UUID as UUID '%s' '%s'\n", cstr, ustr); return -1; } @@ -289,13 +289,13 @@ int main (int argc, char* argv[]) /* list all properties */ jack_description_t* description; - size_t cnt; + int cnt; size_t p; - size_t n; + int n; char buf[JACK_UUID_STRING_SIZE]; if ((cnt = jack_get_all_properties (&description)) < 0) { - fprintf (stderr, "could not retrieve properties for %s\n", subject); + fprintf (stderr, "could not retrieve all properties\n"); exit (1); } -- cgit v1.2.1