2 This file is part of systemd.
4 Copyright 2015 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <sys/prctl.h>
24 #include "alloc-util.h"
25 #include "bus-common-errors.h"
29 #include "hostname-util.h"
30 #include "import-util.h"
31 #include "machine-pool.h"
34 #include "parse-util.h"
35 #include "path-util.h"
36 #include "process-util.h"
37 #include "signal-util.h"
38 #include "socket-util.h"
39 #include "string-table.h"
41 #include "syslog-util.h"
42 #include "user-util.h"
46 typedef struct Transfer Transfer
;
47 typedef struct Manager Manager
;
49 typedef enum TransferType
{
57 _TRANSFER_TYPE_INVALID
= -1,
80 char log_message
[LINE_MAX
];
81 size_t log_message_size
;
83 sd_event_source
*pid_event_source
;
84 sd_event_source
*log_event_source
;
87 unsigned progress_percent
;
97 uint32_t current_transfer_id
;
100 Hashmap
*polkit_registry
;
104 sd_event_source
*notify_event_source
;
107 #define TRANSFERS_MAX 64
109 static const char* const transfer_type_table
[_TRANSFER_TYPE_MAX
] = {
110 [TRANSFER_IMPORT_TAR
] = "import-tar",
111 [TRANSFER_IMPORT_RAW
] = "import-raw",
112 [TRANSFER_EXPORT_TAR
] = "export-tar",
113 [TRANSFER_EXPORT_RAW
] = "export-raw",
114 [TRANSFER_PULL_TAR
] = "pull-tar",
115 [TRANSFER_PULL_RAW
] = "pull-raw",
118 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(transfer_type
, TransferType
);
120 static Transfer
*transfer_unref(Transfer
*t
) {
125 hashmap_remove(t
->manager
->transfers
, UINT32_TO_PTR(t
->id
));
127 sd_event_source_unref(t
->pid_event_source
);
128 sd_event_source_unref(t
->log_event_source
);
133 free(t
->object_path
);
136 (void) kill_and_sigcont(t
->pid
, SIGKILL
);
137 (void) wait_for_terminate(t
->pid
, NULL
);
140 safe_close(t
->log_fd
);
141 safe_close(t
->stdin_fd
);
142 safe_close(t
->stdout_fd
);
147 DEFINE_TRIVIAL_CLEANUP_FUNC(Transfer
*, transfer_unref
);
149 static int transfer_new(Manager
*m
, Transfer
**ret
) {
150 _cleanup_(transfer_unrefp
) Transfer
*t
= NULL
;
157 if (hashmap_size(m
->transfers
) >= TRANSFERS_MAX
)
160 r
= hashmap_ensure_allocated(&m
->transfers
, &trivial_hash_ops
);
164 t
= new0(Transfer
, 1);
168 t
->type
= _TRANSFER_TYPE_INVALID
;
172 t
->verify
= _IMPORT_VERIFY_INVALID
;
174 id
= m
->current_transfer_id
+ 1;
176 if (asprintf(&t
->object_path
, "/org/freedesktop/import1/transfer/_%" PRIu32
, id
) < 0)
179 r
= hashmap_put(m
->transfers
, UINT32_TO_PTR(id
), t
);
183 m
->current_transfer_id
= id
;
194 static void transfer_send_log_line(Transfer
*t
, const char *line
) {
195 int r
, priority
= LOG_INFO
;
200 syslog_parse_priority(&line
, &priority
, true);
202 log_full(priority
, "(transfer%" PRIu32
") %s", t
->id
, line
);
204 r
= sd_bus_emit_signal(
207 "org.freedesktop.import1.Transfer",
213 log_error_errno(r
, "Cannot emit message: %m");
216 static void transfer_send_logs(Transfer
*t
, bool flush
) {
219 /* Try to send out all log messages, if we can. But if we
220 * can't we remove the messages from the buffer, but don't
223 while (t
->log_message_size
> 0) {
224 _cleanup_free_
char *n
= NULL
;
227 if (t
->log_message_size
>= sizeof(t
->log_message
))
228 e
= t
->log_message
+ sizeof(t
->log_message
);
232 a
= memchr(t
->log_message
, 0, t
->log_message_size
);
233 b
= memchr(t
->log_message
, '\n', t
->log_message_size
);
247 e
= t
->log_message
+ t
->log_message_size
;
250 n
= strndup(t
->log_message
, e
- t
->log_message
);
252 /* Skip over NUL and newlines */
253 while ((e
< t
->log_message
+ t
->log_message_size
) && (*e
== 0 || *e
== '\n'))
256 memmove(t
->log_message
, e
, t
->log_message
+ sizeof(t
->log_message
) - e
);
257 t
->log_message_size
-= e
- t
->log_message
;
267 transfer_send_log_line(t
, n
);
271 static int transfer_finalize(Transfer
*t
, bool success
) {
276 transfer_send_logs(t
, true);
278 r
= sd_bus_emit_signal(
280 "/org/freedesktop/import1",
281 "org.freedesktop.import1.Manager",
287 t
->n_canceled
> 0 ? "canceled" : "failed");
290 log_error_errno(r
, "Cannot emit message: %m");
296 static int transfer_cancel(Transfer
*t
) {
301 r
= kill_and_sigcont(t
->pid
, t
->n_canceled
< 3 ? SIGTERM
: SIGKILL
);
309 static int transfer_on_pid(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
310 Transfer
*t
= userdata
;
311 bool success
= false;
316 if (si
->si_code
== CLD_EXITED
) {
317 if (si
->si_status
!= 0)
318 log_error("Import process failed with exit code %i.", si
->si_status
);
320 log_debug("Import process succeeded.");
324 } else if (si
->si_code
== CLD_KILLED
||
325 si
->si_code
== CLD_DUMPED
)
327 log_error("Import process terminated by signal %s.", signal_to_string(si
->si_status
));
329 log_error("Import process failed due to unknown reason.");
333 return transfer_finalize(t
, success
);
336 static int transfer_on_log(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
337 Transfer
*t
= userdata
;
343 l
= read(fd
, t
->log_message
+ t
->log_message_size
, sizeof(t
->log_message
) - t
->log_message_size
);
345 /* EOF/read error. We just close the pipe here, and
346 * close the watch, waiting for the SIGCHLD to arrive,
347 * before we do anything else. */
350 log_error_errno(errno
, "Failed to read log message: %m");
352 t
->log_event_source
= sd_event_source_unref(t
->log_event_source
);
356 t
->log_message_size
+= l
;
358 transfer_send_logs(t
, false);
363 static int transfer_start(Transfer
*t
) {
364 _cleanup_close_pair_
int pipefd
[2] = { -1, -1 };
370 if (pipe2(pipefd
, O_CLOEXEC
) < 0)
377 const char *cmd
[] = {
378 NULL
, /* systemd-import, systemd-export or systemd-pull */
380 NULL
, /* --verify= */
381 NULL
, /* verify argument */
382 NULL
, /* maybe --force */
383 NULL
, /* maybe --read-only */
384 NULL
, /* if so: the actual URL */
385 NULL
, /* maybe --format= */
386 NULL
, /* if so: the actual format */
395 (void) reset_all_signal_handlers();
396 (void) reset_signal_mask();
397 assert_se(prctl(PR_SET_PDEATHSIG
, SIGTERM
) == 0);
399 pipefd
[0] = safe_close(pipefd
[0]);
401 if (dup2(pipefd
[1], STDERR_FILENO
) != STDERR_FILENO
) {
402 log_error_errno(errno
, "Failed to dup2() fd: %m");
406 if (t
->stdout_fd
>= 0) {
407 if (dup2(t
->stdout_fd
, STDOUT_FILENO
) != STDOUT_FILENO
) {
408 log_error_errno(errno
, "Failed to dup2() fd: %m");
412 if (t
->stdout_fd
!= STDOUT_FILENO
)
413 safe_close(t
->stdout_fd
);
415 if (dup2(pipefd
[1], STDOUT_FILENO
) != STDOUT_FILENO
) {
416 log_error_errno(errno
, "Failed to dup2() fd: %m");
421 if (pipefd
[1] != STDOUT_FILENO
&& pipefd
[1] != STDERR_FILENO
)
422 pipefd
[1] = safe_close(pipefd
[1]);
424 if (t
->stdin_fd
>= 0) {
425 if (dup2(t
->stdin_fd
, STDIN_FILENO
) != STDIN_FILENO
) {
426 log_error_errno(errno
, "Failed to dup2() fd: %m");
430 if (t
->stdin_fd
!= STDIN_FILENO
)
431 safe_close(t
->stdin_fd
);
435 null_fd
= open("/dev/null", O_RDONLY
|O_NOCTTY
);
437 log_error_errno(errno
, "Failed to open /dev/null: %m");
441 if (dup2(null_fd
, STDIN_FILENO
) != STDIN_FILENO
) {
442 log_error_errno(errno
, "Failed to dup2() fd: %m");
446 if (null_fd
!= STDIN_FILENO
)
450 stdio_unset_cloexec();
452 setenv("SYSTEMD_LOG_TARGET", "console-prefixed", 1);
453 setenv("NOTIFY_SOCKET", "/run/systemd/import/notify", 1);
455 if (IN_SET(t
->type
, TRANSFER_IMPORT_TAR
, TRANSFER_IMPORT_RAW
))
456 cmd
[k
++] = SYSTEMD_IMPORT_PATH
;
457 else if (IN_SET(t
->type
, TRANSFER_EXPORT_TAR
, TRANSFER_EXPORT_RAW
))
458 cmd
[k
++] = SYSTEMD_EXPORT_PATH
;
460 cmd
[k
++] = SYSTEMD_PULL_PATH
;
462 if (IN_SET(t
->type
, TRANSFER_IMPORT_TAR
, TRANSFER_EXPORT_TAR
, TRANSFER_PULL_TAR
))
467 if (t
->verify
!= _IMPORT_VERIFY_INVALID
) {
468 cmd
[k
++] = "--verify";
469 cmd
[k
++] = import_verify_to_string(t
->verify
);
473 cmd
[k
++] = "--force";
475 cmd
[k
++] = "--read-only";
478 cmd
[k
++] = "--format";
479 cmd
[k
++] = t
->format
;
482 if (!IN_SET(t
->type
, TRANSFER_EXPORT_TAR
, TRANSFER_EXPORT_RAW
)) {
484 cmd
[k
++] = t
->remote
;
493 execv(cmd
[0], (char * const *) cmd
);
494 log_error_errno(errno
, "Failed to execute %s tool: %m", cmd
[0]);
498 pipefd
[1] = safe_close(pipefd
[1]);
499 t
->log_fd
= pipefd
[0];
502 t
->stdin_fd
= safe_close(t
->stdin_fd
);
504 r
= sd_event_add_child(t
->manager
->event
, &t
->pid_event_source
, t
->pid
, WEXITED
, transfer_on_pid
, t
);
508 r
= sd_event_add_io(t
->manager
->event
, &t
->log_event_source
, t
->log_fd
, EPOLLIN
, transfer_on_log
, t
);
512 /* Make sure always process logging before SIGCHLD */
513 r
= sd_event_source_set_priority(t
->log_event_source
, SD_EVENT_PRIORITY_NORMAL
-5);
517 r
= sd_bus_emit_signal(
519 "/org/freedesktop/import1",
520 "org.freedesktop.import1.Manager",
531 static Manager
*manager_unref(Manager
*m
) {
537 sd_event_source_unref(m
->notify_event_source
);
538 safe_close(m
->notify_fd
);
540 while ((t
= hashmap_first(m
->transfers
)))
543 hashmap_free(m
->transfers
);
545 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
547 m
->bus
= sd_bus_flush_close_unref(m
->bus
);
548 sd_event_unref(m
->event
);
553 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager
*, manager_unref
);
555 static int manager_on_notify(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
557 char buf
[NOTIFY_BUFFER_MAX
+1];
558 struct iovec iovec
= {
560 .iov_len
= sizeof(buf
)-1,
563 struct cmsghdr cmsghdr
;
564 uint8_t buf
[CMSG_SPACE(sizeof(struct ucred
)) +
565 CMSG_SPACE(sizeof(int) * NOTIFY_FD_MAX
)];
567 struct msghdr msghdr
= {
570 .msg_control
= &control
,
571 .msg_controllen
= sizeof(control
),
573 struct ucred
*ucred
= NULL
;
574 Manager
*m
= userdata
;
575 struct cmsghdr
*cmsg
;
583 n
= recvmsg(fd
, &msghdr
, MSG_DONTWAIT
|MSG_CMSG_CLOEXEC
);
585 if (errno
== EAGAIN
|| errno
== EINTR
)
591 cmsg_close_all(&msghdr
);
593 CMSG_FOREACH(cmsg
, &msghdr
)
594 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
595 cmsg
->cmsg_type
== SCM_CREDENTIALS
&&
596 cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)))
597 ucred
= (struct ucred
*) CMSG_DATA(cmsg
);
599 if (msghdr
.msg_flags
& MSG_TRUNC
) {
600 log_warning("Got overly long notification datagram, ignoring.");
604 if (!ucred
|| ucred
->pid
<= 0) {
605 log_warning("Got notification datagram lacking credential information, ignoring.");
609 HASHMAP_FOREACH(t
, m
->transfers
, i
)
610 if (ucred
->pid
== t
->pid
)
614 log_warning("Got notification datagram from unexpected peer, ignoring.");
620 p
= startswith(buf
, "X_IMPORT_PROGRESS=");
622 p
= strstr(buf
, "\nX_IMPORT_PROGRESS=");
629 e
= strchrnul(p
, '\n');
632 r
= safe_atou(p
, &percent
);
633 if (r
< 0 || percent
> 100) {
634 log_warning("Got invalid percent value, ignoring.");
638 t
->progress_percent
= percent
;
640 log_debug("Got percentage from client: %u%%", percent
);
644 static int manager_new(Manager
**ret
) {
645 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
646 static const union sockaddr_union sa
= {
647 .un
.sun_family
= AF_UNIX
,
648 .un
.sun_path
= "/run/systemd/import/notify",
650 static const int one
= 1;
655 m
= new0(Manager
, 1);
659 r
= sd_event_default(&m
->event
);
663 sd_event_set_watchdog(m
->event
, true);
665 r
= sd_bus_default_system(&m
->bus
);
669 m
->notify_fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
670 if (m
->notify_fd
< 0)
673 (void) mkdir_parents_label(sa
.un
.sun_path
, 0755);
674 (void) unlink(sa
.un
.sun_path
);
676 if (bind(m
->notify_fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
)) < 0)
679 if (setsockopt(m
->notify_fd
, SOL_SOCKET
, SO_PASSCRED
, &one
, sizeof(one
)) < 0)
682 r
= sd_event_add_io(m
->event
, &m
->notify_event_source
, m
->notify_fd
, EPOLLIN
, manager_on_notify
, m
);
692 static Transfer
*manager_find(Manager
*m
, TransferType type
, const char *remote
) {
698 assert(type
< _TRANSFER_TYPE_MAX
);
700 HASHMAP_FOREACH(t
, m
->transfers
, i
) {
702 if (t
->type
== type
&&
703 streq_ptr(t
->remote
, remote
))
710 static int method_import_tar_or_raw(sd_bus_message
*msg
, void *userdata
, sd_bus_error
*error
) {
711 _cleanup_(transfer_unrefp
) Transfer
*t
= NULL
;
712 int fd
, force
, read_only
, r
;
713 const char *local
, *object
;
714 Manager
*m
= userdata
;
721 r
= bus_verify_polkit_async(
724 "org.freedesktop.import1.import",
733 return 1; /* Will call us back */
735 r
= sd_bus_message_read(msg
, "hsbb", &fd
, &local
, &force
, &read_only
);
739 if (!machine_name_is_valid(local
))
740 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Local name %s is invalid", local
);
742 r
= setup_machine_directory((uint64_t) -1, error
);
746 type
= streq_ptr(sd_bus_message_get_member(msg
), "ImportTar") ? TRANSFER_IMPORT_TAR
: TRANSFER_IMPORT_RAW
;
748 r
= transfer_new(m
, &t
);
753 t
->force_local
= force
;
754 t
->read_only
= read_only
;
756 t
->local
= strdup(local
);
760 t
->stdin_fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
764 r
= transfer_start(t
);
768 object
= t
->object_path
;
772 return sd_bus_reply_method_return(msg
, "uo", id
, object
);
775 static int method_export_tar_or_raw(sd_bus_message
*msg
, void *userdata
, sd_bus_error
*error
) {
776 _cleanup_(transfer_unrefp
) Transfer
*t
= NULL
;
778 const char *local
, *object
, *format
;
779 Manager
*m
= userdata
;
786 r
= bus_verify_polkit_async(
789 "org.freedesktop.import1.export",
798 return 1; /* Will call us back */
800 r
= sd_bus_message_read(msg
, "shs", &local
, &fd
, &format
);
804 if (!machine_name_is_valid(local
))
805 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Local name %s is invalid", local
);
807 type
= streq_ptr(sd_bus_message_get_member(msg
), "ExportTar") ? TRANSFER_EXPORT_TAR
: TRANSFER_EXPORT_RAW
;
809 r
= transfer_new(m
, &t
);
815 if (!isempty(format
)) {
816 t
->format
= strdup(format
);
821 t
->local
= strdup(local
);
825 t
->stdout_fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
826 if (t
->stdout_fd
< 0)
829 r
= transfer_start(t
);
833 object
= t
->object_path
;
837 return sd_bus_reply_method_return(msg
, "uo", id
, object
);
840 static int method_pull_tar_or_raw(sd_bus_message
*msg
, void *userdata
, sd_bus_error
*error
) {
841 _cleanup_(transfer_unrefp
) Transfer
*t
= NULL
;
842 const char *remote
, *local
, *verify
, *object
;
843 Manager
*m
= userdata
;
852 r
= bus_verify_polkit_async(
855 "org.freedesktop.import1.pull",
864 return 1; /* Will call us back */
866 r
= sd_bus_message_read(msg
, "sssb", &remote
, &local
, &verify
, &force
);
870 if (!http_url_is_valid(remote
))
871 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "URL %s is invalid", remote
);
875 else if (!machine_name_is_valid(local
))
876 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Local name %s is invalid", local
);
879 v
= IMPORT_VERIFY_SIGNATURE
;
881 v
= import_verify_from_string(verify
);
883 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown verification mode %s", verify
);
885 r
= setup_machine_directory((uint64_t) -1, error
);
889 type
= streq_ptr(sd_bus_message_get_member(msg
), "PullTar") ? TRANSFER_PULL_TAR
: TRANSFER_PULL_RAW
;
891 if (manager_find(m
, type
, remote
))
892 return sd_bus_error_setf(error
, BUS_ERROR_TRANSFER_IN_PROGRESS
, "Transfer for %s already in progress.", remote
);
894 r
= transfer_new(m
, &t
);
900 t
->force_local
= force
;
902 t
->remote
= strdup(remote
);
907 t
->local
= strdup(local
);
912 r
= transfer_start(t
);
916 object
= t
->object_path
;
920 return sd_bus_reply_method_return(msg
, "uo", id
, object
);
923 static int method_list_transfers(sd_bus_message
*msg
, void *userdata
, sd_bus_error
*error
) {
924 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
925 Manager
*m
= userdata
;
933 r
= sd_bus_message_new_method_return(msg
, &reply
);
937 r
= sd_bus_message_open_container(reply
, 'a', "(usssdo)");
941 HASHMAP_FOREACH(t
, m
->transfers
, i
) {
943 r
= sd_bus_message_append(
947 transfer_type_to_string(t
->type
),
950 (double) t
->progress_percent
/ 100.0,
956 r
= sd_bus_message_close_container(reply
);
960 return sd_bus_send(NULL
, reply
, NULL
);
963 static int method_cancel(sd_bus_message
*msg
, void *userdata
, sd_bus_error
*error
) {
964 Transfer
*t
= userdata
;
970 r
= bus_verify_polkit_async(
973 "org.freedesktop.import1.pull",
977 &t
->manager
->polkit_registry
,
982 return 1; /* Will call us back */
984 r
= transfer_cancel(t
);
988 return sd_bus_reply_method_return(msg
, NULL
);
991 static int method_cancel_transfer(sd_bus_message
*msg
, void *userdata
, sd_bus_error
*error
) {
992 Manager
*m
= userdata
;
1000 r
= bus_verify_polkit_async(
1003 "org.freedesktop.import1.pull",
1007 &m
->polkit_registry
,
1012 return 1; /* Will call us back */
1014 r
= sd_bus_message_read(msg
, "u", &id
);
1018 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid transfer id");
1020 t
= hashmap_get(m
->transfers
, UINT32_TO_PTR(id
));
1022 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_TRANSFER
, "No transfer by id %" PRIu32
, id
);
1024 r
= transfer_cancel(t
);
1028 return sd_bus_reply_method_return(msg
, NULL
);
1031 static int property_get_progress(
1034 const char *interface
,
1035 const char *property
,
1036 sd_bus_message
*reply
,
1038 sd_bus_error
*error
) {
1040 Transfer
*t
= userdata
;
1046 return sd_bus_message_append(reply
, "d", (double) t
->progress_percent
/ 100.0);
1049 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type
, transfer_type
, TransferType
);
1050 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_verify
, import_verify
, ImportVerify
);
1052 static const sd_bus_vtable transfer_vtable
[] = {
1053 SD_BUS_VTABLE_START(0),
1054 SD_BUS_PROPERTY("Id", "u", NULL
, offsetof(Transfer
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
1055 SD_BUS_PROPERTY("Local", "s", NULL
, offsetof(Transfer
, local
), SD_BUS_VTABLE_PROPERTY_CONST
),
1056 SD_BUS_PROPERTY("Remote", "s", NULL
, offsetof(Transfer
, remote
), SD_BUS_VTABLE_PROPERTY_CONST
),
1057 SD_BUS_PROPERTY("Type", "s", property_get_type
, offsetof(Transfer
, type
), SD_BUS_VTABLE_PROPERTY_CONST
),
1058 SD_BUS_PROPERTY("Verify", "s", property_get_verify
, offsetof(Transfer
, verify
), SD_BUS_VTABLE_PROPERTY_CONST
),
1059 SD_BUS_PROPERTY("Progress", "d", property_get_progress
, 0, 0),
1060 SD_BUS_METHOD("Cancel", NULL
, NULL
, method_cancel
, SD_BUS_VTABLE_UNPRIVILEGED
),
1061 SD_BUS_SIGNAL("LogMessage", "us", 0),
1065 static const sd_bus_vtable manager_vtable
[] = {
1066 SD_BUS_VTABLE_START(0),
1067 SD_BUS_METHOD("ImportTar", "hsbb", "uo", method_import_tar_or_raw
, SD_BUS_VTABLE_UNPRIVILEGED
),
1068 SD_BUS_METHOD("ImportRaw", "hsbb", "uo", method_import_tar_or_raw
, SD_BUS_VTABLE_UNPRIVILEGED
),
1069 SD_BUS_METHOD("ExportTar", "shs", "uo", method_export_tar_or_raw
, SD_BUS_VTABLE_UNPRIVILEGED
),
1070 SD_BUS_METHOD("ExportRaw", "shs", "uo", method_export_tar_or_raw
, SD_BUS_VTABLE_UNPRIVILEGED
),
1071 SD_BUS_METHOD("PullTar", "sssb", "uo", method_pull_tar_or_raw
, SD_BUS_VTABLE_UNPRIVILEGED
),
1072 SD_BUS_METHOD("PullRaw", "sssb", "uo", method_pull_tar_or_raw
, SD_BUS_VTABLE_UNPRIVILEGED
),
1073 SD_BUS_METHOD("ListTransfers", NULL
, "a(usssdo)", method_list_transfers
, SD_BUS_VTABLE_UNPRIVILEGED
),
1074 SD_BUS_METHOD("CancelTransfer", "u", NULL
, method_cancel_transfer
, SD_BUS_VTABLE_UNPRIVILEGED
),
1075 SD_BUS_SIGNAL("TransferNew", "uo", 0),
1076 SD_BUS_SIGNAL("TransferRemoved", "uos", 0),
1080 static int transfer_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
1081 Manager
*m
= userdata
;
1093 p
= startswith(path
, "/org/freedesktop/import1/transfer/_");
1097 r
= safe_atou32(p
, &id
);
1098 if (r
< 0 || id
== 0)
1101 t
= hashmap_get(m
->transfers
, UINT32_TO_PTR(id
));
1109 static int transfer_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1110 _cleanup_strv_free_
char **l
= NULL
;
1111 Manager
*m
= userdata
;
1116 l
= new0(char*, hashmap_size(m
->transfers
) + 1);
1120 HASHMAP_FOREACH(t
, m
->transfers
, i
) {
1122 l
[k
] = strdup(t
->object_path
);
1135 static int manager_add_bus_objects(Manager
*m
) {
1140 r
= sd_bus_add_object_vtable(m
->bus
, NULL
, "/org/freedesktop/import1", "org.freedesktop.import1.Manager", manager_vtable
, m
);
1142 return log_error_errno(r
, "Failed to register object: %m");
1144 r
= sd_bus_add_fallback_vtable(m
->bus
, NULL
, "/org/freedesktop/import1/transfer", "org.freedesktop.import1.Transfer", transfer_vtable
, transfer_object_find
, m
);
1146 return log_error_errno(r
, "Failed to register object: %m");
1148 r
= sd_bus_add_node_enumerator(m
->bus
, NULL
, "/org/freedesktop/import1/transfer", transfer_node_enumerator
, m
);
1150 return log_error_errno(r
, "Failed to add transfer enumerator: %m");
1152 r
= sd_bus_request_name(m
->bus
, "org.freedesktop.import1", 0);
1154 return log_error_errno(r
, "Failed to register name: %m");
1156 r
= sd_bus_attach_event(m
->bus
, m
->event
, 0);
1158 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1163 static bool manager_check_idle(void *userdata
) {
1164 Manager
*m
= userdata
;
1166 return hashmap_isempty(m
->transfers
);
1169 static int manager_run(Manager
*m
) {
1172 return bus_event_loop_with_idle(
1175 "org.freedesktop.import1",
1181 int main(int argc
, char *argv
[]) {
1182 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
1185 log_set_target(LOG_TARGET_AUTO
);
1186 log_parse_environment();
1192 log_error("This program takes no arguments.");
1197 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, -1) >= 0);
1199 r
= manager_new(&m
);
1201 log_error_errno(r
, "Failed to allocate manager object: %m");
1205 r
= manager_add_bus_objects(m
);
1211 log_error_errno(r
, "Failed to run event loop: %m");
1216 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;