File: msg_queue.c

package info (click to toggle)
libpulp 0.2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,964 kB
  • sloc: ansic: 11,257; python: 1,110; sh: 881; makefile: 824; cpp: 582; asm: 124
file content (178 lines) | stat: -rw-r--r-- 5,298 bytes parent folder | download
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 *  libpulp - User-space Livepatching Library
 *
 *  Copyright (C) 2021 SUSE Software Solutions GmbH
 *
 *  This file is part of libpulp.
 *
 *  libpulp is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  libpulp is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with libpulp.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "msg_queue.h"

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/* Create an externally visible msg_queue object that will be read with ptrace.
 * It will be read by ulp_messages (tools/messages.c) using ptrace. */

struct msg_queue __ulp_msg_queue;

static char msg[MSGQ_BUFFER_MAX];

static void
msgq_strpush(const char *msg, size_t msg_size)
{
  /* Write the msg_queue values in variables for briefness.  */
  int top = __ulp_msg_queue.top;
  int bottom = __ulp_msg_queue.bottom;
  int distance = __ulp_msg_queue.distance;
  char *buffer = __ulp_msg_queue.buffer;

  /* In case the message is empty or it is too large for the buffer, don't
   * bother even trying to insert it.  */
  if (*msg == '\0' || msg_size > MSGQ_BUFFER_MAX)
    return;

  /* To understand what the `top` and `bottom` means, read messages.h in the
   * include folder.
   *
   * Here, in case the message would not fit the available space in the end
   * of the queue, insert it in the beginning, and account for this jump.
   */
  if (top + msg_size >= MSGQ_BUFFER_MAX) {
    memset(&buffer[top], '\0', MSGQ_BUFFER_MAX - top);
    distance += MSGQ_BUFFER_MAX - top;
    top = 0;
  }

  /* Remember that this is a circular queue. Therefore, the distance between
   * top and bottom should not pass the size of the queue, else we have a
   * buffer overflow. In case when inserting the message would make the top
   * marker overlap the bottom marker, we must eliminate the first
   * inserted contents from the buffer.  This basically means that the bottom
   * should move towards the top until enough space is available.  */
  while (distance + msg_size >= MSGQ_BUFFER_MAX) {
    int travel = strlen(&buffer[bottom]) + 1;
    bottom = (bottom + travel) % MSGQ_BUFFER_MAX;

    /* We may have reached a sequence of null characters because of the top
     * being set to zero, which makes the end of the buffer to be filled with
     * null characters.  Account for this too.  */
    while (buffer[bottom] == '\0') {
      bottom = (bottom + 1) % MSGQ_BUFFER_MAX;
      travel++;
    }

    distance -= travel;
  }

  /* Finally, commit the message to the buffer.  */
  memcpy(&buffer[top], msg, msg_size);

  /* Update other structures.  */
  distance += msg_size;
  top = (top + msg_size) % MSGQ_BUFFER_MAX;

  __ulp_msg_queue.top = top;
  __ulp_msg_queue.bottom = bottom;
  __ulp_msg_queue.distance = distance;
}

/* Push a message into the message queue.
 *
 * @param format - printf like string.
 * */
void
msgq_push(const char *format, ...)
{
  va_list arglist;
  size_t msg_size;

  /* Expand the format string with the arguments provided. vsnprintf will
   * return the size of the string, therefore, the size of the object will
   * be +1 because of the null character in the end of the string.  */
  va_start(arglist, format);
  msg_size = vsnprintf(msg, MSGQ_BUFFER_MAX, format, arglist) + 1;
  va_end(arglist);

  msgq_strpush(msg, msg_size);
}

/* Push a message into the message queue.
 *
 * @param format - printf like string.
 * */
void
msgq_vpush(const char *format, va_list arglist)
{
  size_t msg_size;

  /* Expand the format string with the arguments provided. vsnprintf will
   * return the size of the string, therefore, the size of the object will
   * be +1 because of the null character in the end of the string.  */
  msg_size = vsnprintf(msg, MSGQ_BUFFER_MAX, format, arglist) + 1;

  msgq_strpush(msg, msg_size);
}

/** @brief Post a warning message in libpulp's circular buffer.
 *
 * Implement this here because we have a common library that is compiled once
 * for both libpulp.so and tools.
 *
 * @param format  printf formated message
 */
void
ulp_warn(const char *format, ...)
{
  time_t rawtime;
  struct tm *timeinfo, local_tm;
  char timestamp[32];

  time(&rawtime);
  timeinfo = localtime_r(&rawtime, &local_tm);

  size_t n = strftime(timestamp, 32, "[%Y-%m-%d %H:%M:%S] ", timeinfo);
  if (n > 0)
    msgq_strpush(timestamp, n + 1);

  va_list args;
  va_start(args, format);
  msgq_vpush(format, args);
  va_end(args);
}

void
ulp_debug(const char *format, ...)
{
  time_t rawtime;
  struct tm *timeinfo, local_tm;
  char timestamp[32];

  time(&rawtime);
  timeinfo = localtime_r(&rawtime, &local_tm);

  size_t n = strftime(timestamp, 32, "[%Y-%m-%d %H:%M:%S] ", timeinfo);
  if (n > 0)
    msgq_strpush(timestamp, n + 1);

  va_list args;
  va_start(args, format);
  msgq_vpush(format, args);
  va_end(args);
}