Changeset 2614 for neercs/trunk
- Timestamp:
- Jul 31, 2008, 1:34:55 AM (13 years ago)
- Location:
- neercs/trunk/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
neercs/trunk/src/attach.c
r2611 r2614 17 17 #include "neercs.h" 18 18 19 char * build_socket_path(char *socket_dir, char *session_name, int client)19 char * build_socket_path(char *socket_dir, char *session_name, enum socket_type socktype) 20 20 { 21 21 char *path, *dir; … … 29 29 dir = "/tmp"; 30 30 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"); 32 32 return path; 33 33 } 34 34 35 int create_ client_socket(struct screen_list* screen_list)35 int create_socket(struct screen_list* screen_list, enum socket_type socktype) 36 36 { 37 37 int sock; … … 42 42 if(sock < 0) 43 43 { 44 perror("create_ client_socket:socket");44 perror("create_socket:socket"); 45 45 return errno; 46 46 } … … 49 49 50 50 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); 52 52 53 unlink(screen_list-> c_socket_path);53 unlink(screen_list->socket_path[socktype]); 54 54 55 55 if(bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr_un)) < 0) 56 56 { 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; 96 59 close(sock); 97 60 perror("create_socket:bind"); … … 100 63 fcntl(sock, F_SETFL, O_NONBLOCK); 101 64 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); 103 66 104 screen_list->s _socket= sock;67 screen_list->socket[socktype] = sock; 105 68 106 69 return 0; … … 142 105 } 143 106 144 char * connect_s erver(struct screen_list* screen_list)107 char * connect_socket(struct screen_list* screen_list, enum socket_type socktype) 145 108 { 146 109 int sock; … … 148 111 char *p, *s; 149 112 150 debug("Connecting to %s", screen_list->s _socket_path);113 debug("Connecting to %s", screen_list->socket_path[socktype]); 151 114 152 115 /* Open the socket */ … … 158 121 memset(&addr,0,sizeof(addr)); 159 122 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]); 161 124 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) 162 125 { 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)); 164 127 return NULL; 165 128 } 166 129 fcntl(sock, F_SETFL, O_NONBLOCK); 167 130 168 screen_list->s _socket= sock;131 screen_list->socket[socktype] = sock; 169 132 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; 193 145 } 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; 208 148 } 209 149 … … 219 159 buf[bytes] = '\0'; 220 160 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; 222 162 } 223 163 … … 235 175 buf[bytes] = '\0'; 236 176 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; 238 178 } 239 179 else if(t & CACA_EVENT_RESIZE) … … 247 187 buf[bytes] = '\0'; 248 188 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; 250 190 } 251 191 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; 253 193 254 194 return 0; -
neercs/trunk/src/main.c
r2613 r2614 261 261 for(i=0; sockets[i]; i++); 262 262 i--; 263 screen_list->s _socket_path= strdup(sockets[i]);264 session = connect_s erver(screen_list);263 screen_list->socket_path[SOCK_SERVER] = strdup(sockets[i]); 264 session = connect_socket(screen_list, SOCK_SERVER); 265 265 while(!session && i > 0) 266 266 { 267 free(screen_list->s _socket_path);267 free(screen_list->socket_path[SOCK_SERVER]); 268 268 i--; 269 screen_list->s _socket_path= strdup(sockets[i]);270 session = connect_s erver(screen_list);269 screen_list->socket_path[SOCK_SERVER] = strdup(sockets[i]); 270 session = connect_socket(screen_list, SOCK_SERVER); 271 271 } 272 272 debug("Connected to session %s", session); … … 280 280 caca_set_cursor(screen_list->dp, 1); 281 281 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); 284 285 request_attach(screen_list); 285 286 if(screen_list->session_name) … … 290 291 { 291 292 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; 294 295 attach = 0; 295 296 } … … 320 321 } 321 322 } 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); 329 330 330 331 /* Fork the server if needed */ … … 365 366 return server_main(to_grab, to_start, screen_list); 366 367 } 367 create_ client_socket(screen_list);368 while((sess = connect_s erver(screen_list)) == NULL)368 create_socket(screen_list, SOCK_CLIENT); 369 while((sess = connect_socket(screen_list, SOCK_SERVER)) == NULL) 369 370 usleep(100); 370 371 free(sess); … … 389 390 char buf[128*1024]; 390 391 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) 392 393 { 393 394 buf[n] = 0; … … 445 446 } 446 447 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]); 461 462 462 463 if(screen_list->screen) … … 529 530 screen_list->lock_offset = 0; 530 531 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; 533 534 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; 536 537 screen_list->session_name = NULL; 537 538 screen_list->default_shell = NULL; -
neercs/trunk/src/neercs.h
r2589 r2614 139 139 }; 140 140 141 enum socket_type 142 { 143 SOCK_SERVER=0, 144 SOCK_CLIENT=1 145 }; 146 141 147 struct screen_list 142 148 { … … 147 153 /* Detaching */ 148 154 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 */ 153 157 char *socket_dir; /* Where to create the socket */ 154 158 char *session_name; /* Name of the session */ … … 220 224 int detach(struct screen_list* screen_list); 221 225 int 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); 226 char * build_socket_path(char *socket_dir, char *session_name, enum socket_type socktype); 227 int create_socket(struct screen_list* screen_list, enum socket_type socktype); 228 char * connect_socket(struct screen_list* screen_list, enum socket_type socktype); 227 229 char ** list_sockets(char *socket_dir, char *session_name); 228 230 int server_main(int *to_grab, char **to_start, struct screen_list *screen_list); -
neercs/trunk/src/server.c
r2597 r2614 38 38 { 39 39 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]) 44 44 ret = -1; 45 45 else 46 ret = write(screen_list-> c_socket, msg, strlen(msg));46 ret = write(screen_list->socket[SOCK_CLIENT], msg, strlen(msg)); 47 47 if(ret < 0 && errno != EAGAIN) 48 48 { … … 84 84 memcpy(buf2, "REFRESH ", 8); 85 85 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) 90 90 detach(screen_list); 91 91 free(buf); … … 97 97 { 98 98 screen_list->attached = 0; 99 if(screen_list-> c_socket)99 if(screen_list->socket[SOCK_CLIENT]) 100 100 { 101 101 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; 104 104 } 105 105 return 0; … … 116 116 117 117 /* 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); 120 122 121 123 screen_list->width = screen_list->height = 10; … … 176 178 177 179 /* 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) 179 181 { 180 182 buf[n] = 0; … … 355 357 } 356 358 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]); 371 373 372 374 if(screen_list->screen) free(screen_list->screen);
Note: See TracChangeset
for help on using the changeset viewer.