aboutsummaryrefslogtreecommitdiff
path: root/host/libs/msg_queue/msg_queue.cc
blob: 8016a234780ae8ff3f987d3706d5bb3a9b110e5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// Copyright (C) 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "host/libs/msg_queue/msg_queue.h"

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#include <fstream>
#include <iostream>
#include <memory>

#include <android-base/logging.h>

namespace cuttlefish {

// class holds `msgid` returned from msg_queue_create, and match the lifetime of
// the message queue to the lifetime of the object.

SysVMessageQueue::SysVMessageQueue(int id) { msgid = id; }

SysVMessageQueue::~SysVMessageQueue(void) {
  if (msgctl(msgid, IPC_RMID, NULL) < 0) {
    int error_num = errno;
    LOG(ERROR) << "Could not remove message queue: " << strerror(error_num);
  }
}

// SysVMessageQueue::Create would return an empty/null std::unique_ptr if
// initialization failed.
std::unique_ptr<SysVMessageQueue> SysVMessageQueue::Create(
    const std::string& path, char proj_id) {
  // key file must exist before calling ftok
  std::fstream fs;
  fs.open(path, std::ios::out);
  fs.close();

  // only the owning user has access
  key_t key = ftok(path.c_str(), proj_id);
  if (key < 0) {
    int error_num = errno;
    LOG(ERROR) << "Could not ftok to create IPC key: " << strerror(error_num);
    return NULL;
  }
  int queue_id = msgget(key, 0);
  if (queue_id < 0) {
    queue_id = msgget(key, IPC_CREAT | IPC_EXCL | 0600);
  }
  auto msg = std::unique_ptr<SysVMessageQueue>(new SysVMessageQueue(queue_id));
  return msg;
}

int SysVMessageQueue::Send(void* data, size_t size, bool block) {
  int msgflg = block ? 0 : IPC_NOWAIT;
  if (msgsnd(msgid, data, size, msgflg) < 0) {
    int error_num = errno;
    if (error_num == EAGAIN) {
      // returns EAGAIN if queue is full and non-blocking
      return EAGAIN;
    }
    LOG(ERROR) << "Could not send message: " << strerror(error_num);
    return error_num;
  }
  return 0;
}

// If msgtyp is 0, then the first message in the queue is read.
// If msgtyp is greater than 0, then the first message in the queue of type
// msgtyp is read.
// If msgtyp is less than 0, then the first message in the queue with the lowest
// type less than or equal to the absolute value of msgtyp will be read.
ssize_t SysVMessageQueue::Receive(void* data, size_t size, long msgtyp,
                                  bool block) {
  // System call fails with errno set to ENOMSG if queue is empty and
  // non-blocking.
  int msgflg = block ? 0 : IPC_NOWAIT;
  return msgrcv(msgid, data, size, msgtyp, msgflg);
}

}  // namespace cuttlefish