72
74
static fd_set s_fdset_read;
73
75
static fd_set s_fdset_write;
74
76
static int s_max_fd;
76
77
static int nr_client_slots;
77
78
static struct client *clients;
79
79
static char read_buf[BUFFER_SIZE];
82
initialize server's file descriptor.
85
82
init_server_fd(char *path)
90
85
struct sockaddr_un myhost;
96
91
perror("failed in socket()");
94
fchmod(fd, S_IRUSR | S_IWUSR);
100
bzero(&myhost, sizeof(myhost));
96
memset(&myhost, 0, sizeof(myhost));
101
97
myhost.sun_family = PF_UNIX;
102
98
strlcpy(myhost.sun_path, path, sizeof(myhost.sun_path));
104
foo = bind(fd, (struct sockaddr *)&myhost, SUN_LEN(&myhost));
100
if (bind(fd, (struct sockaddr *)&myhost, SUN_LEN(&myhost)) < 0) {
106
101
perror("failed in bind()");
110
chmod(path, S_IRUSR|S_IWUSR);
112
105
logname = getenv("LOGNAME");
114
107
pw = getpwnam(logname);
116
chown(path, pw->pw_uid, -1);
109
fchown(fd, pw->pw_uid, -1);
119
if ((flag = fcntl(fd, F_GETFL)) == -1) {
112
if ((flag = fcntl(fd, F_GETFL)) < 0) {
124
117
flag |= O_NONBLOCK;
125
if (fcntl(fd, F_SETFL, flag) == -1) {
118
if (fcntl(fd, F_SETFL, flag) < 0) {
123
if (listen(fd, 5) < 0) {
132
124
perror("failed in listen()");
135
/* fprintf(stderr,"listen at %s\n",path);*/
136
128
FD_SET(fd, &s_fdset_read);
142
135
get_unused_client(void)
145
139
for (i = 0; i < nr_client_slots; i++) {
146
if (clients[i].fd == -1) {
140
if (clients[i].fd == -1)
147
141
return &clients[i];
150
144
nr_client_slots++;
151
clients = realloc(clients, sizeof(struct client) * nr_client_slots);
152
clients[nr_client_slots - 1].rbuf = strdup("");
153
clients[nr_client_slots - 1].wbuf = strdup("");
145
clients = uim_realloc(clients, sizeof(struct client) * nr_client_slots);
146
clients[nr_client_slots - 1].rbuf = uim_strdup("");
147
clients[nr_client_slots - 1].wbuf = uim_strdup("");
154
149
return &clients[nr_client_slots - 1];
189
184
reflect_message_fragment(struct client *cl)
195
190
rc = read(cl->fd, read_buf, sizeof(read_buf));
197
if (rc < 0 && (errno == EAGAIN || errno == EINTR))
192
if (errno == EAGAIN || errno == EINTR)
202
198
cl->rbuf = uim_helper_buffer_append(cl->rbuf, read_buf, rc);
214
211
/* If there's no connection, we can assume user logged out. */
216
214
for (i = 0; i < nr_client_slots; i++) {
217
if (clients[i].fd != -1) {
215
if (clients[i].fd != -1)
221
219
return UIM_FALSE; /* User already logged out */
226
224
accept_new_connection(int server_fd)
228
226
struct sockaddr_un clientsoc;
229
socklen_t len = sizeof(clientsoc);
232
229
struct client *cl;
231
len = sizeof(clientsoc);
233
232
new_fd = accept(server_fd, (struct sockaddr *)&clientsoc, &len);
235
234
if (new_fd < 0) {
237
236
return UIM_FALSE;
240
if ((flag = fcntl(new_fd, F_GETFL)) == -1) {
239
if ((flag = fcntl(new_fd, F_GETFL)) < 0) {
242
241
return UIM_FALSE;
245
244
flag |= O_NONBLOCK;
246
if (fcntl(new_fd, F_SETFL, flag) == -1) {
245
if (fcntl(new_fd, F_SETFL, flag) < 0) {
248
247
return UIM_FALSE;
331
330
fd_set readfds, writefds;
334
/* Could we replace this memcpy with direct assignment? */
335
333
/* Copy readfds from s_fdset_read/s_fdset_write because select removes
336
334
readble/writable fd from readfds/writefds */
337
335
memcpy(&readfds, &s_fdset_read, sizeof(fd_set));
348
346
if (FD_ISSET(server_fd, &readfds)) {
349
347
uim_bool accepted;
350
348
accepted = accept_new_connection(server_fd);
351
if(accepted == UIM_FALSE) {
352
continue; /* acception failed, go next loop without message processing. */
349
if (accepted == UIM_FALSE) {
350
/* acception failed, go next loop without message processing. */
355
354
/* check data to write and from clients reached */
356
355
for (i = 0; i < nr_client_slots; i++) {
357
if (clients[i].fd != -1 && FD_ISSET(clients[i].fd, &writefds)) {
356
if (clients[i].fd != -1 && FD_ISSET(clients[i].fd, &writefds))
358
357
write_message(&clients[i]);
360
if (clients[i].fd != -1 && FD_ISSET(clients[i].fd, &readfds)) {
359
if (clients[i].fd != -1 && FD_ISSET(clients[i].fd, &readfds))
361
360
read_message(&clients[i]);
366
if(check_session_alive() == UIM_FALSE) {
364
if (!check_session_alive())
397
400
/* fprintf(stderr,"Waiting for connection at %s\n", path);*/
401
402
signal(SIGPIPE, SIG_IGN);
403
403
uim_helper_server_process_connection(server_fd);