示例#1
0
int main(int argc, char *argv[])
{
    optiondata  opt;
    motparm     mp;  
    port_list * plist = &g_ports;
    int         rv;

    if ( (rv = get_options(&opt, &mp, plist, argc, argv)) != 0 )
        return rv;
    
    /* register interrupt trap */
    signal( SIGHUP,  clean_n_exit );
    signal( SIGINT,  clean_n_exit );
    signal( SIGQUIT, clean_n_exit );
    signal( SIGKILL, clean_n_exit );
    signal( SIGTERM, clean_n_exit );
    
    if ( (rv = open_incoming_socket(&opt, plist)) < 0 )
        return rv;

    while (1)           /* run until interrupt or error (consider restart?) */
    {
        mp.nread = 0;           /* reset our counter */

        /* wait for AFNI to talk to us */
        if ( (rv = wait_for_socket(&opt, plist, &mp)) < 0 ) {
            clean_n_exit(0);
            return rv;
        }

        if ( ! opt.no_serial )
            if ( (rv = open_serial(&opt, plist)) != 0 )
                return rv;

        /* read data while it is there */
        while ( (rv = read_socket(&opt, plist, &mp)) == 0)
            if ( ! opt.no_serial )
                send_serial(&opt, plist, &mp);

        close_data_ports(plist);
    } 

    return 0;   /* should not be reached, of course */
}
示例#2
0
void update_interfaces()
{
    struct int_sock *new_list=NULL;
    struct int_sock *i=NULL,*j,*prev;
    struct int_sock *new_int=NULL;
    int i_fd,o_fd;

    /* if all interfaces are '+'-interfaces, we bridge broadcast */
    do_broadcast_bridging=1;
    update_interface_trigger=0;
    syslog(LOG_DEBUG,"rechecking interfaces");

    if (read_sys(&new_list))
        return;

    for (i=select_set.i_s_list; i; i=i->next) {
        if (!interface_in_list(i,new_list)) {
            /* remove interface i */
            j=select_set.i_s_list;
            prev=NULL;
            while (j) {
                if (!strncmp(j->dev_name,i->dev_name,
                             DEV_NAME_LEN)) {
                    if (!prev) {
                        select_set.i_s_list=j->next;
                    } else {
                        prev->next=j->next;
                    }
                    prev=j;
                    j=j->next;
                    free(j);
                } else {
                    j=j->next;
                }
            }
            /* and close the socket */
            close(i->i_fd);
            close(i->o_fd);
            syslog(LOG_INFO,"removed interface %s",i->dev_name);
        }
    }

    for (i=new_list; i; i=i->next) {
        if (!interface_in_list(i,select_set.i_s_list)) {
            /* add interface i */
            new_int=malloc(sizeof(struct int_sock));
            if (!new_int) {
                syslog(LOG_ERR,"can't add interface %s -- " \
                       "no memory",i->dev_name);
                continue;
            }

            i_fd=open_incoming_socket(i->dev_name);
            if (i_fd==-1) {
                free(new_int);
                continue;
            }

            o_fd=open_outgoing_socket(i->dev_name);
            if (o_fd==-1) {
                close(i_fd);
                free(new_int);
                continue;
            }

            strncpy(new_int->dev_name,i->dev_name,DEV_NAME_LEN);
            new_int->i_fd=i_fd;
            new_int->o_fd=o_fd;
            new_int->features=i->features;
            new_int->next=select_set.i_s_list;
            select_set.i_s_list=new_int;
            syslog(LOG_INFO,"added interface %s",i->dev_name);
        }
    }

    /* kill temporary new_list */
    while (new_list) {
        i=new_list->next;
        free(new_list);
        new_list=i;
    }

    /* prepare the fd_set for select */
    FD_ZERO(&select_set.fds);
    for (i=select_set.i_s_list; i; i=i->next) {
        FD_SET(i->i_fd,&select_set.fds);
        select_set.highest_fd=(i->i_fd>select_set.highest_fd)?
                              i->i_fd:select_set.highest_fd;
    }
}