Merge tag 'v3.10.86' into linux-linaro-lsk-v3.10
[firefly-linux-kernel-4.4.55.git] / tools / gator / daemon / AnnotateListener.cpp
1 /**
2  * Copyright (C) ARM Limited 2014-2015. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #include "AnnotateListener.h"
10
11 #include <unistd.h>
12
13 #include "OlySocket.h"
14
15 static const char STREAMLINE_ANNOTATE_PARENT[] = "\0streamline-annotate-parent";
16
17 struct AnnotateClient {
18         AnnotateClient *next;
19         int fd;
20 };
21
22 AnnotateListener::AnnotateListener() : mClients(NULL), mSock(NULL), mUds(NULL) {
23 }
24
25 AnnotateListener::~AnnotateListener() {
26         close();
27         delete mUds;
28         delete mSock;
29 }
30
31 void AnnotateListener::setup() {
32         mSock = new OlyServerSocket(8082);
33         mUds = new OlyServerSocket(STREAMLINE_ANNOTATE_PARENT, sizeof(STREAMLINE_ANNOTATE_PARENT), true);
34 }
35
36 int AnnotateListener::getSockFd() {
37         return mSock->getFd();
38 }
39
40 void AnnotateListener::handleSock() {
41         AnnotateClient *const client = new AnnotateClient();
42         client->fd = mSock->acceptConnection();
43         client->next = mClients;
44         mClients = client;
45 }
46
47 int AnnotateListener::getUdsFd() {
48         return mUds->getFd();
49 }
50
51 void AnnotateListener::handleUds() {
52         AnnotateClient *const client = new AnnotateClient();
53         client->fd = mUds->acceptConnection();
54         client->next = mClients;
55         mClients = client;
56 }
57
58 void AnnotateListener::close() {
59         if (mUds != NULL) {
60                 mUds->closeServerSocket();
61         }
62         if (mSock != NULL) {
63                 mSock->closeServerSocket();
64         }
65         while (mClients != NULL) {
66                 ::close(mClients->fd);
67                 AnnotateClient *next = mClients->next;
68                 delete mClients;
69                 mClients = next;
70         }
71 }
72
73 void AnnotateListener::signal() {
74         const char ch = 0;
75         AnnotateClient **ptr = &mClients;
76         AnnotateClient *client = mClients;
77         while (client != NULL) {
78                 if (write(client->fd, &ch, sizeof(ch)) != 1) {
79                         ::close(client->fd);
80                         AnnotateClient *next = client->next;
81                         delete client;
82                         *ptr = next;
83                         client = next;
84                         continue;
85                 }
86                 ptr = &client->next;
87                 client = client->next;
88         }
89 }