1 //
2 // Copyright (C) 2020 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "host/libs/msg_queue/msg_queue.h"
17 
18 #include <stdbool.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/ipc.h>
23 #include <sys/msg.h>
24 
25 #include <fstream>
26 #include <iostream>
27 #include <memory>
28 
29 #include <android-base/logging.h>
30 
31 #define DEFAULT_MSGQ_KEY 0x1234
32 #define HASH_MULTIPLIER 5381
33 
34 namespace cuttlefish {
35 
GenerateQueueKey(const char * str)36 key_t GenerateQueueKey(const char* str) {
37   if (str == nullptr || *str == '\0') {
38     LOG(ERROR) << "Invalid queue name provided: " << str;
39     LOG(ERROR) << "Using default msg queue key: " << DEFAULT_MSGQ_KEY;
40     return DEFAULT_MSGQ_KEY;
41   }
42 
43   uint64_t hash = HASH_MULTIPLIER;
44   int c;
45 
46   while ((c = *str++)) {
47     hash = ((hash << 5) + hash) + c;
48   }
49 
50   return static_cast<key_t>(hash);
51 }
52 
53 // class holds `msgid` returned from msg_queue_create, and match the lifetime of
54 // the message queue to the lifetime of the object.
55 
SysVMessageQueue(int id,bool auto_close)56 SysVMessageQueue::SysVMessageQueue(int id, bool auto_close)
57     : msgid_(id), auto_close_(auto_close) {}
58 
~SysVMessageQueue(void)59 SysVMessageQueue::~SysVMessageQueue(void) {
60   if (auto_close_ && msgctl(msgid_, IPC_RMID, NULL) < 0) {
61     int error_num = errno;
62     LOG(ERROR) << "Could not remove message queue: " << strerror(error_num);
63   }
64 }
65 
66 // SysVMessageQueue::Create would return an empty/null std::unique_ptr if
67 // initialization failed.
Create(const std::string & queue_name,bool auto_close)68 std::unique_ptr<SysVMessageQueue> SysVMessageQueue::Create(
69     const std::string& queue_name, bool auto_close) {
70   key_t key = GenerateQueueKey(queue_name.c_str());
71 
72   int queue_id = msgget(key, 0);
73   if (queue_id < 0) {
74     queue_id = msgget(key, IPC_CREAT | IPC_EXCL | 0600);
75   }
76   auto msg = std::unique_ptr<SysVMessageQueue>(
77       new SysVMessageQueue(queue_id, auto_close));
78   return msg;
79 }
80 
Send(void * data,size_t size,bool block)81 int SysVMessageQueue::Send(void* data, size_t size, bool block) {
82   int msgflg = block ? 0 : IPC_NOWAIT;
83   if (msgsnd(msgid_, data, size, msgflg) < 0) {
84     int error_num = errno;
85     if (error_num == EAGAIN) {
86       // returns EAGAIN if queue is full and non-blocking
87       return EAGAIN;
88     }
89     LOG(ERROR) << "Could not send message: " << strerror(error_num);
90     return error_num;
91   }
92   return 0;
93 }
94 
95 // If msgtyp is 0, then the first message in the queue is read.
96 // If msgtyp is greater than 0, then the first message in the queue of type
97 // msgtyp is read.
98 // If msgtyp is less than 0, then the first message in the queue with the lowest
99 // type less than or equal to the absolute value of msgtyp will be read.
Receive(void * data,size_t size,long msgtyp,bool block)100 ssize_t SysVMessageQueue::Receive(void* data, size_t size, long msgtyp,
101                                   bool block) {
102   // Ensure data buffer has space for message type
103   if (size < sizeof(long)) {
104     LOG(ERROR) << "receive: buffer size too small";
105     return -1;
106   }
107   // System call fails with errno set to ENOMSG if queue is empty and
108   // non-blocking.
109   int msgflg = block ? 0 : IPC_NOWAIT;
110   ssize_t result = msgrcv(msgid_, data, size, msgtyp, msgflg);
111   if (result == -1) {
112     LOG(ERROR) << "receive: failed to receive any messages. Error: "
113                << strerror(errno);
114   }
115 
116   return result;
117 }
118 
119 }  // namespace cuttlefish
120