Changeset 2614 for neercs/trunk


Ignore:
Timestamp:
Jul 31, 2008, 1:34:55 AM (12 years ago)
Author:
Pascal Terjan
Message:
  • Factorize some code in attach.c
Location:
neercs/trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • neercs/trunk/src/attach.c

    r2611 r2614  
    1717#include "neercs.h"
    1818
    19 char * build_socket_path(char *socket_dir, char *session_name, int client)
     19char * build_socket_path(char *socket_dir, char *session_name, enum socket_type socktype)
    2020{
    2121    char *path, *dir;
     
    2929        dir = "/tmp";
    3030    if(path)
    31         snprintf(path, PATH_MAX+1, "%s/neercs.%s%s.sock", dir, session_name, client?"":".srv");
     31        snprintf(path, PATH_MAX+1, "%s/neercs.%s%s.sock", dir, session_name, socktype?"":".srv");
    3232    return path;
    3333}
    3434
    35 int create_client_socket(struct screen_list* screen_list)
     35int create_socket(struct screen_list* screen_list, enum socket_type socktype)
    3636{
    3737    int sock;
     
    4242    if(sock < 0)
    4343    {
    44         perror("create_client_socket:socket");
     44        perror("create_socket:socket");
    4545        return errno;
    4646    }
     
    4949
    5050    myaddr.sun_family = AF_UNIX;
    51     strncpy(myaddr.sun_path, screen_list->c_socket_path, sizeof(myaddr.sun_path) - 1);
     51    strncpy(myaddr.sun_path, screen_list->socket_path[socktype], sizeof(myaddr.sun_path) - 1);
    5252
    53     unlink(screen_list->c_socket_path);
     53    unlink(screen_list->socket_path[socktype]);
    5454
    5555    if(bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr_un)) < 0)
    5656    {
    57         free(screen_list->c_socket_path);
    58         screen_list->c_socket_path = NULL;
    59         close(sock);
    60         perror("create_client_socket:bind");
    61         return errno;
    62     }
    63     fcntl(sock, F_SETFL, O_NONBLOCK);
    64 
    65     debug("Client listening on %s (%d)", screen_list->c_socket_path, sock);
    66 
    67     screen_list->c_socket = sock;
    68 
    69     return 0;
    70 }
    71 
    72 int create_server_socket(struct screen_list* screen_list)
    73 {
    74     int sock;
    75     struct sockaddr_un myaddr;
    76 
    77     sock = socket(AF_UNIX, SOCK_DGRAM, 0);
    78 
    79     if(sock < 0)
    80     {
    81         perror("create_server_socket:socket");
    82         return errno;
    83     }
    84 
    85     memset(&myaddr, 0, sizeof(struct sockaddr_un));
    86 
    87     myaddr.sun_family = AF_UNIX;
    88     strncpy(myaddr.sun_path, screen_list->s_socket_path, sizeof(myaddr.sun_path) - 1);
    89 
    90     unlink(screen_list->s_socket_path);
    91 
    92     if(bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr_un)) < 0)
    93     {
    94         free(screen_list->s_socket_path);
    95         screen_list->s_socket_path = NULL;
     57        free(screen_list->socket_path[socktype]);
     58        screen_list->socket_path[socktype] = NULL;
    9659        close(sock);
    9760        perror("create_socket:bind");
     
    10063    fcntl(sock, F_SETFL, O_NONBLOCK);
    10164
    102     debug("Server listening on %s (%d)", screen_list->s_socket_path, sock);
     65    debug("Listening on %s (%d)", screen_list->socket_path[socktype], sock);
    10366
    104     screen_list->s_socket = sock;
     67    screen_list->socket[socktype] = sock;
    10568
    10669    return 0;
     
    142105}
    143106
    144 char * connect_server(struct screen_list* screen_list)
     107char * connect_socket(struct screen_list* screen_list, enum socket_type socktype)
    145108{
    146109    int sock;
     
    148111    char *p, *s;
    149112
    150     debug("Connecting to %s", screen_list->s_socket_path);
     113    debug("Connecting to %s", screen_list->socket_path[socktype]);
    151114
    152115    /* Open the socket */
     
    158121    memset(&addr,0,sizeof(addr));
    159122    addr.sun_family = AF_UNIX;
    160     strcpy(addr.sun_path,screen_list->s_socket_path);
     123    strcpy(addr.sun_path,screen_list->socket_path[socktype]);
    161124    if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    162125    {
    163         fprintf(stderr, "Failed to connect to %s: %s\n", screen_list->s_socket_path, strerror(errno));
     126        fprintf(stderr, "Failed to connect to %s: %s\n", screen_list->socket_path[socktype], strerror(errno));
    164127        return NULL;
    165128    }
    166129    fcntl(sock, F_SETFL, O_NONBLOCK);
    167130
    168     screen_list->s_socket = sock;
     131    screen_list->socket[socktype] = sock;
    169132
    170     p = strrchr(screen_list->s_socket_path, '/');
    171     p+=8; /* skip neercs. */
    172     s = strdup(p);
    173     p = strrchr(s, '.');
    174     *p = '\0'; /* drop .sock */
    175     p = strrchr(s, '.');
    176     *p = '\0'; /* drop .srv */
    177     p = strdup(s);
    178     free(s);
    179     return p;
    180 }
    181 
    182 int connect_client(struct screen_list* screen_list)
    183 {
    184     int sock;
    185     struct sockaddr_un addr;
    186 
    187     debug("Connecting to %s", screen_list->c_socket_path);
    188 
    189     /* Open the socket */
    190     if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
    191         perror("connect_client:socket");
    192         return 1;
     133    if(socktype == SOCK_SERVER)
     134    {
     135        p = strrchr(screen_list->socket_path[socktype], '/');
     136        p+=8; /* skip neercs. */
     137        s = strdup(p);
     138        p = strrchr(s, '.');
     139        *p = '\0'; /* drop .sock */
     140        p = strrchr(s, '.');
     141        *p = '\0'; /* drop .srv */
     142        p = strdup(s);
     143        free(s);
     144        return p;
    193145    }
    194 
    195     memset(&addr,0,sizeof(addr));
    196     addr.sun_family = AF_UNIX;
    197     strcpy(addr.sun_path,screen_list->c_socket_path);
    198     if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    199     {
    200         fprintf(stderr, "Failed to connect to %s: %s\n", screen_list->c_socket_path, strerror(errno));
    201         return 1;
    202     }
    203     fcntl(sock, F_SETFL, O_NONBLOCK);
    204 
    205     screen_list->c_socket = sock;
    206 
    207     return 0;
     146    else
     147        return NULL;
    208148}
    209149
     
    219159    buf[bytes] = '\0';
    220160    debug("Requesting attach: %s", buf);
    221     return write(screen_list->s_socket, buf, strlen(buf)) <= 0;
     161    return write(screen_list->socket[SOCK_SERVER], buf, strlen(buf)) <= 0;
    222162}
    223163
     
    235175        buf[bytes] = '\0';
    236176        debug("Sending key press to server: %s", buf);
    237         return write(screen_list->s_socket, buf, strlen(buf)) <= 0;
     177        return write(screen_list->socket[SOCK_SERVER], buf, strlen(buf)) <= 0;
    238178    }
    239179    else if(t & CACA_EVENT_RESIZE)
     
    247187        buf[bytes] = '\0';
    248188        debug("Sending resize to server: %s", buf);
    249         return write(screen_list->s_socket, buf, strlen(buf)) <= 0;
     189        return write(screen_list->socket[SOCK_SERVER], buf, strlen(buf)) <= 0;
    250190    }
    251191    else if(t & CACA_EVENT_QUIT)
    252         return write(screen_list->s_socket, "QUIT", strlen("QUIT")) <= 0;
     192        return write(screen_list->socket[SOCK_SERVER], "QUIT", strlen("QUIT")) <= 0;
    253193
    254194    return 0;
  • neercs/trunk/src/main.c

    r2613 r2614  
    261261            for(i=0; sockets[i]; i++);
    262262            i--;
    263             screen_list->s_socket_path = strdup(sockets[i]);
    264             session = connect_server(screen_list);
     263            screen_list->socket_path[SOCK_SERVER] = strdup(sockets[i]);
     264            session = connect_socket(screen_list, SOCK_SERVER);
    265265            while(!session && i > 0)
    266266            {
    267                 free(screen_list->s_socket_path);
     267                free(screen_list->socket_path[SOCK_SERVER]);
    268268                i--;
    269                 screen_list->s_socket_path = strdup(sockets[i]);
    270                 session = connect_server(screen_list);
     269                screen_list->socket_path[SOCK_SERVER] = strdup(sockets[i]);
     270                session = connect_socket(screen_list, SOCK_SERVER);
    271271            }
    272272            debug("Connected to session %s", session);
     
    280280                caca_set_cursor(screen_list->dp, 1);
    281281
    282                 screen_list->c_socket_path = build_socket_path(screen_list->socket_dir, session, 1);
    283                 create_client_socket(screen_list);
     282                screen_list->socket_path[SOCK_CLIENT] =
     283                    build_socket_path(screen_list->socket_dir, session, SOCK_CLIENT);
     284                create_socket(screen_list, SOCK_CLIENT);
    284285                request_attach(screen_list);
    285286                if(screen_list->session_name)
     
    290291            {
    291292                fprintf(stderr, "Failed to attach!\n");
    292                 free(screen_list->s_socket_path);
    293                 screen_list->s_socket_path = NULL;
     293                free(screen_list->socket_path[SOCK_SERVER]);
     294                screen_list->socket_path[SOCK_SERVER] = NULL;
    294295                attach = 0;
    295296            }
     
    320321        }
    321322    }
    322     if(!screen_list->c_socket_path)
    323         screen_list->c_socket_path =
    324             build_socket_path(screen_list->socket_dir, screen_list->session_name, 1);
    325 
    326     if(!screen_list->s_socket_path)
    327         screen_list->s_socket_path =
    328             build_socket_path(screen_list->socket_dir, screen_list->session_name, 0);
     323    if(!screen_list->socket_path[SOCK_CLIENT])
     324        screen_list->socket_path[SOCK_CLIENT] =
     325            build_socket_path(screen_list->socket_dir, screen_list->session_name, SOCK_CLIENT);
     326
     327    if(!screen_list->socket_path[SOCK_SERVER])
     328        screen_list->socket_path[SOCK_SERVER] =
     329            build_socket_path(screen_list->socket_dir, screen_list->session_name, SOCK_SERVER);
    329330
    330331    /* Fork the server if needed */
     
    365366            return server_main(to_grab, to_start, screen_list);
    366367        }
    367         create_client_socket(screen_list);
    368         while((sess = connect_server(screen_list)) == NULL)
     368        create_socket(screen_list, SOCK_CLIENT);
     369        while((sess = connect_socket(screen_list, SOCK_SERVER)) == NULL)
    369370            usleep(100);
    370371        free(sess);
     
    389390        char buf[128*1024];
    390391
    391         if (screen_list->c_socket && (n = read(screen_list->c_socket, buf, sizeof(buf)-1)) > 0)
     392        if (screen_list->socket[SOCK_CLIENT] && (n = read(screen_list->socket[SOCK_CLIENT], buf, sizeof(buf)-1)) > 0)
    392393        {
    393394            buf[n] = 0;
     
    445446        }
    446447
    447         if(screen_list->s_socket_path)
    448             free(screen_list->s_socket_path);
    449 
    450         if(screen_list->c_socket_path)
    451         {
    452             unlink(screen_list->c_socket_path);
    453             free(screen_list->c_socket_path);
    454         }
    455 
    456         if(screen_list->c_socket)
    457             close(screen_list->c_socket);
    458 
    459         if(screen_list->s_socket)
    460             close(screen_list->s_socket);
     448        if(screen_list->socket_path[SOCK_SERVER])
     449            free(screen_list->socket_path[SOCK_SERVER]);
     450
     451        if(screen_list->socket_path[SOCK_CLIENT])
     452        {
     453            unlink(screen_list->socket_path[SOCK_CLIENT]);
     454            free(screen_list->socket_path[SOCK_CLIENT]);
     455        }
     456
     457        if(screen_list->socket[SOCK_CLIENT])
     458            close(screen_list->socket[SOCK_CLIENT]);
     459
     460        if(screen_list->socket[SOCK_SERVER])
     461            close(screen_list->socket[SOCK_SERVER]);
    461462
    462463        if(screen_list->screen)
     
    529530    screen_list->lock_offset = 0;
    530531    screen_list->attached = 1;
    531     screen_list->s_socket = 0;
    532     screen_list->c_socket = 0;
     532    screen_list->socket[SOCK_SERVER] = 0;
     533    screen_list->socket[SOCK_CLIENT] = 0;
    533534    screen_list->socket_dir    = NULL;
    534     screen_list->s_socket_path = NULL;
    535     screen_list->c_socket_path = NULL;
     535    screen_list->socket_path[SOCK_SERVER] = NULL;
     536    screen_list->socket_path[SOCK_CLIENT] = NULL;
    536537    screen_list->session_name  = NULL;
    537538    screen_list->default_shell = NULL;
  • neercs/trunk/src/neercs.h

    r2589 r2614  
    139139};
    140140
     141enum socket_type
     142{
     143    SOCK_SERVER=0,
     144    SOCK_CLIENT=1
     145};
     146
    141147struct screen_list
    142148{
     
    147153    /* Detaching */
    148154    int attached;                /* Are we attached to a terminal */
    149     int s_socket;                /* Socket to write to the server */
    150     int c_socket;                /* Socket to write to the client */
    151     char *s_socket_path;         /* Socket to write to the server */
    152     char *c_socket_path;         /* Socket to write to the client */
     155    int socket[2];               /* Sockets to write to the server / to the client */
     156    char *socket_path[2];        /* Sockets to write to the server / to the client */
    153157    char *socket_dir;            /* Where to create the socket */
    154158    char *session_name;          /* Name of the session */
     
    220224int detach(struct screen_list* screen_list);
    221225int request_attach(struct screen_list* screen_list);
    222 char * build_socket_path(char *socket_dir, char *session_name, int client);
    223 int create_client_socket(struct screen_list* screen_list);
    224 int create_server_socket(struct screen_list* screen_list);
    225 int connect_client(struct screen_list* screen_list);
    226 char * connect_server(struct screen_list* screen_list);
     226char * build_socket_path(char *socket_dir, char *session_name, enum socket_type socktype);
     227int create_socket(struct screen_list* screen_list, enum socket_type socktype);
     228char * connect_socket(struct screen_list* screen_list, enum socket_type socktype);
    227229char ** list_sockets(char *socket_dir, char *session_name);
    228230int server_main(int *to_grab, char **to_start, struct screen_list *screen_list);
  • neercs/trunk/src/server.c

    r2597 r2614  
    3838{
    3939    int ret;
    40     if(!screen_list->c_socket)
    41         connect_client(screen_list);
    42     debug("Sending message (%s) to client on socket %d", msg, screen_list->c_socket);
    43     if(!screen_list->c_socket)
     40    if(!screen_list->socket[SOCK_CLIENT])
     41        connect_socket(screen_list, SOCK_CLIENT);
     42    debug("Sending message (%s) to client on socket %d", msg, screen_list->socket[SOCK_CLIENT]);
     43    if(!screen_list->socket[SOCK_CLIENT])
    4444        ret = -1;
    4545    else
    46         ret = write(screen_list->c_socket, msg, strlen(msg));
     46        ret = write(screen_list->socket[SOCK_CLIENT], msg, strlen(msg));
    4747    if(ret < 0 && errno != EAGAIN)
    4848    {
     
    8484    memcpy(buf2, "REFRESH ", 8);
    8585    memcpy(buf2+8, buf, bytes);
    86     if(!screen_list->c_socket)
    87         connect_client(screen_list);
    88     if(screen_list->c_socket)
    89         if(write(screen_list->c_socket, buf2, bytes+8) <= 0 && errno != EAGAIN)
     86    if(!screen_list->socket[SOCK_CLIENT])
     87        connect_socket(screen_list, SOCK_CLIENT);
     88    if(screen_list->socket[SOCK_CLIENT])
     89        if(write(screen_list->socket[SOCK_CLIENT], buf2, bytes+8) <= 0 && errno != EAGAIN)
    9090            detach(screen_list);
    9191    free(buf);
     
    9797{
    9898    screen_list->attached = 0;
    99     if(screen_list->c_socket)
     99    if(screen_list->socket[SOCK_CLIENT])
    100100    {
    101101        send_to_client("DETACH", screen_list);
    102         close(screen_list->c_socket);
    103         screen_list->c_socket = 0;
     102        close(screen_list->socket[SOCK_CLIENT]);
     103        screen_list->socket[SOCK_CLIENT] = 0;
    104104    }
    105105    return 0;
     
    116116
    117117    /* Create socket and bind it */
    118     create_server_socket(screen_list);
    119     connect_client(screen_list);
     118    create_socket(screen_list, SOCK_SERVER);
     119
     120    /* Connect to the client */
     121    connect_socket(screen_list, SOCK_CLIENT);
    120122
    121123    screen_list->width = screen_list->height = 10;
     
    176178
    177179        /* Check if we got something from the client */
    178         while (screen_list->s_socket && (n = read(screen_list->s_socket, buf, sizeof(buf)-1)) > 0)
     180        while (screen_list->socket[SOCK_SERVER] && (n = read(screen_list->socket[SOCK_SERVER], buf, sizeof(buf)-1)) > 0)
    179181        {
    180182            buf[n] = 0;
     
    355357    }
    356358
    357     if(screen_list->s_socket_path)
    358     {
    359         unlink(screen_list->s_socket_path);
    360         free(screen_list->s_socket_path);
    361     }
    362 
    363     if(screen_list->c_socket_path)
    364         free(screen_list->c_socket_path);
    365 
    366     if(screen_list->s_socket)
    367         close(screen_list->s_socket);
    368 
    369     if(screen_list->c_socket)
    370         close(screen_list->c_socket);
     359    if(screen_list->socket_path[SOCK_SERVER])
     360    {
     361        unlink(screen_list->socket_path[SOCK_SERVER]);
     362        free(screen_list->socket_path[SOCK_SERVER]);
     363    }
     364
     365    if(screen_list->socket_path[SOCK_CLIENT])
     366        free(screen_list->socket_path[SOCK_CLIENT]);
     367
     368    if(screen_list->socket[SOCK_SERVER])
     369        close(screen_list->socket[SOCK_SERVER]);
     370
     371    if(screen_list->socket[SOCK_CLIENT])
     372        close(screen_list->socket[SOCK_CLIENT]);
    371373
    372374    if(screen_list->screen) free(screen_list->screen);
Note: See TracChangeset for help on using the changeset viewer.