akaros/tests/srv.c
<<
>>
Prefs
   1/* Copyright (c) 2014 The Regents of the University of California
   2 * Barret Rhoden <brho@cs.berkeley.edu>
   3 * See LICENSE for details.
   4 *
   5 * srv DIALSTRING SRVNAME
   6 *
   7 * Opens DIALSTRING and drops its chan in #s at SRVNAME */
   8
   9#include <stdlib.h>
  10#include <stdio.h>
  11#include <string.h>
  12#include <unistd.h>
  13#include <assert.h>
  14#include <parlib/net.h>
  15#include <iplib/iplib.h>
  16
  17#include <sys/types.h>
  18#include <sys/stat.h>
  19#include <fcntl.h>
  20
  21int main(int argc, char *argv[])
  22{
  23        char *dialstring, *srvname;
  24        int dfd, srvfd, ret;
  25
  26        #define buf_len 128
  27        char buf[buf_len];
  28
  29        if (argc != 3) {
  30                printf("Usage: %s DIALSTRING SRVNAME\n", argv[0]);
  31                exit(-1);
  32        }
  33        dialstring = argv[1];
  34        srvname = argv[2];
  35
  36        dfd = dial9(dialstring, 0, 0, 0, 0);
  37        if (dfd < 0) {
  38                perror("Unable to dial!");
  39                exit(-1);
  40        }
  41        ret = snprintf(buf, buf_len, "#srv/%s", srvname);
  42        if (snprintf_error(ret, buf_len)) {
  43                printf("srvname too long\n");
  44                exit(-1);
  45        }
  46        srvfd = open(buf, O_RDWR | O_EXCL | O_CREAT, 0666);
  47        if (srvfd < 0) {
  48                perror("Can't create srvvile");
  49                close(dfd);
  50                exit(-1);
  51        }
  52        ret = snprintf(buf, buf_len, "%d", dfd);
  53        ret = write(srvfd, buf, ret);
  54        if (ret < 0) {
  55                perror("Failed to post fd");
  56                close(dfd);
  57                exit(-1);
  58        }
  59}
  60