Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / common / event_socket.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2015 XSky <haomai@xsky.com>
7  *
8  * Author: Haomai Wang <haomaiwang@gmail.com>
9  *
10  * This is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License version 2.1, as published by the Free Software
13  * Foundation.  See file COPYING.
14  *
15  */
16
17 #ifndef CEPH_COMMON_EVENT_SOCKET_H
18 #define CEPH_COMMON_EVENT_SOCKET_H
19
20 #include <unistd.h>
21 #if defined(__FreeBSD__)
22 #include <errno.h>
23 #endif
24 #include "include/event_type.h"
25
26 class EventSocket {
27   int socket;
28   int type;
29
30  public:
31   EventSocket(): socket(-1), type(EVENT_SOCKET_TYPE_NONE) {}
32   bool is_valid() const { return socket != -1; }
33   int init(int fd, int t) {
34     switch (t) {
35       case EVENT_SOCKET_TYPE_PIPE:
36 #ifdef HAVE_EVENTFD
37       case EVENT_SOCKET_TYPE_EVENTFD:
38 #endif
39       {
40         socket = fd;
41         type = t;
42         return 0;
43       }
44     }
45     return -EINVAL;
46   }
47   int notify() {
48     int ret;
49     switch (type) {
50       case EVENT_SOCKET_TYPE_PIPE:
51       {
52         char buf[1];
53         buf[0] = 'i';
54         ret = write(socket, buf, 1);
55         if (ret < 0)
56           ret = -errno;
57         else
58           ret = 0;
59         break;
60       }
61 #ifdef HAVE_EVENTFD
62       case EVENT_SOCKET_TYPE_EVENTFD:
63       {
64         uint64_t value = 1;
65         ret = write(socket, &value, sizeof (value));
66         if (ret < 0)
67           ret = -errno;
68         else
69           ret = 0;
70         break;
71       }
72 #endif
73       default:
74       {
75         ret = -1;
76         break;
77       }
78     }
79     return ret;
80   }
81 };
82
83 #endif