static pthread_mutex_t dacp_server_information_lock;
static pthread_cond_t dacp_server_information_cv = PTHREAD_COND_INITIALIZER;
+void addrinfo_cleanup(void *arg) {
+ debug(1, "addrinfo cleanup called.");
+ struct addrinfo **info = (struct addrinfo **)arg;
+ freeaddrinfo(*info);
+}
+
+void mutex_lock_cleanup(void *arg) {
+ debug(1, "mutex lock cleanup called.");
+ pthread_mutex_t *m = (pthread_mutex_t *)arg;
+ pthread_mutex_unlock(m);
+}
+
+void connect_cleanup(void *arg) {
+ debug(1, "connect cleanup called.");
+ int *fd = (int *)arg;
+ close(*fd);
+}
+
+void http_cleanup(void *arg) {
+ debug(1, "http cleanup called.");
+ struct http_roundtripper *rt = (struct http_roundtripper *)arg;
+ http_free(rt);
+}
+
+void malloc_cleanup(void *arg) {
+ debug(1, "malloc cleanup called.");
+ free(arg);
+}
+
int dacp_send_command(const char *command, char **body, ssize_t *bodysize) {
// will malloc space for the body or set it to NULL -- the caller should free it.
// debug(1,"Error %d \"%s\" at getaddrinfo.",ires,gai_strerror(ires));
response.code = 498; // Bad Address information for the DACP server
} else {
-
+ pthread_cleanup_push(addrinfo_cleanup, (void *)&res);
// only do this one at a time -- not sure it is necessary, but better safe than sorry
int mutex_reply = sps_pthread_mutex_timedlock(&dacp_conversation_lock, 2000000, command, 1);
// int mutex_reply = pthread_mutex_lock(&dacp_conversation_lock);
if (mutex_reply == 0) {
+ pthread_cleanup_push(mutex_lock_cleanup, (void *)&dacp_conversation_lock);
// debug(1,"dacp_conversation_lock acquired for command \"%s\".",command);
// make a socket:
debug(3, "DACP connect failed with errno %d.", errno);
response.code = 496; // Can't connect to the DACP server
} else {
+ pthread_cleanup_push(connect_cleanup, (void *)&sockfd);
// debug(1,"DACP connect succeeded.");
snprintf(message, sizeof(message),
response.body = malloc(2048); // it can resize this if necessary
response.malloced_size = 2048;
+ pthread_cleanup_push(malloc_cleanup, response.body);
struct http_roundtripper rt;
http_init(&rt, responseFuncs, &response);
+ pthread_cleanup_push(http_cleanup, &rt);
int needmore = 1;
int looperror = 0;
response.size = 0;
}
// debug(1,"Size of response body is %d",response.size);
- http_free(&rt);
+ pthread_cleanup_pop(1); // this should call http_cleanup
+ // http_free(&rt);
+ pthread_cleanup_pop(
+ 0); // this should *not* free the malloced buffer -- just pop the malloc cleanup
}
+ pthread_cleanup_pop(1); // this should close the socket
+ // close(sockfd);
+ // debug(1,"DACP socket closed.");
}
- close(sockfd);
- // debug(1,"DACP socket closed.");
}
- pthread_mutex_unlock(&dacp_conversation_lock);
+ pthread_cleanup_pop(1); // this should unlock the dacp_conversation_lock);
+ // pthread_mutex_unlock(&dacp_conversation_lock);
// debug(1,"Sent command\"%s\" with a response body of size %d.",command,response.size);
// debug(1,"dacp_conversation_lock released.");
} else {
command);
response.code = 494; // This client is already busy
}
+ pthread_cleanup_pop(1); // this should free the addrinfo
+ // freeaddrinfo(res);
}
*body = response.body;
*bodysize = response.size;
sp -= item_size;
le -= 8;
speaker_index++;
- if (speaker_index == max_size_of_array)
+ if (speaker_index == max_size_of_array) {
return 413; // Payload Too Large -- too many speakers
+ }
speaker_info[speaker_index].active = 0;
speaker_info[speaker_index].speaker_number = 0;
speaker_info[speaker_index].volume = 0;
- speaker_info[speaker_index].name = NULL;
+ speaker_info[speaker_index].name[0] = '\0';
} else {
le -= item_size + 8;
char *t;
switch (type) {
case 'minm':
t = sp - item_size;
- speaker_info[speaker_index].name = strndup(t, item_size);
- // debug(1," \"%s\"",speaker_info[speaker_index].name);
+ strncpy((char *)&speaker_info[speaker_index].name, t,
+ sizeof(speaker_info[speaker_index].name));
+ speaker_info[speaker_index].name[sizeof(speaker_info[speaker_index].name) - 1] =
+ '\0'; // just in case
break;
case 'cmvo':
t = sp - item_size;