apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marco Spinetti <m.spine...@pisa.iol.it>
Subject Re: FW: RedHat Linux 7.1 Problem apr
Date Tue, 13 Jan 2004 13:44:55 GMT
Sorry Jeff,

you are right.

My code is:

void *
io_thread_main(void *arg)
{
    apr_port_t num_porta;
    apr_pool_t *context;
    apr_socket_t *sock;
    apr_size_t length;
    apr_status_t stat;
    apr_interval_time_t read_timeout;
    apr_sockaddr_t *local_sa, *remote_sa;
    char msgbuf[80];
    char *receive;
    Thread_struct *p;
    int num_byte_query = 0;

    Pthread_detach(pthread_self());
   
    p = (Thread_struct *) arg;
   
    read_timeout = apr_time_from_sec(p->timeout);
   
    if (read_timeout <= 0) {
        fprintf(stderr, "[ERRORE][io_thread_main] Errore nella 
get_thread_timeout\n");
        read_timeout = apr_time_from_sec(5);
    }
   
    fprintf(stderr, "[io_thread_main] Timeout %s(%s) = %d\n", p->host, 
p->porta, read_timeout);
   
    if (apr_initialize() != APR_SUCCESS) {
        fprintf(stderr, "[ERRORE][io_thread_main] apr_initialize 
fallita\n");
        p->status = -1;
        io_rilascio_risorse(p);
        goto FINE1;
    }
   
    atexit(apr_terminate);

    if (apr_pool_create(&context, NULL) != APR_SUCCESS) {
        fprintf(stderr, "[ERRORE][io_thread_main] apr_initialize 
fallita\n");
        p->status = -1;
        io_rilascio_risorse(p);
        goto FINE1;
    }
   
    num_porta = (apr_port_t)strtol(p->porta, (char **)NULL, 10);
   
    if ((stat = apr_sockaddr_info_get(&remote_sa, p->host, APR_UNSPEC, 
num_porta, APR_IPV4_ADDR_OK, context)) != APR_SUCCESS) {
        fprintf(stderr, "[ERRORE][io_thread_main] Address resolution 
failed for %s: %s\n", p->host, apr_strerror(stat, msgbuf, sizeof(msgbuf)));
        p->status = -1;
        io_rilascio_risorse(p);
        goto FINE1;
    }
   
    if (apr_socket_create(&sock, remote_sa->family, SOCK_STREAM, 
APR_PROTO_TCP, context) != APR_SUCCESS) {
        fprintf(stderr, "[ERRORE][io_thread_main] apr_socket_create 
fails\n");
        p->status = -1;
        io_rilascio_risorse(p);
        goto FINE1;
    }

    stat = apr_socket_connect(sock, remote_sa);

    if (stat != APR_SUCCESS) {
        apr_socket_close(sock);
        fprintf(stderr, "[ERRORE][io_thread_main] Could not connect 
(%s): %s (%d)\n", p->host, apr_strerror(stat, msgbuf, sizeof(msgbuf)), 
stat);
       
        p->status = -2;
        io_rilascio_risorse(p);
        goto FINE1;
    }
   
    length = strlen(p->query) + 1;
    num_byte_query = (int) length;
   
    if (apr_socket_send(sock, p->query, &length) != APR_SUCCESS) {
        apr_socket_close(sock);
        fprintf(stderr, "[ERRORE][io_thread_main] apr_send fallita\n");
        p->status = -1;
        io_rilascio_risorse(p);
        goto FINE1;
    }
   
    p->out = (char *) malloc(RET_MAX * sizeof(char));
    bzero(p->out, RET_MAX * sizeof(char));
    p->out_len = RET_MAX * sizeof(char);
   
    receive = (char *) malloc(MAXLINE * sizeof(char));
    if (receive == NULL) {
        apr_socket_close(sock);
        p->status = -1;
        io_rilascio_risorse(p);
        goto FINE1;
    }
   
    bzero(receive, MAXLINE * sizeof(char));
    length = MAXLINE * sizeof(char);
   
    /*
    stat = apr_socket_timeout_set(sock, read_timeout);
    if (stat) {
        fprintf(stderr, "[ERRORE][io_thread_main] apr_socket_opt_set 
fallita (%d)\n", stat);
    }
    */
   
    while ((!APR_STATUS_IS_EOF(stat = apr_socket_recv(sock, receive, 
&length))) && (!APR_STATUS_IS_TIMEUP(stat))) {
        //fprintf(stderr, "Receive = %s\n", receive);
        strcat(p->out, receive);
        bzero(receive, strlen(receive) + 1);   
    }
   
    if (receive[0] != '\0')
        strcat(p->out, receive);
       
    if (APR_STATUS_IS_EOF(stat)) {
        p->status = 0;
        p->out_len = strlen(p->out) + 1;
    }
    else if (APR_STATUS_IS_TIMEUP(stat)) {
        p->status = -4;
        bzero(p->out, strlen(p->out)+1);
        p->out_len = 0;
    }
    else {
        p->status = -1;
        bzero(p->out, strlen(p->out)+1);
        p->out_len = 0;
    }
   
    free(receive);
       
    if (apr_socket_shutdown(sock, APR_SHUTDOWN_WRITE) != APR_SUCCESS) {
        apr_socket_close(sock);
        fprintf(stderr, "[ERRORE][io_thread_main] apr_shutdown fallita\n");
       
        io_rilascio_risorse(p);
        goto FINE1;
    }

    if (apr_socket_close(sock) != APR_SUCCESS) {
        fprintf(stderr, "[ERRORE][io_thread_main] apr_socket_close 
fallita\n");
       
        io_rilascio_risorse(p);
        goto FINE1;
    }
       
    pthread_cleanup_push(pthread_mutex_unlock, p->mutex_conn);
    if (Pthread_mutex_lock(p->mutex_conn) == -1) {
        fprintf(stderr, "[ERRORE][io_thread_main] Pthread_mutex_lock 
fallita\n");
        goto CLEANUP;
    }
   
    fprintf(stderr, "Num_conn Prima = %d\n", *(p->nconn_max));
    *(p->nconn_max) = *(p->nconn_max) - 1;
    fprintf(stderr, "Num_conn Dopo = %d\n", *(p->nconn_max));
   
    if (*(p->nconn_max) <= 0)
        Pthread_cond_signal(p->cond_conn);
   
    if (Pthread_mutex_unlock(p->mutex_conn) == -1) {
        fprintf(stderr, "[ERRORE][io_thread_main] Pthread_mutex_unlock 
fallita\n");
        goto CLEANUP;
    }
   
    CLEANUP:
    pthread_cleanup_pop(0);   
    FINE1:
    fprintf(stderr, "[lib_mod_io.c] Out da %s = %s\n", p->host, p->out);
}


I could substitute the " if" with:

do {
        stat = apr_socket_send(sock, p->query, &length);
        num_byte_query = num_byte_query - length;
        length = num_byte_query;   
    } while (num_byte_query != 0);

but I don't know if it is correct. 

Are you advising me to test (stat != APR_SUCCESS) instead of 
num_byte_query != 0?


Thanks for your help


--Marco



Jeff Trawick wrote:

> Marco Spinetti wrote:
>
>> there is another information: really my fprint is outside if: so 
>> apr_socket_send return APR_SUCCESS but the number of bytes written is 0.
>
>
> Marco, you're not making this easy for us to help you.  Please show 
> the real code your using, and check return codes from all APR 
> functions rather than just checking to see what the length parameter 
> was updated with.
>
>



Mime
View raw message