applauncherd
invokelib.c
Go to the documentation of this file.
1/***************************************************************************
2**
3** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** Copyright (c) 2021 Open Mobile Platform LLC.
5** Copyright (c) 2021 Jolla Ltd.
6** All rights reserved.
7** Contact: Nokia Corporation (directui@nokia.com)
8**
9** This file is part of applauncherd
10**
11** If you have questions regarding the use of this file, please contact
12** Nokia at directui@nokia.com.
13**
14** This library is free software; you can redistribute it and/or
15** modify it under the terms of the GNU Lesser General Public
16** License version 2.1 as published by the Free Software Foundation
17** and appearing in the file LICENSE.LGPL included in the packaging
18** of this file.
19**
20****************************************************************************/
21
22#include <stdint.h>
23#include <stdbool.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27
28#include "report.h"
29#include "invokelib.h"
30
31static void invoke_send_or_die(int fd, const void *data, size_t size)
32{
33 if (write(fd, data, size) != (ssize_t)size) {
34 const char m[] = "*** socket write failure, terminating\n";
35 if (write(STDERR_FILENO, m, sizeof m - 1) == -1) {
36 // dontcare
37 }
38 _exit(EXIT_FAILURE);
39 }
40}
41
42void invoke_send_msg(int fd, uint32_t msg)
43{
44 debug("%s: %08x\n", __FUNCTION__, msg);
45 invoke_send_or_die(fd, &msg, sizeof(msg));
46}
47
48bool invoke_recv_msg(int fd, uint32_t *msg)
49{
50 uint32_t readBuf = 0;
51 int len = sizeof(readBuf);
52 ssize_t numRead = read(fd, &readBuf, len);
53
54 if (numRead == -1) {
55 debug("%s: Error reading message: %m\n", __FUNCTION__);
56 *msg = 0;
57 return false;
58 } else if (numRead < len) {
59 debug("%s: Error: unexpected end-of-file \n", __FUNCTION__);
60 *msg = 0;
61 return false;
62 }
63
64 debug("%s: %08x\n", __FUNCTION__, readBuf);
65 *msg = readBuf;
66 return true;
67}
68
69void invoke_send_str(int fd, const char *str)
70{
71 if (!str)
72 str = "";
73 uint32_t size = strlen(str) + 1;
74
75 /* Send size. */
76 invoke_send_msg(fd, size);
77
78 debug("%s: '%s'\n", __FUNCTION__, str);
79
80 /* Send the string. */
81 invoke_send_or_die(fd, str, size);
82}
void invoke_send_msg(int fd, uint32_t msg)
Definition invokelib.c:42
static void invoke_send_or_die(int fd, const void *data, size_t size)
Definition invokelib.c:31
bool invoke_recv_msg(int fd, uint32_t *msg)
Definition invokelib.c:48
void invoke_send_str(int fd, const char *str)
Definition invokelib.c:69