-/* Number of active buffers and the mutex to protect it. */
-int active_buffers = 0;
-pthread_mutex_t active_buffers_mutex = PTHREAD_MUTEX_INITIALIZER;
-/* Whether a request to end the program was received. */
-sig_atomic_t terminate_req = 0;
-
-int get_subbuffer(struct buffer_info *buf)
-{
- char *send_msg=NULL;
- char *received_msg=NULL;
- char *rep_code=NULL;
- int retval;
- int result;
-
- asprintf(&send_msg, "get_subbuffer %s", buf->name);
- result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
- if(result == -1 && errno == EPIPE || result == 0) {
- DBG("app died while being traced");
- retval = GET_SUBBUF_DIED;
- goto end;
- }
- else if(result < 0) {
- ERR("get_subbuffer: ustcomm_send_request failed");
- retval = -1;
- goto end;
- }
-
- result = sscanf(received_msg, "%as %ld", &rep_code, &buf->consumed_old);
- if(result != 2 && result != 1) {
- ERR("unable to parse response to get_subbuffer");
- retval = -1;
- goto end_rep;
- }
-
- DBG("received msg is %s", received_msg);
-
- if(!strcmp(rep_code, "OK")) {
- DBG("got subbuffer %s", buf->name);
- retval = GET_SUBBUF_OK;
- }
- else if(nth_token_is(received_msg, "END", 0) == 1) {
- retval = GET_SUBBUF_DONE;
- goto end_rep;
- }
- else {
- DBG("error getting subbuffer %s", buf->name);
- retval = -1;
- }
-
- /* FIMXE: free correctly the stuff */
-end_rep:
- if(rep_code)
- free(rep_code);
-end:
- if(send_msg)
- free(send_msg);
- if(received_msg)
- free(received_msg);
-
- return retval;
-}
-
-int put_subbuffer(struct buffer_info *buf)
-{
- char *send_msg=NULL;
- char *received_msg=NULL;
- char *rep_code=NULL;
- int retval;
- int result;
-
- asprintf(&send_msg, "put_subbuffer %s %ld", buf->name, buf->consumed_old);
- result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
- if(result < 0 && errno == ECONNRESET) {
- retval = PUT_SUBBUF_DIED;
- goto end;
- }
- if(result < 0) {
- ERR("put_subbuffer: send_message failed");
- retval = -1;
- goto end;
- }
-
- result = sscanf(received_msg, "%as", &rep_code);
- if(result != 1) {
- ERR("unable to parse response to put_subbuffer");
- retval = -1;
- goto end_rep;
- }
-
- if(!strcmp(rep_code, "OK")) {
- DBG("subbuffer put %s", buf->name);
- retval = PUT_SUBBUF_OK;
- }
- else {
- DBG("put_subbuffer: received error, we were pushed");
- retval = PUT_SUBBUF_PUSHED;
- goto end_rep;
- }
-
-end_rep:
- if(rep_code)
- free(rep_code);
-
-end:
- if(send_msg)
- free(send_msg);
- if(received_msg)
- free(received_msg);