summaryrefslogtreecommitdiff
path: root/vtest/vtest_server.c
blob: ee2e33c6ff730b19ad8822cd6af5600ef0a74a44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>

#include "vtest.h"
#include "vtest_protocol.h"

static int vtest_open_socket(const char *path)
{
    struct sockaddr_un un;
    int sock;

    sock = socket(PF_UNIX, SOCK_STREAM, 0);
    if (sock < 0) {
	return -1;
    }

    memset(&un, 0, sizeof(un));
    un.sun_family = AF_UNIX;
    
    snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);

    unlink(un.sun_path);

    if (bind(sock, (struct sockaddr *)&un, sizeof(un)) < 0) {
	goto err;
    }
    
    if (listen(sock, 1) < 0){
	goto err;
    }

    return sock;
 err:
    close(sock);
    return -1;
}

int wait_for_socket_accept(int sock)
{
    fd_set read_fds;
    int new_fd;
    int ret;
    FD_ZERO(&read_fds);
    FD_SET(sock, &read_fds);

    ret = select(sock + 1, &read_fds, NULL, NULL, NULL);
    if (ret < 0)
	return ret;

    if (FD_ISSET(sock, &read_fds)) {	
	new_fd = accept(sock, NULL, NULL);
	return new_fd;
    }
    return -1;
}

int wait_for_socket_read(int sock)
{
    fd_set read_fds;

    int ret;
    FD_ZERO(&read_fds);
    FD_SET(sock, &read_fds);

    ret = select(sock + 1, &read_fds, NULL, NULL, NULL);
    if (ret < 0)
	return ret;

    if (FD_ISSET(sock, &read_fds)) {	
      return 0;
    }
    return -1;
}

int main(void)
{
    int sock, new_fd, ret;
    uint32_t header[VTEST_HDR_SIZE];
    sock = vtest_open_socket("/tmp/.virgl_test");

    new_fd = wait_for_socket_accept(sock);

again:
    ret = wait_for_socket_read(new_fd);
    if (ret < 0)
      goto err;
    
    vtest_create_renderer(new_fd);
    ret = read(new_fd, &header, sizeof(header));

    if (ret == 8) {
      fprintf(stderr, "got length: %d cmd: %d\n", header[0], header[1]);

      switch (header[1]) {
      case VCMD_GET_CAPS:
	vtest_send_caps();
	break;
      case VCMD_RESOURCE_CREATE:
	vtest_create_resource();
	break;
      case VCMD_RESOURCE_UNREF:
	vtest_resource_unref();
	break;
      case VCMD_SUBMIT_CMD:
	vtest_submit_cmd(header[0]);
	break;
      default:
	break;
      }
      goto again;
    }
err:
    close(new_fd);
    close(sock);
}