apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From SAMEER KUMAR <sameer.kasi2...@gmail.com>
Subject How to pass a variable (socket) from a program to a child process (which will execute a new program)
Date Thu, 16 Apr 2009 10:17:01 GMT
How to pass a variable (socket) from a program to a child process (which
will execute a new program)

While using apr_proc_create, how can I pass a variable to the new process
(which is executing a new binray file)

The code I am using is as below...

*file name: echo_listner.c*
*
#include "apr.h"
#include "apr_network_io.h"
#include"apr_thread_proc.h"
#include <stdlib.h>
#include <stdio.h>
#define MAXLINE 100
#define MAXN 100

void new_conn(apr_socket_t **new_sock);
int i=0;
int port=4754;


apr_status_t serve(apr_pool_t *pool)
{
    apr_status_t rv = APR_SUCCESS;
    apr_sockaddr_t *saddr;
    apr_pool_t *subpool;
    apr_socket_t *lskt;
    apr_socket_t *cskt;

//Resources for new process for new connection
    apr_proc_t   new_proc;
    apr_procattr_t  *attr;
    apr_pool_t  *attr_pool, *new_proc_pool;
    apr_thread_start_t func=new_conn(apr_socket_t **);

    for (;;i++) {

    rv = apr_socket_create(&lskt, APR_INET, SOCK_STREAM, APR_PROTO_TCP,
                           pool);
    if (rv)
       return rv;

    rv = apr_sockaddr_info_get(&saddr, "127.0.0.1", APR_UNSPEC, port, 0,
                               pool);
    port++;
    if (rv)
        return rv;

    rv = apr_socket_bind(lskt, saddr);
    if (rv)
            return rv;


    rv = apr_socket_listen(lskt, 5);

    if (rv)
        return rv;

        apr_pool_create(&subpool, pool);
        apr_pool_clear(subpool);

    rv = apr_socket_accept(&cskt, lskt, subpool);
    if(rv)
        return rv;


    printf("\nServer's instance no %d will be created\n", i);

    apr_pool_create(&attr_pool, NULL);
    apr_pool_create(&new_proc_pool,subpool);

    rv= apr_procattr_create (&attr,attr_pool);
    if(rv)
        return rv;
    //printf("\nNew pattr created\n");

    rv = apr_procattr_cmdtype_set(attr,APR_PROGRAM) ;
    if(rv)
        return rv;
    //printf("\nCommand type assigned to execute binary program\n");

    rv = apr_procattr_detach_set(attr, APR_PROC_DETACH_DAEMONIZE);
    //printf("\nDetatch done succeffully\n");
    if(rv)
        return rv;

    rv = apr_socket_inherit_set(cskt);
    if(rv)
        return rv;
    //printf("\nSocket can be now inherited by child\n");


    rv = apr_proc_create (&new_proc,"new_conn",NULL,NULL,
attr,new_proc_pool);
    if(rv)
        return rv;
    printf("\nNew process was created\n");

    rv = apr_socket_close(lskt);
    if(rv)
        return rv;

    continue;

   }

    return rv;
}

int main(int argc, const char * const argv[])
{
    apr_pool_t *pool;
    apr_status_t rv;
    int type=0;

    apr_initialize();
    atexit(apr_terminate);

    apr_pool_create(&pool, NULL);


    rv = serve(pool);
    if (rv) {
        char buffer[256] = { 0 };

        fprintf(stdout, "error: %s\n",
                apr_strerror(rv, buffer, sizeof(buffer)));
    printf("\n There is an error");

        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}*

*file: new_conn.c*
*
#include "apr.h"
#include "apr_network_io.h"
#include <stdlib.h>
#include <stdio.h>
#define MAXLINE 100
#define MAXN 100

extern apr_socket_t *cskt;

int main(int argc, char *argv[])
{


    int    ntowrite;
    ssize_t    nread;
    char    line[MAXLINE], result[MAXN];
    apr_status_t rv;
    apr_size_t len = 100;
        char c;
        printf("\n in new proc I have reached here successfully\n");

    for (;;) {

           rv = apr_socket_recv(cskt, &c, &len);
            if (APR_STATUS_IS_EOF(rv))
                break;
            else if (rv)
                break;
            else if (! len)
                break;

            rv = apr_socket_send(cskt, &c, &len);
            if (APR_STATUS_IS_EOF(rv))
                break;
            else if (rv)
                break;
            else if (! len)
                break;
        }

}


*I want to pass cskt from the echo_listner to the new_conn which will be
executed in new process created in echo_listner. I have made cskt
inheritable. How can I pass it to new process? If i pass it straightway, it
will show error/warning related to type conversion.

Regards,
Sameer Kumar
B.Tech, Computer Engineering
Institute of Technology,
Nirma University,
Ahmedabad, Gujarat
email: sameer.kasi200x@gmail.com
         05bce032@nirmauni.ac.in
Phone: +919375220396

Mime
View raw message