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 namespace cuttlefish {
32
33 // class holds `msgid` returned from msg_queue_create, and match the lifetime of
34 // the message queue to the lifetime of the object.
35
SysVMessageQueue(int id,bool auto_close)36 SysVMessageQueue::SysVMessageQueue(int id, bool auto_close)
37 : msgid_(id), auto_close_(auto_close) {}
38
~SysVMessageQueue(void)39 SysVMessageQueue::~SysVMessageQueue(void) {
40 if (auto_close_ && msgctl(msgid_, IPC_RMID, NULL) < 0) {
41 int error_num = errno;
42 LOG(ERROR) << "Could not remove message queue: " << strerror(error_num);
43 }
44 }
45
46 // SysVMessageQueue::Create would return an empty/null std::unique_ptr if
47 // initialization failed.
Create(const std::string & path,char proj_id,bool auto_close)48 std::unique_ptr<SysVMessageQueue> SysVMessageQueue::Create(
49 const std::string& path, char proj_id, bool auto_close) {
50 // key file must exist before calling ftok
51 std::fstream fs;
52 fs.open(path, std::ios::out);
53 fs.close();
54
55 // only the owning user has access
56 key_t key = ftok(path.c_str(), proj_id);
57 if (key < 0) {
58 int error_num = errno;
59 LOG(ERROR) << "Could not ftok to create IPC key: " << strerror(error_num);
60 return NULL;
61 }
62 int queue_id = msgget(key, 0);
63 if (queue_id < 0) {
64 queue_id = msgget(key, IPC_CREAT | IPC_EXCL | 0600);
65 }
66 auto msg = std::unique_ptr<SysVMessageQueue>(
67 new SysVMessageQueue(queue_id, auto_close));
68 return msg;
69 }
70
Send(void * data,size_t size,bool block)71 int SysVMessageQueue::Send(void* data, size_t size, bool block) {
72 int msgflg = block ? 0 : IPC_NOWAIT;
73 if (msgsnd(msgid_, data, size, msgflg) < 0) {
74 int error_num = errno;
75 if (error_num == EAGAIN) {
76 // returns EAGAIN if queue is full and non-blocking
77 return EAGAIN;
78 }
79 LOG(ERROR) << "Could not send message: " << strerror(error_num);
80 return error_num;
81 }
82 return 0;
83 }
84
85 // If msgtyp is 0, then the first message in the queue is read.
86 // If msgtyp is greater than 0, then the first message in the queue of type
87 // msgtyp is read.
88 // If msgtyp is less than 0, then the first message in the queue with the lowest
89 // type less than or equal to the absolute value of msgtyp will be read.
Receive(void * data,size_t size,long msgtyp,bool block)90 ssize_t SysVMessageQueue::Receive(void* data, size_t size, long msgtyp,
91 bool block) {
92 // System call fails with errno set to ENOMSG if queue is empty and
93 // non-blocking.
94 int msgflg = block ? 0 : IPC_NOWAIT;
95 return msgrcv(msgid_, data, size, msgtyp, msgflg);
96 }
97
98 } // namespace cuttlefish
99