From: Mike Brady Date: Sun, 8 Apr 2018 11:36:00 +0000 (+0100) Subject: Use snprintf in place of sprintf and initialise mutexes for debugging. X-Git-Tag: 3.2RC2~2^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7fb8913532a7be943fef38ed98a91f541596dc07;p=thirdparty%2Fshairport-sync.git Use snprintf in place of sprintf and initialise mutexes for debugging. --- diff --git a/common.c b/common.c index 0a8eaa04..d545c16d 100644 --- a/common.c +++ b/common.c @@ -1009,7 +1009,7 @@ int sps_pthread_mutex_timedlock(pthread_mutex_t *mutex, useconds_t dally_time, useconds_t time_to_wait = dally_time; int r = pthread_mutex_trylock(mutex); - while ((r) && (time_to_wait > 0)) { + while ((r==EBUSY) && (time_to_wait > 0)) { useconds_t st = time_to_wait; if (st > 20000) st = 20000; @@ -1019,8 +1019,11 @@ int sps_pthread_mutex_timedlock(pthread_mutex_t *mutex, useconds_t dally_time, } if (r != 0) { char errstr[1000]; - debug(debuglevel, "error %d: \"%s\" waiting for a mutex: \"%s\".", r, - strerror_r(r, errstr, sizeof(errstr)), debugmessage); + if (r==EBUSY) + debug(debuglevel, "error EBUSY waiting for a mutex: \"%s\".", debugmessage); + else + debug(debuglevel, "error %d: \"%s\" waiting for a mutex: \"%s\".", r, + strerror_r(r, errstr, sizeof(errstr)), debugmessage); } return r; } diff --git a/dacp.c b/dacp.c index b0afb184..e22ac63a 100644 --- a/dacp.c +++ b/dacp.c @@ -113,8 +113,10 @@ static const struct http_funcs responseFuncs = { response_realloc, response_body, response_header, response_code, }; -static pthread_mutex_t dacp_conversation_lock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t dacp_server_information_lock = PTHREAD_MUTEX_INITIALIZER; +// static pthread_mutex_t dacp_conversation_lock = PTHREAD_MUTEX_INITIALIZER; +// static pthread_mutex_t dacp_server_information_lock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t dacp_conversation_lock; +static pthread_mutex_t dacp_server_information_lock; static pthread_cond_t dacp_server_information_cv = PTHREAD_COND_INITIALIZER; int dacp_send_command(const char *command, char **body, ssize_t *bodysize) { @@ -139,14 +141,14 @@ int dacp_send_command(const char *command, char **body, ssize_t *bodysize) { response.size = 0; response.code = 0; - char portstring[10], server[256], message[1024]; + char portstring[10], server[1024], message[1024]; memset(&portstring, 0, sizeof(portstring)); if (dacp_server.connection_family == AF_INET6) { - sprintf(server, "%s%%%u", dacp_server.ip_string, dacp_server.scope_id); + snprintf(server, sizeof(server), "%s%%%u", dacp_server.ip_string, dacp_server.scope_id); } else { strcpy(server, dacp_server.ip_string); } - sprintf(portstring, "%u", dacp_server.port); + snprintf(portstring, sizeof(portstring), "%u", dacp_server.port); // first, load up address structs with getaddrinfo(): @@ -192,9 +194,9 @@ int dacp_send_command(const char *command, char **body, ssize_t *bodysize) { } else { // debug(1,"DACP connect succeeded."); - sprintf(message, - "GET /ctrl-int/1/%s HTTP/1.1\r\nHost: %s:%u\r\nActive-Remote: %u\r\n\r\n", - command, dacp_server.ip_string, dacp_server.port, dacp_server.active_remote_id); + snprintf(message, sizeof(message), + "GET /ctrl-int/1/%s HTTP/1.1\r\nHost: %s:%u\r\nActive-Remote: %u\r\n\r\n", + command, dacp_server.ip_string, dacp_server.port, dacp_server.active_remote_id); // Send command // debug(1,"DACP connect message: \"%s\".",message); @@ -221,7 +223,7 @@ int dacp_send_command(const char *command, char **body, ssize_t *bodysize) { if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof tv) == -1) debug(1, "Error %d setting receive timeout for DACP service.", errno); int ndata = recv(sockfd, buffer, sizeof(buffer), 0); - // debug(1,"Received %d bytes: \"%s\".",ndata,buffer); + debug(3, "Received %d bytes: \"%s\".", ndata, buffer); if (ndata <= 0) { debug(1, "dacp_send_command -- error receiving response for command \"%s\".", command); @@ -422,7 +424,7 @@ void *dacp_monitor_thread_code(__attribute__((unused)) void *na) { else idle_scan_count = 0; - debug(3, "Scan Result: %d, Bad Scan Count: %d, Idle Scan Count: %d.", result, bad_result_count, + debug(2, "Scan Result: %d, Bad Scan Count: %d, Idle Scan Count: %d.", result, bad_result_count, idle_scan_count); if ((bad_result_count == config.scan_max_bad_response_count) || @@ -761,6 +763,49 @@ void *dacp_monitor_thread_code(__attribute__((unused)) void *na) { } void dacp_monitor_start() { + int rc; + pthread_mutexattr_t mta; + + rc = pthread_mutexattr_init(&mta); + if (rc) + debug(1, "Error creating the DACP Conversation Lock Mutex Att Init"); + + rc = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); + if (rc) + debug(1, "Error creating the DACP Conversation Lock Mutex Errorcheck"); + + // rc = pthread_mutexattr_setname_np(&mta, "DACP Conversation Lock"); + // if (rc) + // debug(1,"Error creating the DACP Conversation Lock Mutex Set Name"); + + rc = pthread_mutex_init(&dacp_conversation_lock, &mta); + if (rc) + debug(1, "Error creating the DACP Conversation Lock Mutex Init"); + + rc = pthread_mutexattr_destroy(&mta); + if (rc) + debug(1, "Error creating the DACP Conversation Lock Attr Destroy"); + + rc = pthread_mutexattr_init(&mta); + if (rc) + debug(1, "Error creating the DACP Server Information Lock Mutex Att Init"); + + rc = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); + if (rc) + debug(1, "Error creating the DACP Server Information Lock Mutex Errorcheck"); + + // rc = pthread_mutexattr_setname_np(&mta, "DACP Conversation Lock"); + // if (rc) + // debug(1,"Error creating the DACP Server Information Lock Mutex Set Name"); + + rc = pthread_mutex_init(&dacp_server_information_lock, &mta); + if (rc) + debug(1, "Error creating the DACP Server Information Lock Mutex Init"); + + rc = pthread_mutexattr_destroy(&mta); + if (rc) + debug(1, "Error creating the DACP Server Information Lock Attr Destroy"); + memset(&dacp_server, 0, sizeof(dacp_server_record)); pthread_create(&dacp_monitor_thread, NULL, dacp_monitor_thread_code, NULL); } @@ -821,8 +866,9 @@ int dacp_set_include_speaker_volume(int64_t machine_number, int32_t vo) { debug(2, "dacp_set_include_speaker_volume to %" PRId32 ".", vo); char message[1000]; memset(message, 0, sizeof(message)); - sprintf(message, "setproperty?include-speaker-id=%" PRId64 "&dmcp.volume=%" PRId32 "", - machine_number, vo); + snprintf(message, sizeof(message), + "setproperty?include-speaker-id=%" PRId64 "&dmcp.volume=%" PRId32 "", machine_number, + vo); debug(2, "sending \"%s\"", message); return send_simple_dacp_command(message); // should return 204 @@ -831,8 +877,8 @@ int dacp_set_include_speaker_volume(int64_t machine_number, int32_t vo) { int dacp_set_speaker_volume(int64_t machine_number, int32_t vo) { char message[1000]; memset(message, 0, sizeof(message)); - sprintf(message, "setproperty?speaker-id=%" PRId64 "&dmcp.volume=%" PRId32 "", machine_number, - vo); + snprintf(message, sizeof(message), "setproperty?speaker-id=%" PRId64 "&dmcp.volume=%" PRId32 "", + machine_number, vo); debug(2, "sending \"%s\"", message); return send_simple_dacp_command(message); // should return 204