kdbus: the driver, original and non-working
[platform/kernel/linux-exynos.git] / tools / testing / selftests / kdbus / test-attach-flags.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <stdint.h>
9 #include <errno.h>
10 #include <assert.h>
11 #include <sys/capability.h>
12 #include <sys/mman.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <linux/unistd.h>
16
17 #include "kdbus-api.h"
18 #include "kdbus-test.h"
19 #include "kdbus-util.h"
20 #include "kdbus-enum.h"
21
22 /*
23  * Should be the sum of the currently supported and compiled-in
24  * KDBUS_ITEMS_* that reflect KDBUS_ATTACH_* flags.
25  */
26 static unsigned int KDBUS_TEST_ITEMS_SUM = KDBUS_ATTACH_ITEMS_TYPE_SUM;
27
28 static struct kdbus_conn *__kdbus_hello(const char *path, uint64_t flags,
29                                         uint64_t attach_flags_send,
30                                         uint64_t attach_flags_recv)
31 {
32         struct kdbus_cmd_free cmd_free = {};
33         int ret, fd;
34         struct kdbus_conn *conn;
35         struct {
36                 struct kdbus_cmd_hello hello;
37
38                 struct {
39                         uint64_t size;
40                         uint64_t type;
41                         char str[16];
42                 } conn_name;
43
44                 uint8_t extra_items[0];
45         } h;
46
47         memset(&h, 0, sizeof(h));
48
49         kdbus_printf("-- opening bus connection %s\n", path);
50         fd = open(path, O_RDWR|O_CLOEXEC);
51         if (fd < 0) {
52                 kdbus_printf("--- error %d (%m)\n", fd);
53                 return NULL;
54         }
55
56         h.hello.flags = flags | KDBUS_HELLO_ACCEPT_FD;
57         h.hello.attach_flags_send = attach_flags_send;
58         h.hello.attach_flags_recv = attach_flags_recv;
59         h.conn_name.type = KDBUS_ITEM_CONN_DESCRIPTION;
60         strcpy(h.conn_name.str, "this-is-my-name");
61         h.conn_name.size = KDBUS_ITEM_HEADER_SIZE + strlen(h.conn_name.str) + 1;
62
63         h.hello.size = sizeof(h);
64         h.hello.pool_size = POOL_SIZE;
65
66         ret = kdbus_cmd_hello(fd, (struct kdbus_cmd_hello *) &h.hello);
67         if (ret < 0) {
68                 kdbus_printf("--- error when saying hello: %d (%m)\n", ret);
69                 return NULL;
70         }
71
72         kdbus_printf("-- New connection ID : %llu\n",
73                      (unsigned long long)h.hello.id);
74
75         cmd_free.size = sizeof(cmd_free);
76         cmd_free.offset = h.hello.offset;
77         ret = kdbus_cmd_free(fd, &cmd_free);
78         if (ret < 0)
79                 return NULL;
80
81         conn = malloc(sizeof(*conn));
82         if (!conn) {
83                 kdbus_printf("unable to malloc()!?\n");
84                 return NULL;
85         }
86
87         conn->buf = mmap(NULL, POOL_SIZE, PROT_READ, MAP_SHARED, fd, 0);
88         if (conn->buf == MAP_FAILED) {
89                 ret = -errno;
90                 free(conn);
91                 close(fd);
92                 kdbus_printf("--- error mmap: %d (%m)\n", ret);
93                 return NULL;
94         }
95
96         conn->fd = fd;
97         conn->id = h.hello.id;
98         return conn;
99 }
100
101 static int kdbus_test_peers_creation(struct kdbus_test_env *env)
102 {
103         int ret;
104         int control_fd;
105         char *path;
106         char *busname;
107         char buspath[2048];
108         char control_path[2048];
109         uint64_t attach_flags_mask;
110         struct kdbus_conn *conn;
111
112         snprintf(control_path, sizeof(control_path),
113                  "%s/control", env->root);
114
115         /*
116          * Set kdbus system-wide mask to 0, this has nothing
117          * to do with the following tests, bus and connection
118          * creation nor connection update, but we do it so we are
119          * sure that everything work as expected
120          */
121
122         attach_flags_mask = 0;
123         ret = kdbus_sysfs_set_parameter_mask(env->mask_param_path,
124                                              attach_flags_mask);
125         ASSERT_RETURN(ret == 0);
126
127
128         /*
129          * Create bus with a full set of ATTACH flags
130          */
131
132         control_fd = open(control_path, O_RDWR);
133         ASSERT_RETURN(control_fd >= 0);
134
135         busname = unique_name("test-peers-creation-bus");
136         ASSERT_RETURN(busname);
137
138         ret = kdbus_create_bus(control_fd, busname, _KDBUS_ATTACH_ALL,
139                                0, &path);
140         ASSERT_RETURN(ret == 0);
141
142         snprintf(buspath, sizeof(buspath), "%s/%s/bus", env->root, path);
143
144         /*
145          * Create a connection with an empty send attach flags, or
146          * with just KDBUS_ATTACH_CREDS, this should fail
147          */
148         conn = __kdbus_hello(buspath, 0, 0, 0);
149         ASSERT_RETURN(conn == NULL);
150         ASSERT_RETURN(errno == ECONNREFUSED);
151
152         conn = __kdbus_hello(buspath, 0, KDBUS_ATTACH_CREDS,
153                              _KDBUS_ATTACH_ALL);
154         ASSERT_RETURN(conn == NULL);
155         ASSERT_RETURN(errno == ECONNREFUSED);
156
157         conn = __kdbus_hello(buspath, 0, _KDBUS_ATTACH_ALL, 0);
158         ASSERT_RETURN(conn);
159
160         /* Try to cut back some send attach flags */
161         ret = kdbus_conn_update_attach_flags(conn,
162                                              KDBUS_ATTACH_CREDS|
163                                              KDBUS_ATTACH_PIDS,
164                                              _KDBUS_ATTACH_ALL);
165         ASSERT_RETURN(ret == -EINVAL);
166
167         ret = kdbus_conn_update_attach_flags(conn,
168                                              _KDBUS_ATTACH_ALL, 0);
169         ASSERT_RETURN(ret == 0);
170
171         kdbus_conn_free(conn);
172         free(path);
173         free(busname);
174         close(control_fd);
175
176
177         /* Test a new bus with KDBUS_ATTACH_PIDS */
178
179         control_fd = open(control_path, O_RDWR);
180         ASSERT_RETURN(control_fd >= 0);
181
182         busname = unique_name("test-peer-flags-bus");
183         ASSERT_RETURN(busname);
184
185         ret = kdbus_create_bus(control_fd, busname, KDBUS_ATTACH_PIDS,
186                                0, &path);
187         ASSERT_RETURN(ret == 0);
188
189         snprintf(buspath, sizeof(buspath), "%s/%s/bus", env->root, path);
190
191         /*
192          * Create a connection with an empty send attach flags, or
193          * all flags except KDBUS_ATTACH_PIDS
194          */
195         conn = __kdbus_hello(buspath, 0, 0, 0);
196         ASSERT_RETURN(conn == NULL);
197         ASSERT_RETURN(errno == ECONNREFUSED);
198
199         conn = __kdbus_hello(buspath, 0,
200                              _KDBUS_ATTACH_ALL & ~KDBUS_ATTACH_PIDS,
201                              _KDBUS_ATTACH_ALL);
202         ASSERT_RETURN(conn == NULL);
203         ASSERT_RETURN(errno == ECONNREFUSED);
204
205         /* The following should succeed */
206         conn = __kdbus_hello(buspath, 0, KDBUS_ATTACH_PIDS, 0);
207         ASSERT_RETURN(conn);
208         kdbus_conn_free(conn);
209
210         conn = __kdbus_hello(buspath, 0, _KDBUS_ATTACH_ALL, 0);
211         ASSERT_RETURN(conn);
212
213         ret = kdbus_conn_update_attach_flags(conn,
214                                              _KDBUS_ATTACH_ALL &
215                                              ~KDBUS_ATTACH_PIDS,
216                                              _KDBUS_ATTACH_ALL);
217         ASSERT_RETURN(ret == -EINVAL);
218
219         ret = kdbus_conn_update_attach_flags(conn, 0,
220                                              _KDBUS_ATTACH_ALL);
221         ASSERT_RETURN(ret == -EINVAL);
222
223         /* Now we want only KDBUS_ATTACH_PIDS */
224         ret = kdbus_conn_update_attach_flags(conn,
225                                              KDBUS_ATTACH_PIDS, 0);
226         ASSERT_RETURN(ret == 0);
227
228         kdbus_conn_free(conn);
229         free(path);
230         free(busname);
231         close(control_fd);
232
233
234         /*
235          * Create bus with 0 as ATTACH flags, the bus does not
236          * require any attach flags
237          */
238
239         control_fd = open(control_path, O_RDWR);
240         ASSERT_RETURN(control_fd >= 0);
241
242         busname = unique_name("test-peer-flags-bus");
243         ASSERT_RETURN(busname);
244
245         ret = kdbus_create_bus(control_fd, busname, 0, 0, &path);
246         ASSERT_RETURN(ret == 0);
247
248         snprintf(buspath, sizeof(buspath), "%s/%s/bus", env->root, path);
249
250         /* Bus is open it does not require any send attach flags */
251         conn = __kdbus_hello(buspath, 0, 0, 0);
252         ASSERT_RETURN(conn);
253         kdbus_conn_free(conn);
254
255         conn = __kdbus_hello(buspath, 0, _KDBUS_ATTACH_ALL, 0);
256         ASSERT_RETURN(conn);
257
258         ret = kdbus_conn_update_attach_flags(conn, 0, 0);
259         ASSERT_RETURN(ret == 0);
260
261         ret = kdbus_conn_update_attach_flags(conn, KDBUS_ATTACH_CREDS, 0);
262         ASSERT_RETURN(ret == 0);
263
264         kdbus_conn_free(conn);
265         free(path);
266         free(busname);
267         close(control_fd);
268
269         return 0;
270 }
271
272 static int kdbus_test_peers_info(struct kdbus_test_env *env)
273 {
274         int ret;
275         int control_fd;
276         char *path;
277         char *busname;
278         unsigned int i = 0;
279         uint64_t offset = 0;
280         char buspath[2048];
281         char control_path[2048];
282         uint64_t attach_flags_mask;
283         struct kdbus_item *item;
284         struct kdbus_info *info;
285         struct kdbus_conn *conn;
286         struct kdbus_conn *reader;
287         unsigned long long attach_count = 0;
288
289         snprintf(control_path, sizeof(control_path),
290                  "%s/control", env->root);
291
292         attach_flags_mask = 0;
293         ret = kdbus_sysfs_set_parameter_mask(env->mask_param_path,
294                                              attach_flags_mask);
295         ASSERT_RETURN(ret == 0);
296
297         control_fd = open(control_path, O_RDWR);
298         ASSERT_RETURN(control_fd >= 0);
299
300         busname = unique_name("test-peers-info-bus");
301         ASSERT_RETURN(busname);
302
303         ret = kdbus_create_bus(control_fd, busname, _KDBUS_ATTACH_ALL,
304                                0, &path);
305         ASSERT_RETURN(ret == 0);
306
307         snprintf(buspath, sizeof(buspath), "%s/%s/bus", env->root, path);
308
309         /* Create connections with the appropriate flags */
310         conn = __kdbus_hello(buspath, 0, _KDBUS_ATTACH_ALL, 0);
311         ASSERT_RETURN(conn);
312
313         reader = __kdbus_hello(buspath, 0, _KDBUS_ATTACH_ALL, 0);
314         ASSERT_RETURN(reader);
315
316         ret = kdbus_conn_info(reader, conn->id, NULL,
317                               _KDBUS_ATTACH_ALL, &offset);
318         ASSERT_RETURN(ret == 0);
319
320         info = (struct kdbus_info *)(reader->buf + offset);
321         ASSERT_RETURN(info->id == conn->id);
322
323         /* all attach flags are masked, no metadata */
324         KDBUS_ITEM_FOREACH(item, info, items)
325                 i++;
326
327         ASSERT_RETURN(i == 0);
328
329         kdbus_free(reader, offset);
330
331         /* Set the mask to _KDBUS_ATTACH_ANY */
332         attach_flags_mask = _KDBUS_ATTACH_ANY;
333         ret = kdbus_sysfs_set_parameter_mask(env->mask_param_path,
334                                              attach_flags_mask);
335         ASSERT_RETURN(ret == 0);
336
337         ret = kdbus_conn_info(reader, conn->id, NULL,
338                               _KDBUS_ATTACH_ALL, &offset);
339         ASSERT_RETURN(ret == 0);
340
341         info = (struct kdbus_info *)(reader->buf + offset);
342         ASSERT_RETURN(info->id == conn->id);
343
344         attach_count = 0;
345         KDBUS_ITEM_FOREACH(item, info, items)
346                     attach_count += item->type;
347
348         /*
349          * All flags have been returned except for:
350          * KDBUS_ITEM_TIMESTAMP and
351          * KDBUS_ITEM_OWNED_NAME we do not own any name.
352          */
353         ASSERT_RETURN(attach_count == (KDBUS_TEST_ITEMS_SUM -
354                                        KDBUS_ITEM_OWNED_NAME -
355                                        KDBUS_ITEM_TIMESTAMP));
356
357         kdbus_free(reader, offset);
358
359         /* Request only OWNED names */
360         ret = kdbus_conn_info(reader, conn->id, NULL,
361                               KDBUS_ATTACH_NAMES, &offset);
362         ASSERT_RETURN(ret == 0);
363
364         info = (struct kdbus_info *)(reader->buf + offset);
365         ASSERT_RETURN(info->id == conn->id);
366
367         attach_count = 0;
368         KDBUS_ITEM_FOREACH(item, info, items)
369                 attach_count += item->type;
370
371         /* we should not get any metadata since we do not own names */
372         ASSERT_RETURN(attach_count == 0);
373
374         kdbus_free(reader, offset);
375
376         kdbus_conn_free(conn);
377         kdbus_conn_free(reader);
378
379         return 0;
380 }
381
382 /**
383  * @kdbus_mask_param:   kdbus module mask parameter (system-wide)
384  * @requested_meta:     The bus owner metadata that we want
385  * @expected_items:     The returned KDBUS_ITEMS_* sum. Used to
386  *                      validate the returned metadata items
387  */
388 static int kdbus_cmp_bus_creator_metadata(struct kdbus_test_env *env,
389                                           struct kdbus_conn *conn,
390                                           uint64_t kdbus_mask_param,
391                                           uint64_t requested_meta,
392                                           unsigned long expected_items)
393 {
394         int ret;
395         uint64_t offset = 0;
396         struct kdbus_info *info;
397         struct kdbus_item *item;
398         unsigned long attach_count = 0;
399
400         ret = kdbus_sysfs_set_parameter_mask(env->mask_param_path,
401                                              kdbus_mask_param);
402         ASSERT_RETURN(ret == 0);
403
404         ret = kdbus_bus_creator_info(conn, requested_meta, &offset);
405         ASSERT_RETURN(ret == 0);
406
407         info = (struct kdbus_info *)(conn->buf + offset);
408
409         KDBUS_ITEM_FOREACH(item, info, items)
410                 attach_count += item->type;
411
412         ASSERT_RETURN(attach_count == expected_items);
413
414         ret = kdbus_free(conn, offset);
415         ASSERT_RETURN(ret == 0);
416
417         return 0;
418 }
419
420 static int kdbus_test_bus_creator_info(struct kdbus_test_env *env)
421 {
422         int ret;
423         int control_fd;
424         char *path;
425         char *busname;
426         char buspath[2048];
427         char control_path[2048];
428         uint64_t attach_flags_mask;
429         struct kdbus_conn *conn;
430         unsigned long expected_items = 0;
431
432         snprintf(control_path, sizeof(control_path),
433                  "%s/control", env->root);
434
435         control_fd = open(control_path, O_RDWR);
436         ASSERT_RETURN(control_fd >= 0);
437
438         busname = unique_name("test-peers-info-bus");
439         ASSERT_RETURN(busname);
440
441         /*
442          * Now the bus allows us to see all its KDBUS_ATTACH_*
443          * items
444          */
445         ret = kdbus_create_bus(control_fd, busname, 0,
446                                _KDBUS_ATTACH_ALL, &path);
447         ASSERT_RETURN(ret == 0);
448
449         snprintf(buspath, sizeof(buspath), "%s/%s/bus", env->root, path);
450
451         conn = __kdbus_hello(buspath, 0, 0, 0);
452         ASSERT_RETURN(conn);
453
454         /*
455          * Start with a kdbus module mask set to _KDBUS_ATTACH_ANY
456          */
457         attach_flags_mask = _KDBUS_ATTACH_ANY;
458
459         /*
460          * All flags will be returned except for:
461          * KDBUS_ITEM_TIMESTAMP
462          * KDBUS_ITEM_OWNED_NAME
463          * KDBUS_ITEM_CONN_DESCRIPTION
464          *
465          * An extra flags is always returned KDBUS_ITEM_MAKE_NAME
466          * which contains the bus name
467          */
468         expected_items = KDBUS_TEST_ITEMS_SUM + KDBUS_ITEM_MAKE_NAME;
469         expected_items -= KDBUS_ITEM_TIMESTAMP +
470                           KDBUS_ITEM_OWNED_NAME +
471                           KDBUS_ITEM_CONN_DESCRIPTION;
472         ret = kdbus_cmp_bus_creator_metadata(env, conn,
473                                              attach_flags_mask,
474                                              _KDBUS_ATTACH_ALL,
475                                              expected_items);
476         ASSERT_RETURN(ret == 0);
477
478         /*
479          * We should have:
480          * KDBUS_ITEM_PIDS + KDBUS_ITEM_CREDS + KDBUS_ITEM_MAKE_NAME
481          */
482         expected_items = KDBUS_ITEM_PIDS + KDBUS_ITEM_CREDS +
483                          KDBUS_ITEM_MAKE_NAME;
484         ret = kdbus_cmp_bus_creator_metadata(env, conn,
485                                              attach_flags_mask,
486                                              KDBUS_ATTACH_PIDS |
487                                              KDBUS_ATTACH_CREDS,
488                                              expected_items);
489         ASSERT_RETURN(ret == 0);
490
491         /* KDBUS_ITEM_MAKE_NAME is always returned */
492         expected_items = KDBUS_ITEM_MAKE_NAME;
493         ret = kdbus_cmp_bus_creator_metadata(env, conn,
494                                              attach_flags_mask,
495                                              0, expected_items);
496         ASSERT_RETURN(ret == 0);
497
498         /*
499          * Restrict kdbus system-wide mask to KDBUS_ATTACH_PIDS
500          */
501
502         attach_flags_mask = KDBUS_ATTACH_PIDS;
503
504         /*
505          * We should have:
506          * KDBUS_ITEM_PIDS + KDBUS_ITEM_MAKE_NAME
507          */
508         expected_items = KDBUS_ITEM_PIDS + KDBUS_ITEM_MAKE_NAME;
509         ret = kdbus_cmp_bus_creator_metadata(env, conn,
510                                              attach_flags_mask,
511                                              _KDBUS_ATTACH_ALL,
512                                              expected_items);
513         ASSERT_RETURN(ret == 0);
514
515
516         /* system-wide mask to 0 */
517         attach_flags_mask = 0;
518
519         /* we should only see: KDBUS_ITEM_MAKE_NAME */
520         expected_items = KDBUS_ITEM_MAKE_NAME;
521         ret = kdbus_cmp_bus_creator_metadata(env, conn,
522                                              attach_flags_mask,
523                                              _KDBUS_ATTACH_ALL,
524                                              expected_items);
525         ASSERT_RETURN(ret == 0);
526
527         kdbus_conn_free(conn);
528         free(path);
529         free(busname);
530         close(control_fd);
531
532
533         /*
534          * A new bus that hides all its owner metadata
535          */
536
537         control_fd = open(control_path, O_RDWR);
538         ASSERT_RETURN(control_fd >= 0);
539
540         busname = unique_name("test-peers-info-bus");
541         ASSERT_RETURN(busname);
542
543         ret = kdbus_create_bus(control_fd, busname, 0, 0, &path);
544         ASSERT_RETURN(ret == 0);
545
546         snprintf(buspath, sizeof(buspath), "%s/%s/bus", env->root, path);
547
548         conn = __kdbus_hello(buspath, 0, 0, 0);
549         ASSERT_RETURN(conn);
550
551         /*
552          * Start with a kdbus module mask set to _KDBUS_ATTACH_ANY
553          */
554         attach_flags_mask = _KDBUS_ATTACH_ANY;
555
556         /*
557          * We only get the KDBUS_ITEM_MAKE_NAME
558          */
559         expected_items = KDBUS_ITEM_MAKE_NAME;
560         ret = kdbus_cmp_bus_creator_metadata(env, conn,
561                                              attach_flags_mask,
562                                              _KDBUS_ATTACH_ALL,
563                                              expected_items);
564         ASSERT_RETURN(ret == 0);
565
566         /*
567          * We still get only kdbus_ITEM_MAKE_NAME
568          */
569         attach_flags_mask = 0;
570         expected_items = KDBUS_ITEM_MAKE_NAME;
571         ret = kdbus_cmp_bus_creator_metadata(env, conn,
572                                              attach_flags_mask,
573                                              _KDBUS_ATTACH_ALL,
574                                              expected_items);
575         ASSERT_RETURN(ret == 0);
576
577         kdbus_conn_free(conn);
578         free(path);
579         free(busname);
580         close(control_fd);
581
582
583         /*
584          * A new bus that shows only the PID and CREDS metadata
585          * of the bus owner.
586          */
587         control_fd = open(control_path, O_RDWR);
588         ASSERT_RETURN(control_fd >= 0);
589
590         busname = unique_name("test-peers-info-bus");
591         ASSERT_RETURN(busname);
592
593         ret = kdbus_create_bus(control_fd, busname, 0,
594                                KDBUS_ATTACH_PIDS|
595                                KDBUS_ATTACH_CREDS, &path);
596         ASSERT_RETURN(ret == 0);
597
598         snprintf(buspath, sizeof(buspath), "%s/%s/bus", env->root, path);
599
600         conn = __kdbus_hello(buspath, 0, 0, 0);
601         ASSERT_RETURN(conn);
602
603         /*
604          * Start with a kdbus module mask set to _KDBUS_ATTACH_ANY
605          */
606         attach_flags_mask = _KDBUS_ATTACH_ANY;
607
608         /*
609          * We should have:
610          * KDBUS_ITEM_PIDS + KDBUS_ITEM_CREDS + KDBUS_ITEM_MAKE_NAME
611          */
612         expected_items = KDBUS_ITEM_PIDS + KDBUS_ITEM_CREDS +
613                          KDBUS_ITEM_MAKE_NAME;
614         ret = kdbus_cmp_bus_creator_metadata(env, conn,
615                                              attach_flags_mask,
616                                              _KDBUS_ATTACH_ALL,
617                                              expected_items);
618         ASSERT_RETURN(ret == 0);
619
620         expected_items = KDBUS_ITEM_CREDS + KDBUS_ITEM_MAKE_NAME;
621         ret = kdbus_cmp_bus_creator_metadata(env, conn,
622                                              attach_flags_mask,
623                                              KDBUS_ATTACH_CREDS,
624                                              expected_items);
625         ASSERT_RETURN(ret == 0);
626
627         /* KDBUS_ITEM_MAKE_NAME is always returned */
628         expected_items = KDBUS_ITEM_MAKE_NAME;
629         ret = kdbus_cmp_bus_creator_metadata(env, conn,
630                                              attach_flags_mask,
631                                              0, expected_items);
632         ASSERT_RETURN(ret == 0);
633
634         /*
635          * Restrict kdbus system-wide mask to KDBUS_ATTACH_PIDS
636          */
637
638         attach_flags_mask = KDBUS_ATTACH_PIDS;
639         /*
640          * We should have:
641          * KDBUS_ITEM_PIDS + KDBUS_ITEM_MAKE_NAME
642          */
643         expected_items = KDBUS_ITEM_PIDS + KDBUS_ITEM_MAKE_NAME;
644         ret = kdbus_cmp_bus_creator_metadata(env, conn,
645                                              attach_flags_mask,
646                                              _KDBUS_ATTACH_ALL,
647                                              expected_items);
648         ASSERT_RETURN(ret == 0);
649
650         /* No KDBUS_ATTACH_CREDS */
651         expected_items = KDBUS_ITEM_MAKE_NAME;
652         ret = kdbus_cmp_bus_creator_metadata(env, conn,
653                                              attach_flags_mask,
654                                              KDBUS_ATTACH_CREDS,
655                                              expected_items);
656         ASSERT_RETURN(ret == 0);
657
658         /* system-wide mask to 0 */
659         attach_flags_mask = 0;
660
661         /* we should only see: KDBUS_ITEM_MAKE_NAME */
662         expected_items = KDBUS_ITEM_MAKE_NAME;
663         ret = kdbus_cmp_bus_creator_metadata(env, conn,
664                                              attach_flags_mask,
665                                              _KDBUS_ATTACH_ALL,
666                                              expected_items);
667         ASSERT_RETURN(ret == 0);
668
669
670         kdbus_conn_free(conn);
671         free(path);
672         free(busname);
673         close(control_fd);
674
675         return 0;
676 }
677
678 int kdbus_test_attach_flags(struct kdbus_test_env *env)
679 {
680         int ret;
681         uint64_t flags_mask;
682         uint64_t old_kdbus_flags_mask;
683
684         /* We need CAP_DAC_OVERRIDE to overwrite the kdbus mask */
685         ret = test_is_capable(CAP_DAC_OVERRIDE, -1);
686         ASSERT_RETURN(ret >= 0);
687
688         /* no enough privileges, SKIP test */
689         if (!ret)
690                 return TEST_SKIP;
691
692         /*
693          * We need to be able to write to
694          * "/sys/module/kdbus/parameters/attach_flags_mask"
695          * perhaps we are unprvileged/privileged in its userns
696          */
697         ret = access(env->mask_param_path, W_OK);
698         if (ret < 0) {
699                 kdbus_printf("--- access() '%s' failed: %d (%m)\n",
700                              env->mask_param_path, -errno);
701                 return TEST_SKIP;
702         }
703
704         ret = kdbus_sysfs_get_parameter_mask(env->mask_param_path,
705                                              &old_kdbus_flags_mask);
706         ASSERT_RETURN(ret == 0);
707
708         /* setup the right KDBUS_TEST_ITEMS_SUM */
709         if (!config_auditsyscall_is_enabled())
710                 KDBUS_TEST_ITEMS_SUM -= KDBUS_ITEM_AUDIT;
711
712         if (!config_cgroups_is_enabled())
713                 KDBUS_TEST_ITEMS_SUM -= KDBUS_ITEM_CGROUP;
714
715         if (!config_security_is_enabled())
716                 KDBUS_TEST_ITEMS_SUM -= KDBUS_ITEM_SECLABEL;
717
718         /*
719          * Test the connection creation attach flags
720          */
721         ret = kdbus_test_peers_creation(env);
722         /* Restore previous kdbus mask */
723         kdbus_sysfs_set_parameter_mask(env->mask_param_path,
724                                        old_kdbus_flags_mask);
725         ASSERT_RETURN(ret == 0);
726
727         /*
728          * Test the CONN_INFO attach flags
729          */
730         ret = kdbus_test_peers_info(env);
731         /* Restore previous kdbus mask */
732         kdbus_sysfs_set_parameter_mask(env->mask_param_path,
733                                        old_kdbus_flags_mask);
734         ASSERT_RETURN(ret == 0);
735
736         /*
737          * Test the Bus creator info and its attach flags
738          */
739         ret = kdbus_test_bus_creator_info(env);
740         /* Restore previous kdbus mask */
741         kdbus_sysfs_set_parameter_mask(env->mask_param_path,
742                                        old_kdbus_flags_mask);
743         ASSERT_RETURN(ret == 0);
744
745         ret = kdbus_sysfs_get_parameter_mask(env->mask_param_path,
746                                              &flags_mask);
747         ASSERT_RETURN(ret == 0 && old_kdbus_flags_mask == flags_mask);
748
749         return TEST_OK;
750 }