akaros/tools/compilers/gcc-glibc/glibc-2.19-akaros/sysdeps/akaros/shutdown.c
<<
>>
Prefs
   1/* Copyright (c) 2016 Google Inc.
   2 * Barret Rhoden <brho@cs.berkeley.edu>
   3 * See LICENSE for details.
   4 *
   5 * shutdown(). */
   6
   7#include <sys/socket.h>
   8#include <sys/types.h>
   9#include <unistd.h>
  10#include <errno.h>
  11#include <sys/stat.h>
  12#include <fcntl.h>
  13
  14#include <sys/plan9_helpers.h>
  15
  16/* Shuts down all or part of the connection open on socket FD.  Returns 0 on
  17 * success, -1 for errors. */
  18int shutdown(int fd, int how)
  19{
  20        int ret;
  21        static const char rd_msg[] = "shutdown rd";
  22        static const char wr_msg[] = "shutdown wr";
  23        static const char rdwr_msg[] = "shutdown rdwr";
  24        char *msg;
  25        size_t msg_sz;
  26        Rock *r;
  27
  28        switch (how) {
  29        case SHUT_RD:
  30                msg = rd_msg;
  31                msg_sz = sizeof(rd_msg);
  32                break;
  33        case SHUT_WR:
  34                msg = wr_msg;
  35                msg_sz = sizeof(wr_msg);
  36                break;
  37        case SHUT_RDWR:
  38                msg = rdwr_msg;
  39                msg_sz = sizeof(rdwr_msg);
  40                break;
  41        default:
  42                errno = EINVAL;
  43                werrstr("shutdown has bad 'how' %d", how);
  44                return -1;
  45        }
  46        r = _sock_findrock(fd, 0);
  47        if (!r) {
  48                errno = EBADF;
  49                werrstr("Rock lookup failed");
  50                return -1;
  51        }
  52        ret = write(r->ctl_fd, msg, msg_sz);
  53        if (ret != msg_sz)
  54                return -1;
  55        return 0;
  56}
  57