Changeset 4783
- Timestamp:
- Jun 25, 2011, 12:08:51 PM (11 years ago)
- Location:
- neercs/trunk/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
neercs/trunk/src/mini-client.c
r4381 r4783 1 1 /* 2 2 * neercs console-based window manager 3 * Copyright (c) 2006-201 0Sam Hocevar <sam@hocevar.net>3 * Copyright (c) 2006-2011 Sam Hocevar <sam@hocevar.net> 4 4 * 2008-2010 Jean-Yves Lamoureux <jylam@lnxscene.org> 5 5 * 2008-2010 Pascal Terjan <pterjan@linuxfr.org> … … 69 69 { 70 70 fprintf(stderr, "neercs: connection established\n"); 71 socket_puts(insock, "TEST insock"); 71 72 } 72 73 } -
neercs/trunk/src/mini-server.c
r4381 r4783 1 1 /* 2 2 * neercs console-based window manager 3 * Copyright (c) 2006-201 0Sam Hocevar <sam@hocevar.net>3 * Copyright (c) 2006-2011 Sam Hocevar <sam@hocevar.net> 4 4 * 2008-2010 Jean-Yves Lamoureux <jylam@lnxscene.org> 5 5 * 2008-2010 Pascal Terjan <pterjan@linuxfr.org> … … 27 27 void server_init(void) 28 28 { 29 while (!insock) 29 30 insock = socket_open("/tmp/neercs.sock", 1); 30 31 } … … 35 36 ssize_t bytes; 36 37 int ret; 38 39 if (outsock) 40 { 41 ret = socket_select(outsock, 1000); 42 if (ret <= 0) 43 goto nothing; 44 45 bytes = socket_read(outsock, buf, BUFSIZ); 46 if (bytes <= 0) 47 goto nothing; 48 } 49 nothing: 37 50 38 51 ret = socket_select(insock, 1000); -
neercs/trunk/src/mini-socket.c
r4380 r4783 1 1 /* 2 2 * neercs console-based window manager 3 * Copyright (c) 2006-201 0Sam Hocevar <sam@hocevar.net>3 * Copyright (c) 2006-2011 Sam Hocevar <sam@hocevar.net> 4 4 * 2008-2010 Jean-Yves Lamoureux <jylam@lnxscene.org> 5 5 * 2008-2010 Pascal Terjan <pterjan@linuxfr.org> … … 26 26 #include <sys/socket.h> /* bind(), connect() */ 27 27 #include <sys/un.h> /* AF_UNIX */ 28 #include <errno.h> /* AF_UNIX */ 29 #include <time.h> /* time */ 28 30 29 31 #include "mini-neercs.h" … … 31 33 32 34 #define SIZEOF_SUN_PATH (sizeof(((struct sockaddr_un *)NULL)->sun_path)) 35 36 #define offsetof(s, f) ((int)(intptr_t)((s *)NULL)->f) 33 37 34 38 struct nrx_socket … … 38 42 int fd; 39 43 int server; 44 int connected; 40 45 char path[SIZEOF_SUN_PATH]; 41 46 #else … … 44 49 }; 45 50 51 #define QLEN 10 52 53 int 54 serv_listen(const char *name) 55 { 56 int fd, len, err, rval; 57 struct sockaddr_un un; 58 59 /* create a UNIX domain stream socket */ 60 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) 61 return(-1); 62 unlink(name); /* in case it already exists */ 63 64 /* fill in socket address structure */ 65 memset(&un, 0, sizeof(un)); 66 un.sun_family = AF_UNIX; 67 strcpy(un.sun_path, name); 68 len = offsetof(struct sockaddr_un, sun_path) + strlen(name); 69 70 /* bind the name to the descriptor */ 71 if (bind(fd, (struct sockaddr *)&un, len) < 0) { 72 rval = -2; 73 goto errout; 74 } 75 if (listen(fd, QLEN) < 0) { /* tell kernel we're a server */ 76 rval = -3; 77 goto errout; 78 } 79 return(fd); 80 81 errout: 82 err = errno; 83 close(fd); 84 errno = err; 85 return(rval); 86 } 87 88 #define STALE 30 /* client's name can't be older than this (sec) */ 89 90 /* 91 * Wait for a client connection to arrive, and accept it. 92 * We also obtain the client's user ID from the pathname 93 * that it must bind before calling us. 94 * Returns new fd if all OK, <0 on error 95 */ 96 int 97 serv_accept(int listenfd, uid_t *uidptr) 98 { 99 int clifd, len, err, rval; 100 time_t staletime; 101 struct sockaddr_un un; 102 struct stat statbuf; 103 104 len = sizeof(un); 105 if ((clifd = accept(listenfd, (struct sockaddr *)&un, &len)) < 0) 106 return(-1); /* often errno=EINTR, if signal caught */ 107 108 /* obtain the client's uid from its calling address */ 109 len -= offsetof(struct sockaddr_un, sun_path); /* len of pathname */ 110 un.sun_path[len] = 0; /* null terminate */ 111 112 if (stat(un.sun_path, &statbuf) < 0) { 113 rval = -2; 114 goto errout; 115 } 116 #ifdef S_ISSOCK /* not defined for SVR4 */ 117 if (S_ISSOCK(statbuf.st_mode) == 0) { 118 rval = -3; /* not a socket */ 119 goto errout; 120 } 121 #endif 122 if ((statbuf.st_mode & (S_IRWXG | S_IRWXO)) || 123 (statbuf.st_mode & S_IRWXU) != S_IRWXU) { 124 rval = -4; /* is not rwx------ */ 125 goto errout; 126 } 127 128 staletime = time(NULL) - STALE; 129 if (statbuf.st_atime < staletime || 130 statbuf.st_ctime < staletime || 131 statbuf.st_mtime < staletime) { 132 rval = -5; /* i-node is too old */ 133 goto errout; 134 } 135 if (uidptr != NULL) 136 *uidptr = statbuf.st_uid; /* return uid of caller */ 137 unlink(un.sun_path); /* we're done with pathname now */ 138 return(clifd); 139 140 errout: 141 err = errno; 142 close(clifd); 143 errno = err; 144 return(rval); 145 } 146 147 #define CLI_PATH "/var/tmp/" /* +5 for pid = 14 chars */ 148 #define CLI_PERM S_IRWXU /* rwx for user only */ 149 150 /* 151 * Create a client endpoint and connect to a server. 152 * Returns fd if all OK, <0 on error. 153 */ 154 int 155 cli_conn(const char *name) 156 { 157 int fd, len, err, rval; 158 struct sockaddr_un un; 159 160 /* create a UNIX domain stream socket */ 161 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) 162 return(-1); 163 164 /* fill socket address structure with our address */ 165 memset(&un, 0, sizeof(un)); 166 un.sun_family = AF_UNIX; 167 sprintf(un.sun_path, "%s%05d", CLI_PATH, getpid()); 168 len = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path); 169 170 unlink(un.sun_path); /* in case it already exists */ 171 if (bind(fd, (struct sockaddr *)&un, len) < 0) { 172 rval = -2; 173 goto errout; 174 } 175 if (chmod(un.sun_path, CLI_PERM) < 0) { 176 rval = -3; 177 goto errout; 178 } 179 /* fill socket address structure with server's address */ 180 memset(&un, 0, sizeof(un)); 181 un.sun_family = AF_UNIX; 182 strcpy(un.sun_path, name); 183 len = offsetof(struct sockaddr_un, sun_path) + strlen(name); 184 if (connect(fd, (struct sockaddr *)&un, len) < 0) { 185 rval = -4; 186 goto errout; 187 } 188 return(fd); 189 190 errout: 191 err = errno; 192 close(fd); 193 errno = err; 194 return(rval); 195 } 196 46 197 nrx_socket_t * socket_open(char const *path, int server) 47 198 { … … 50 201 int ret, fd; 51 202 52 fd = socket(AF_UNIX, SOCK_DGRAM, 0); 203 #if 0 204 fd = socket(AF_UNIX, SOCK_STREAM, 0); 205 //fd = socket(AF_UNIX, SOCK_DGRAM, 0); 53 206 if (fd < 0) 54 207 { … … 79 232 80 233 fcntl(fd, F_SETFL, O_NONBLOCK); 234 #endif 235 if (server) 236 fd = serv_listen(path); 237 else 238 fd = cli_conn(path); 239 if (fd < 0) return NULL; 81 240 82 241 sock = malloc(sizeof(*sock)); 83 242 sock->fd = fd; 84 243 sock->server = server; 244 sock->connected = 0; 85 245 strncpy(sock->path, path, SIZEOF_SUN_PATH - 1); 86 246 … … 113 273 { 114 274 int ret; 275 fprintf(stderr, "pid %i sending %i bytes on %s: %s\n", getpid(), (int)strlen(str), sock->path, str); 115 276 ret = write(sock->fd, str, strlen(str)); 116 277 return ret; … … 121 282 int ret; 122 283 ret = read(sock->fd, buf, count); 284 if (ret >= 0) ((char *)buf)[ret] = 0; 285 if (ret >= 0) fprintf(stderr, "pid %i recving %i bytes on %s: %s\n", getpid(), ret, sock->path, (char *)buf); 123 286 return ret; 124 287 }
Note: See TracChangeset
for help on using the changeset viewer.