Changeset 2588 for neercs/trunk/src/attach.c
- Timestamp:
- 07/27/08 01:17:04 (5 years ago)
- File:
-
- 1 edited
-
neercs/trunk/src/attach.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
neercs/trunk/src/attach.c
r2488 r2588 12 12 #include <sys/stat.h> 13 13 14 #include <caca.h> 15 14 16 #include "neercs.h" 15 17 16 staticchar * build_socket_path(char *socket_dir, char *session_name)18 char * build_socket_path(char *socket_dir, char *session_name) 17 19 { 18 20 char *path, *dir; … … 46 48 47 49 myaddr.sun_family = AF_UNIX; 48 screen_list->socket_path = build_socket_path(screen_list->socket_path, screen_list->session_name);49 50 strncpy(myaddr.sun_path, screen_list->socket_path, sizeof(myaddr.sun_path) - 1); 50 unlink(myaddr.sun_path);51 51 52 52 if(bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr_un)) < 0) … … 65 65 66 66 return 0; 67 }68 69 int detach(struct screen_list* screen_list, caca_display_t * dp)70 {71 int fd;72 int pid;73 74 screen_list->attached = 0;75 caca_free_display(dp);76 77 close(0);78 close(1);79 close(2);80 81 fd = open("/dev/null", O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);82 dup2(fd, 0);83 if(fd != 0)84 close(fd);85 fd = open("/tmp/log.txt", O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);86 dup2(fd, 1);87 dup2(fd, 2);88 if (fd > 2)89 close(fd);90 91 if(!screen_list->socket)92 {93 pid = fork();94 if (pid < 0)95 return 1;96 if (pid > 0)97 exit(0);98 99 create_socket(screen_list);100 setsid();101 }102 103 return 0;104 }105 106 static int handle_command(char * cmd, struct screen_list* screen_list, cucul_canvas_t * cv, caca_display_t ** dp)107 {108 int refresh = 0;109 debug("Received command %s", cmd);110 if(!strncmp("ENV ", cmd, 4))111 {112 putenv(strdup(cmd+4));113 }114 else if(!strncmp("ATTACH ", cmd, 7))115 {116 int fd;117 screen_list->attached = 1;118 close(screen_list->socket);119 screen_list->socket= 0;120 unlink(screen_list->socket_path);121 screen_list->socket_path = NULL;122 close(0);123 fd=open(cmd+7, O_RDWR);124 dup2(fd, 0);125 close(1);126 fd=open(cmd+7, O_RDWR);127 dup2(fd, 1);128 close(2);129 fd=open(cmd+7, O_RDWR);130 dup2(fd, 2);131 *dp = caca_create_display(cv);132 if(*dp)133 {134 caca_set_cursor(*dp, 1);135 refresh = 1;136 }137 }138 else139 {140 fprintf(stderr, "Unknown command received: %s\n", cmd);141 }142 return refresh;143 }144 145 int read_socket(struct screen_list* screen_list, cucul_canvas_t * cv, caca_display_t ** dp) {146 ssize_t n;147 char buf[4097];148 int refresh = 0;149 150 if(!screen_list->socket)151 return 0;152 153 while (screen_list->socket && (n = read(screen_list->socket, buf, sizeof(buf)-1)) > 0)154 {155 buf[n] = 0;156 refresh |= handle_command(buf, screen_list, cv, dp);157 }158 return refresh;159 67 } 160 68 … … 194 102 } 195 103 196 197 /* FIXME Is this really not defined anywhere ? */ 198 extern char **environ; 199 200 int request_attach(char *socket_path) 104 char * connect_server(char *socket_path, struct screen_list* screen_list) 201 105 { 202 106 int sock; 203 char fdstr[PATH_MAX], lnk[PATH_MAX];204 char buf[4096];205 107 struct sockaddr_un addr; 206 108 … … 210 112 if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { 211 113 perror("request_attach:socket"); 212 return 1;114 return NULL; 213 115 } 214 116 … … 219 121 { 220 122 printf("Failed to connect to %s: %s\n", socket_path, strerror(errno)); 221 return 1;123 return NULL; 222 124 } 125 fcntl(sock, F_SETFL, O_NONBLOCK); 223 126 224 debug("Sending environment", 0); 127 screen_list->socket_path = socket_path; 128 screen_list->socket = sock; 225 129 226 /* Send our environment */ 227 #define sendvar(var) if(getenv(var)) {snprintf(buf, 4096, "ENV %s=%s", var, getenv(var)); write(sock, buf, strlen(buf));} 228 sendvar("DISPLAY") 229 sendvar("CACA_DRIVER") 130 return strdup(socket_path); /* FIXME */ 131 } 230 132 231 snprintf(fdstr, sizeof(fdstr), "/proc/%d/fd/0", getpid()); 232 readlink(fdstr, lnk, sizeof(lnk)-1); 233 lnk[PATH_MAX-1] = '\0';133 int request_attach(struct screen_list* screen_list) 134 { 135 debug("Requesting attach"); 234 136 235 close(0);137 write(screen_list->socket, "ATTACH", strlen("ATTACH")); 236 138 237 debug("Requesting attach to %s", lnk); 139 return 0; 140 } 238 141 239 /* Request attaching */ 240 snprintf(buf, 4096, "ATTACH %s", lnk); 241 write(sock, buf, strlen(buf)); 242 close(sock); 142 int send_event(caca_event_t ev, int sock) 143 { 144 enum caca_event_type t; 243 145 244 debug("Exiting", 0); 245 exit(0); 146 t = caca_get_event_type(&ev); 147 148 if(t & CACA_EVENT_KEY_PRESS) 149 { 150 char buf[16]; 151 int bytes; 152 bytes = snprintf(buf, sizeof(buf)-1, "KEY %d", caca_get_event_key_ch(&ev)); 153 buf[bytes] = '\0'; 154 debug("Sending key press to server: %s", buf); 155 return write(sock, buf, strlen(buf)) <= 0; 156 } 157 else if(t & CACA_EVENT_RESIZE) 158 return write(sock, "RESIZE", strlen("RESIZE")) <= 0; 159 else if(t & CACA_EVENT_QUIT) 160 return write(sock, "QUIT", strlen("QUIT")) <= 0; 161 162 return 0; 246 163 } 164
Note: See TracChangeset
for help on using the changeset viewer.
