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