source: neercs/trunk/src/attach.c @ 2588

Last change on this file since 2588 was 2588, checked in by Pascal Terjan, 12 years ago
  • Rewrote detach handling, enjoy silent neercs
  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1#include <errno.h>
2#include <fcntl.h>
3#include <glob.h>
4#include <limits.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8
9#include <sys/socket.h>
10#include <sys/un.h>
11#include <sys/types.h>
12#include <sys/stat.h>
13
14#include <caca.h>
15
16#include "neercs.h"
17
18char * build_socket_path(char *socket_dir, char *session_name)
19{
20    char *path, *dir;
21    path = (char *)malloc(PATH_MAX+1);
22    dir = socket_dir;
23    if(!dir)
24        dir = getenv("NEERCSDIR");
25    if(!dir)
26        dir = getenv("TMPDIR");
27    if(!dir)
28        dir = "/tmp";
29    if(path)
30        snprintf(path, PATH_MAX+1, "%s/neercs.%s.sock", dir, session_name);
31    return path;
32}
33
34int create_socket(struct screen_list* screen_list)
35{
36    int sock;
37    struct sockaddr_un myaddr;
38
39    sock = socket(AF_UNIX, SOCK_DGRAM, 0);
40
41    if(sock < 0)
42    {
43        perror("create_socket:socket");
44        return errno;
45    }
46
47    memset(&myaddr, 0, sizeof(struct sockaddr_un));
48
49    myaddr.sun_family = AF_UNIX;
50    strncpy(myaddr.sun_path, screen_list->socket_path, sizeof(myaddr.sun_path) - 1);
51
52    if(bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr_un)) < 0)
53    {
54        free(screen_list->socket_path);
55        screen_list->socket_path = NULL;
56        close(sock);
57        perror("create_socket:bind");
58        return errno;
59    }
60    fcntl(sock, F_SETFL, O_NONBLOCK);
61
62    debug("Listening on %s (%d)", screen_list->socket_path, sock);
63
64    screen_list->socket = sock;
65
66    return 0;
67}
68
69char ** list_sockets(char *socket_dir, char *session_name)
70{
71    char *pattern, *dir;
72    glob_t globbuf;
73
74    globbuf.gl_pathv = NULL;
75
76    pattern = (char *)malloc(PATH_MAX+1);
77
78    dir = socket_dir;
79    if(!dir)
80        dir = getenv("NEERCSDIR");
81    if(!dir)
82        dir = getenv("TMPDIR");
83    if(!dir)
84        dir = "/tmp";
85
86    if(!pattern)
87        return globbuf.gl_pathv;
88
89    if(session_name && strlen(session_name)+strlen(dir)+13<PATH_MAX)
90        sprintf(pattern, "%s/neercs.%s.sock", dir, session_name);
91    else
92        snprintf(pattern, PATH_MAX, "%s/neercs.*.sock", dir);
93    pattern[PATH_MAX] = '\0';
94
95    debug("Looking for sockets in the form %s", pattern);
96
97    glob(pattern, 0, NULL, &globbuf);
98
99    free(pattern);
100
101    return globbuf.gl_pathv;
102}
103
104char * connect_server(char *socket_path, struct screen_list* screen_list)
105{
106    int sock;
107    struct sockaddr_un addr;
108
109    debug("Connecting to %s", socket_path);
110
111    /* Open the socket */
112    if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
113        perror("request_attach:socket");
114        return NULL;
115    }
116
117    memset(&addr,0,sizeof(addr));
118    addr.sun_family = AF_UNIX;
119    strcpy(addr.sun_path,socket_path);
120    if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
121    {
122        printf("Failed to connect to %s: %s\n", socket_path, strerror(errno));
123        return NULL;
124    }
125    fcntl(sock, F_SETFL, O_NONBLOCK);
126
127    screen_list->socket_path = socket_path;
128    screen_list->socket = sock;
129
130    return strdup(socket_path); /* FIXME */
131}
132
133int request_attach(struct screen_list* screen_list)
134{
135    debug("Requesting attach");
136
137    write(screen_list->socket, "ATTACH", strlen("ATTACH"));
138
139    return 0;
140}
141
142int send_event(caca_event_t ev, int sock)
143{
144    enum caca_event_type t;
145
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;
163}
164
Note: See TracBrowser for help on using the repository browser.