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 
31 static 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 
42 void 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 
48 bool 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  {
56  debug("%s: Error reading message: %s\n", __FUNCTION__, strerror(errno));
57  *msg = 0;
58  return false;
59  }
60  else if (numRead < len)
61  {
62  debug("%s: Error: unexpected end-of-file \n", __FUNCTION__);
63  *msg = 0;
64  return false;
65  }
66  else
67  {
68  debug("%s: %08x\n", __FUNCTION__, readBuf);
69  *msg = readBuf;
70  return true;
71  }
72 }
73 
74 void invoke_send_str(int fd, const char *str)
75 {
76  if (!str)
77  str = "";
78  uint32_t size = strlen(str) + 1;
79 
80  /* Send size. */
81  invoke_send_msg(fd, size);
82 
83  debug("%s: '%s'\n", __FUNCTION__, str);
84 
85  /* Send the string. */
86  invoke_send_or_die(fd, str, size);
87 }
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:74