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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/*
* Copyright © 2018 Red Hat, Inc
*
* This program 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.
*
* This library 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 Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Christian J. Kellner <christian@kellner.me>
*/
#include "config.h"
#include "bolt-unix.h"
#include "bolt-io.h"
#include "bolt-macros.h"
#include "bolt-str.h"
#include "bolt-test.h"
#include <glib-unix.h>
#include <gio/gio.h>
#include <locale.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h> /* unlinkat, fork */
typedef struct TestDummy
{
int dummy;
} TestDummy;
static void
test_pid_is_alive (TestDummy *tt, gconstpointer user_data)
{
gboolean ok;
pid_t p;
pid_t r;
int status;
ok = bolt_pid_is_alive (0);
g_assert_true (ok);
p = fork ();
g_assert_cmpint ((int) p, >, -1);
if (p == 0)
/* child */
exit (42);
/* parent */
ok = bolt_pid_is_alive (p);
g_assert_true (ok);
r = waitpid (0, &status, 0);
g_assert_cmpint ((int) r, ==, (int) p);
ok = bolt_pid_is_alive (p);
g_assert_false (ok);
}
typedef struct TestNotify
{
BoltTmpDir tmpdir;
char *socket_path;
guint socket_watch;
int socket_fd;
/* */
guint counter;
GQueue messages;
} TestNotify;
union ctrlmsg
{
struct cmsghdr hdr;
guint8 buf[CMSG_SPACE (sizeof (struct ucred))];
};
static char *
test_notify_revmsg (TestNotify *tt, gboolean queue)
{
char data[4096];
char *msg;
struct iovec iov = {
.iov_base = data,
.iov_len = sizeof (data) - 1,
};
union ctrlmsg crtl = {};
struct msghdr hdr = {
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = &crtl,
.msg_controllen = sizeof (crtl),
};
struct ucred *ucred = NULL;
ssize_t r;
/* MSG_TRUNC: return the real size */
r = recvmsg (tt->socket_fd, &hdr, MSG_DONTWAIT | MSG_CMSG_CLOEXEC | MSG_TRUNC);
if (r < 0)
{
if (errno == EINTR || errno == EAGAIN)
return NULL;
g_critical ("i/o error reading from notify socket: %m");
return NULL;
}
if (hdr.msg_flags & MSG_TRUNC || ((size_t) r > sizeof (data) - 1))
{
g_warning ("notification message truncated");
return NULL;
}
g_assert_cmpint (r, <, sizeof (data));
data[r] = '\0';
tt->counter++;
msg = g_strdup (data);
for (struct cmsghdr *c = CMSG_FIRSTHDR (&hdr);
c != NULL;
c = CMSG_NXTHDR (&hdr, c))
{
if (c->cmsg_level != SOL_SOCKET)
continue;
if (c->cmsg_type == SCM_CREDENTIALS &&
c->cmsg_len == CMSG_LEN (sizeof (struct ucred)))
ucred = (struct ucred *) (void *) CMSG_DATA (c);
}
if (queue)
g_queue_push_tail (&tt->messages, msg);
g_debug ("got message: '%s' [%s]", msg, bolt_yesno (queue));
if (ucred != NULL)
g_debug (" ucred, pid: %i, uid: %li, gid: %li",
(int) ucred->pid, (long) ucred->uid, (long) ucred->gid);
return msg;
}
static gboolean
got_notification (gpointer user_data)
{
TestNotify *tt = (TestNotify *) user_data;
test_notify_revmsg (tt, TRUE);
return TRUE;
}
static void
test_notify_setup (TestNotify *tt, gconstpointer data)
{
g_autoptr(GError) err = NULL;
g_autoptr(GSource) source = NULL;
bolt_autoclose int fd = -1;
static const int one = 1;
struct sockaddr_un sau = {AF_UNIX, {'\0', }};
size_t socklen;
int r;
tt->tmpdir = bolt_tmp_dir_make ("bolt.unix.XXXXXX", &err);
g_assert_no_error (err);
g_assert_nonnull (tt->tmpdir);
fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
g_assert_cmpint (fd, >, -1);
tt->socket_path = g_build_filename (tt->tmpdir, "notify_socket", NULL);
strncpy (sau.sun_path, tt->socket_path, sizeof (sau.sun_path) - 1);
socklen =
offsetof (struct sockaddr_un, sun_path)
+ strlen (sau.sun_path)
+ 1;
r = bind (fd, &sau, socklen);
g_assert_cmpint (r, >, -1);
r = setsockopt (fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof (one));
g_assert_cmpint (r, >, -1);
source = g_unix_fd_source_new (fd, G_IO_IN);
g_assert_nonnull (source);
g_source_set_callback (source, got_notification, tt, NULL);
tt->socket_watch = g_source_attach (source, NULL);
tt->socket_fd = bolt_steal (&fd, -1);
g_queue_init (&tt->messages);
g_debug ("notification socket at '%s'", sau.sun_path);
}
static void
test_notify_teardown (TestNotify *tt, gconstpointer user)
{
g_autoptr(GError) err = NULL;
g_clear_handle_id (&tt->socket_watch, g_source_remove);
if (tt->socket_fd > -1)
{
bolt_close (tt->socket_fd, &err);
g_assert_no_error (err);
tt->socket_fd = -1;
}
g_clear_pointer (&tt->tmpdir, bolt_tmp_dir_destroy);
g_queue_free_full (&tt->messages, g_free);
}
static void
test_sd_notify (TestNotify *tt, gconstpointer user)
{
g_autoptr(GError) err = NULL;
g_autofree char *verylong = NULL;
const char *ref = NULL;
gboolean sent;
gboolean ok;
size_t l;
char *msg;
/* no socket at all */
ref = "STATUS=this is my message";
ok = bolt_sd_notify_literal (ref, &sent, &err);
g_assert_no_error (err);
g_assert_true (ok);
g_assert_false (sent);
/* invalid/unsupported destinations */
g_setenv ("NOTIFY_SOCKET", "INVALID SOCKET", TRUE);
ok = bolt_sd_notify_literal (ref, &sent, &err);
g_assert_error (err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
g_assert_false (ok);
g_assert_false (sent);
g_clear_error (&err);
/* socket dest too long */
l = sizeof (((struct sockaddr_un *) 0)->sun_path) + 10;
g_assert_cmpuint (l, <, 1024);
verylong = g_strnfill (l, 'a');
g_setenv ("NOTIFY_SOCKET", verylong, TRUE);
ok = bolt_sd_notify_literal (ref, &sent, &err);
g_assert_error (err, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
g_assert_false (ok);
g_assert_false (sent);
g_clear_error (&err);
/* peer does not exist */
g_setenv ("NOTIFY_SOCKET", "@NONEXISTANTABSTRACT", TRUE);
ok = bolt_sd_notify_literal (ref, &sent, &err);
g_assert_error (err, G_IO_ERROR, G_IO_ERROR_CONNECTION_REFUSED);
g_assert_false (ok);
g_assert_true (sent);
g_clear_error (&err);
/* finally the VALID socket */
g_setenv ("NOTIFY_SOCKET", tt->socket_path, TRUE);
ok = bolt_sd_notify_literal (ref, &sent, &err);
g_assert_no_error (err);
g_assert_true (ok);
g_assert_true (sent);
msg = test_notify_revmsg (tt, FALSE);
g_assert_nonnull (msg);
g_assert_cmpstr (msg, ==, ref);
g_free (msg);
}
int
main (int argc, char **argv)
{
setlocale (LC_ALL, "");
g_test_init (&argc, &argv, NULL);
g_test_add ("/common/unix/pid_is_alive",
TestDummy,
NULL,
NULL,
test_pid_is_alive,
NULL);
g_test_add ("/common/unix/bolt_sd_notify",
TestNotify,
NULL,
test_notify_setup,
test_sd_notify,
test_notify_teardown);
return g_test_run ();
}
|