Changeset 4783 for neercs


Ignore:
Timestamp:
Jun 25, 2011, 12:08:51 PM (8 years ago)
Author:
Sam Hocevar
Message:

mini: tweak stuff in the mini server / mini client test programs.

Location:
neercs/trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • neercs/trunk/src/mini-client.c

    r4381 r4783  
    11/*
    22 *  neercs        console-based window manager
    3  *  Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
     3 *  Copyright (c) 2006-2011 Sam Hocevar <sam@hocevar.net>
    44 *                2008-2010 Jean-Yves Lamoureux <jylam@lnxscene.org>
    55 *                2008-2010 Pascal Terjan <pterjan@linuxfr.org>
     
    6969        {
    7070            fprintf(stderr, "neercs: connection established\n");
     71socket_puts(insock, "TEST insock");
    7172        }
    7273    }
  • neercs/trunk/src/mini-server.c

    r4381 r4783  
    11/*
    22 *  neercs        console-based window manager
    3  *  Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
     3 *  Copyright (c) 2006-2011 Sam Hocevar <sam@hocevar.net>
    44 *                2008-2010 Jean-Yves Lamoureux <jylam@lnxscene.org>
    55 *                2008-2010 Pascal Terjan <pterjan@linuxfr.org>
     
    2727void server_init(void)
    2828{
     29while (!insock)
    2930    insock = socket_open("/tmp/neercs.sock", 1);
    3031}
     
    3536    ssize_t bytes;
    3637    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    }
     49nothing:
    3750
    3851    ret = socket_select(insock, 1000);
  • neercs/trunk/src/mini-socket.c

    r4380 r4783  
    11/*
    22 *  neercs        console-based window manager
    3  *  Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
     3 *  Copyright (c) 2006-2011 Sam Hocevar <sam@hocevar.net>
    44 *                2008-2010 Jean-Yves Lamoureux <jylam@lnxscene.org>
    55 *                2008-2010 Pascal Terjan <pterjan@linuxfr.org>
     
    2626#include <sys/socket.h> /* bind(), connect() */
    2727#include <sys/un.h> /* AF_UNIX */
     28#include <errno.h> /* AF_UNIX */
     29#include <time.h> /* time */
    2830
    2931#include "mini-neercs.h"
     
    3133
    3234#define SIZEOF_SUN_PATH (sizeof(((struct sockaddr_un *)NULL)->sun_path))
     35
     36#define offsetof(s, f) ((int)(intptr_t)((s *)NULL)->f)
    3337
    3438struct nrx_socket
     
    3842    int fd;
    3943    int server;
     44    int connected;
    4045    char path[SIZEOF_SUN_PATH];
    4146#else
     
    4449};
    4550
     51#define QLEN 10
     52
     53int
     54serv_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
     81errout:
     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 */
     96int
     97serv_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
     140errout:
     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 */
     154int
     155cli_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
     190errout:
     191    err = errno;
     192    close(fd);
     193    errno = err;
     194    return(rval);
     195}
     196
    46197nrx_socket_t * socket_open(char const *path, int server)
    47198{
     
    50201    int ret, fd;
    51202
    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);
    53206    if (fd < 0)
    54207    {
     
    79232
    80233    fcntl(fd, F_SETFL, O_NONBLOCK);
     234#endif
     235if (server)
     236    fd = serv_listen(path);
     237else
     238    fd = cli_conn(path);
     239if (fd < 0) return NULL;
    81240
    82241    sock = malloc(sizeof(*sock));
    83242    sock->fd = fd;
    84243    sock->server = server;
     244    sock->connected = 0;
    85245    strncpy(sock->path, path, SIZEOF_SUN_PATH - 1);
    86246
     
    113273{
    114274    int ret;
     275fprintf(stderr, "pid %i sending %i bytes on %s: %s\n", getpid(), (int)strlen(str), sock->path, str);
    115276    ret = write(sock->fd, str, strlen(str));
    116277    return ret;
     
    121282    int ret;
    122283    ret = read(sock->fd, buf, count);
     284if (ret >= 0) ((char *)buf)[ret] = 0;
     285if (ret >= 0) fprintf(stderr, "pid %i recving %i bytes on %s: %s\n", getpid(), ret, sock->path, (char *)buf);
    123286    return ret;
    124287}
Note: See TracChangeset for help on using the changeset viewer.