2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Mateusz Malicki <m.malicki2@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
22 * @author Mateusz Malicki (m.malicki2@samsung.com)
23 * @brief This file contains the public API for Vasum Client
28 #include "client/vasum-client.h"
30 int main(int argc, char** argv)
34 VsmArrayString values = NULL;
37 client = vsm_client_create(); // create client handle
44 status = vsm_connect(client); // connect to dbus
45 if (VSMCLIENT_SUCCESS != status) {
51 status = vsm_get_zone_ids(client, &values);
52 if (VSMCLIENT_SUCCESS != status) {
59 for (VsmArrayString iValues = values; *iValues; iValues++) {
60 printf("%s\n", *iValues);
64 vsm_array_string_free(values); // free memory
65 vsm_client_free(client); // destroy client handle
71 #ifndef VASUM_CLIENT_H
72 #define VASUM_CLIENT_H
76 #include <netinet/ip.h>
77 #include <linux/if_link.h>
85 * vasum-server's client pointer.
87 typedef void* VsmClient;
90 * NULL-terminated string type.
92 * @sa vsm_array_string_free
94 typedef char* VsmString;
97 * NULL-terminated array of strings type.
101 typedef VsmString* VsmArrayString;
104 * Completion status of communication function.
107 VSMCLIENT_CUSTOM_ERROR, /**< User specified error */
108 VSMCLIENT_IO_ERROR, /**< Input/Output error */
109 VSMCLIENT_OPERATION_FAILED, /**< Operation failed */
110 VSMCLIENT_INVALID_ARGUMENT, /**< Invalid argument */
111 VSMCLIENT_OTHER_ERROR, /**< Other error */
112 VSMCLIENT_SUCCESS /**< Success */
118 typedef unsigned int VsmSubscriptionId;
138 * Zone information structure
150 typedef VsmZoneStructure* VsmZone;
153 * Netowrk device type
162 * Network device information structure
167 } VsmNetdevStructure;
170 * Network device information
172 typedef VsmNetdevStructure* VsmNetdev;
184 * Event dispacher types.
186 * @par Example usage:
192 #include <sys/epoll.h>
193 #include <vasum-client.h>
195 volatile static sig_atomic_t running;
196 static int LOOP_INTERVAL = 1000; // ms
198 void* main_loop(void* client)
201 VsmStatus status = vsm_get_poll_fd(client, &fd);
202 assert(VSMCLIENT_SUCCESS == status);
205 struct epoll_event event;
206 int num = epoll_wait(fd, &event, 1, LOOP_INTERVAL);
208 status = vsm_enter_eventloop(client, 0 , 0);
209 assert(VSMCLIENT_SUCCESS == status);
215 int main(int argc, char** argv)
222 client = vsm_client_create();
225 status = vsm_set_dispatcher_type(client, VSMDISPATCHER_EXTERNAL);
226 assert(VSMCLIENT_SUCCESS == status);
228 status = vsm_connect(client);
229 assert(VSMCLIENT_SUCCESS == status);
233 ret = pthread_create(&loop, NULL, main_loop, client);
236 // make vsm_* calls on client
239 status = vsm_disconnect(client);
240 assert(VSMCLIENT_SUCCESS == status);
244 ret = pthread_join(loop, NULL);
247 vsm_client_free(client); // destroy client handle
253 VSMDISPATCHER_EXTERNAL, /**< User must handle dispatching messages */
254 VSMDISPATCHER_INTERNAL /**< Library will take care of dispatching messages */
257 #ifndef __VASUM_WRAPPER_SOURCE__
260 * Get file descriptor associated with event dispatcher of zone client
262 * The function vsm_get_poll_fd() returns the file descriptor associated with the event dispatcher of vsm client.
263 * The file descriptor can be bound to another I/O multiplexing facilities like epoll, select, and poll.
265 * @param[in] client vsm client
266 * @param[out] fd epoll file descriptor
267 * @return status of this function call
269 VsmStatus vsm_get_poll_fd(VsmClient client, int* fd);
272 * Wait for an I/O event on a vsm client
274 * vsm_enter_eventloop() waits for event on the vsm client
276 * The call waits for a maximum time of timout milliseconds. Specifying a timeout of -1 makes vsm_enter_eventloop() wait indefinitely,
277 * while specifying a timeout equal to zero makes vsm_enter_eventloop() to return immediately even if no events are available.
279 * @param[in] client vasum-server's client
280 * @param[in] flags Reserved
281 * @param[in] timeout Timeout time (milisecond), -1 is infinite
282 * @return status of this function call
284 VsmStatus vsm_enter_eventloop(VsmClient client, int flags, int timeout);
287 * Set dispatching method
289 * @param[in] client vasum-server's client
290 * @param[in] dispacher dispatching method
291 * @return status of this function call
293 VsmStatus vsm_set_dispatcher_type(VsmClient client, VsmDispacherType dispacher);
296 * Get dispatching method
298 * @param[in] client vasum-server's client
299 * @param[out] dispacher dispatching method
300 * @return status of this function call
302 VsmStatus vsm_get_dispatcher_type(VsmClient client, VsmDispacherType* dispacher);
305 * Create a new vasum-server's client.
307 * @return Created client pointer or NULL on failure.
309 VsmClient vsm_client_create();
312 * Release client resources.
314 * @param[in] client vasum-server's client
316 void vsm_client_free(VsmClient client);
319 * Get status code of last vasum-server communication.
321 * @param[in] client vasum-server's client
322 * @return status of this function call
324 VsmStatus vsm_get_status(VsmClient client);
327 * Get status message of the last vasum-server communication.
329 * @param[in] client vasum-server's client
330 * @return last status message from vasum-server communication
332 const char* vsm_get_status_message(VsmClient client);
335 * Connect client to the vasum-server.
337 * @param[in] client vasum-server's client
338 * @return status of this function call
340 VsmStatus vsm_connect(VsmClient client);
343 * Connect client to the vasum-server via custom address.
345 * @param[in] client vasum-server's client
346 * @param[in] address dbus address
347 * @return status of this function call
349 VsmStatus vsm_connect_custom(VsmClient client, const char* address);
352 * Disconnect client from vasum-server.
354 * @param[in] client vasum-server's client
355 * @return status of this function call
357 VsmStatus vsm_disconnect(VsmClient client);
360 * Release VsmArrayString.
362 * @param[in] astring VsmArrayString
364 void vsm_array_string_free(VsmArrayString astring);
369 * @param string VsmString
371 void vsm_string_free(VsmString string);
376 * @param zone VsmZone
378 void vsm_zone_free(VsmZone zone);
383 * @param netdev VsmNetdev
385 void vsm_netdev_free(VsmNetdev netdev);
390 * Functions using org.tizen.vasum.host.manager D-Bus interface.
396 * Zone's D-Bus state change callback function signature.
398 * @param[in] zoneId affected zone id
399 * @param[in] address new D-Bus address
400 * @param data custom user's data pointer passed to vsm_add_state_callback() function
402 typedef void (*VsmZoneDbusStateCallback)(const char* zoneId,
407 * Lock the command queue exclusively.
409 * @param[in] client vasum-server's client
410 * @return status of this function call
412 VsmStatus vsm_lock_queue(VsmClient client);
415 * Unlock the command queue.
417 * @param[in] client vasum-server's client
418 * @return status of this function call
420 VsmStatus vsm_unlock_queue(VsmClient client);
423 * Get dbus address of each zone.
425 * @param[in] client vasum-server's client
426 * @param[out] keys array of zones name
427 * @param[out] values array of zones dbus address
428 * @return status of this function call
429 * @post keys[i] corresponds to values[i]
430 * @remark Use vsm_array_string_free() to free memory occupied by @p keys and @p values.
432 VsmStatus vsm_get_zone_dbuses(VsmClient client, VsmArrayString* keys, VsmArrayString* values);
437 * @param[in] client vasum-server's client
438 * @param[out] array array of zones name
439 * @return status of this function call
440 * @remark Use vsm_array_string_free() to free memory occupied by @p array.
442 VsmStatus vsm_get_zone_ids(VsmClient client, VsmArrayString* array);
445 * Get active (foreground) zone name.
447 * @param[in] client vasum-server's client
448 * @param[out] id active zone name
449 * @return status of this function call
450 * @remark Use @p vsm_string_free() to free memory occupied by @p id.
452 VsmStatus vsm_get_active_zone_id(VsmClient client, VsmString* id);
455 * Get zone rootfs path.
457 * @param[in] client vasum-server's client
458 * @param[in] id zone name
459 * @param[out] rootpath zone rootfs path
460 * @return status of this function call
461 * @remark Use @p vsm_string_free() to free memory occupied by @p rootpath.
463 VsmStatus vsm_get_zone_rootpath(VsmClient client, const char* id, VsmString* rootpath);
466 * Get zone name of process with given pid.
468 * @param[in] client vasum-server's client
469 * @param[in] pid process id
470 * @param[out] id active zone name
471 * @return status of this function call
472 * @remark Use @p vsm_string_free() to free memory occupied by @p id.
474 VsmStatus vsm_lookup_zone_by_pid(VsmClient client, int pid, VsmString* id);
477 * Get zone informations of zone with given id.
479 * @param[in] client vasum-server's client
480 * @param[in] id zone name
481 * @param[out] zone zone informations
482 * @return status of this function call
483 * @remark Use @p vsm_zone_free() to free memory occupied by @p zone
485 VsmStatus vsm_lookup_zone_by_id(VsmClient client, const char* id, VsmZone* zone);
488 * Get zone name with given terminal.
490 * @param[in] client vasum-server's client
491 * @param[in] terminal terminal id
492 * @param[out] id zone name with given terminal
493 * @return status of this function call
494 * @remark Use @p vsm_string_free() to free memory occupied by @p id.
496 VsmStatus vsm_lookup_zone_by_terminal_id(VsmClient client, int terminal, VsmString* id);
499 * Set active (foreground) zone.
501 * @param[in] client vasum-server's client
502 * @param[in] id zone name
503 * @return status of this function call
505 VsmStatus vsm_set_active_zone(VsmClient client, const char* id);
508 * Create and add zone
510 * @param[in] client vasum-server's client
511 * @param[in] id zone id
512 * @param[in] tname template name, NULL is equivalent to "default"
513 * @return status of this function call
515 VsmStatus vsm_create_zone(VsmClient client, const char* id, const char* tname);
520 * @param[in] client vasum-server's client
521 * @param[in] id zone id
522 * @param[in] force if 0 data will be kept, otherwise data will be lost
523 * @return status of this function call
525 VsmStatus vsm_destroy_zone(VsmClient client, const char* id, int force);
530 * @param[in] client vasum-server's client
531 * @param[in] id zone name
532 * @return status of this function call
534 VsmStatus vsm_shutdown_zone(VsmClient client, const char* id);
539 * @param[in] client vasum-server's client
540 * @param[in] id zone name
541 * @return status of this function call
543 VsmStatus vsm_start_zone(VsmClient client, const char* id);
548 * @param[in] client vasum-server's client
549 * @param[in] id zone name
550 * @return status of this function call
552 VsmStatus vsm_lock_zone(VsmClient client, const char* id);
557 * @param[in] client vasum-server's client
558 * @param[in] id zone name
559 * @return status of this function call
561 VsmStatus vsm_unlock_zone(VsmClient client, const char* id);
564 * Register dbus state change callback function.
566 * @note The callback function will be invoked on a different thread.
568 * @param[in] client vasum-server's client
569 * @param[in] zoneDbusStateCallback callback function
570 * @param[in] data some extra data that will be passed to callback function
571 * @param[out] subscriptionId subscription identifier that can be used to unsubscribe signal,
572 * pointer can be NULL.
573 * @return status of this function call
575 VsmStatus vsm_add_state_callback(VsmClient client,
576 VsmZoneDbusStateCallback zoneDbusStateCallback,
578 VsmSubscriptionId* subscriptionId);
581 * Unregister dbus state change callback function.
583 * @param[in] client vasum-server's client
584 * @param[in] subscriptionId subscription identifier returned by vsm_add_state_callback
585 * @return status of this function call
587 VsmStatus vsm_del_state_callback(VsmClient client, VsmSubscriptionId subscriptionId);
590 * Grant access to device
592 * @param[in] client vasum-server's client
593 * @param[in] zone zone name
594 * @param[in] device device path
595 * @param[in] flags access flags
596 * @return status of this function call
598 VsmStatus vsm_grant_device(VsmClient client,
604 * Revoke access to device
606 * @param[in] client vasum-server's client
607 * @param[in] zone zone name
608 * @param[in] device device path
609 * @return status of this function call
611 VsmStatus vsm_revoke_device(VsmClient client, const char* zone, const char* device);
614 * Get array of netdev from given zone
616 * @param[in] client vasum-server's client
617 * @param[in] zone zone name
618 * @param[out] netdevIds array of netdev id
619 * @return status of this function call
620 * @remark Use vsm_array_string_free() to free memory occupied by @p netdevIds.
622 VsmStatus vsm_zone_get_netdevs(VsmClient client, const char* zone, VsmArrayString* netdevIds);
625 * Get ipv4 address for given netdevId
627 * @param[in] client vasum-server's client
628 * @param[in] zone zone name
629 * @param[in] netdevId netdev id
630 * @param[out] addr ipv4 address
631 * @return status of this function call
633 VsmStatus vsm_netdev_get_ipv4_addr(VsmClient client,
635 const char* netdevId,
636 struct in_addr *addr);
639 * Get ipv6 address for given netdevId
641 * @param[in] client vasum-server's client
642 * @param[in] zone zone name
643 * @param[in] netdevId netdev id
644 * @param[out] addr ipv6 address
645 * @return status of this function call
647 VsmStatus vsm_netdev_get_ipv6_addr(VsmClient client,
649 const char* netdevId,
650 struct in6_addr *addr);
653 * Set ipv4 address for given netdevId
655 * @param[in] client vasum-server's client
656 * @param[in] zone zone name
657 * @param[in] netdevId netdev id
658 * @param[in] addr ipv4 address
659 * @param[in] prefix bit-length of the network prefix
660 * @return status of this function call
662 VsmStatus vsm_netdev_set_ipv4_addr(VsmClient client,
664 const char* netdevId,
665 struct in_addr *addr,
669 * Set ipv6 address for given netdevId
671 * @param[in] client vasum-server's client
672 * @param[in] zone zone name
673 * @param[in] netdevId netdev id
674 * @param[in] addr ipv6 address
675 * @param[in] prefix bit-length of the network prefix
676 * @return status of this function call
678 VsmStatus vsm_netdev_set_ipv6_addr(VsmClient client,
680 const char* netdevId,
681 struct in6_addr *addr,
685 * Remove ipv4 address from netdev
687 * @param[in] client vasum-server's client
688 * @param[in] zone zone name
689 * @param[in] netdevId network device id
690 * @param[in] addr ipv4 address
691 * @param[in] prefix bit-length of the network prefix
692 * @return status of this function call
694 VsmStatus vsm_netdev_del_ipv4_addr(VsmClient client,
696 const char* netdevId,
697 struct in_addr* addr,
701 * Remove ipv6 address from netdev
703 * @param[in] client vasum-server's client
704 * @param[in] zone zone name
705 * @param[in] netdevId network device id
706 * @param[in] addr ipv6 address
707 * @param[in] prefix bit-length of the network prefix
708 * @return status of this function call
710 VsmStatus vsm_netdev_del_ipv6_addr(VsmClient client,
712 const char* netdevId,
713 struct in6_addr* addr,
717 * Turn up a network device in the zone
719 * @param[in] client vasum-server's client
720 * @param[in] zone zone name
721 * @param[in] netdevId netdev id
722 * @return status of this function call
724 VsmStatus vsm_netdev_up(VsmClient client,
726 const char* netdevId);
729 * Turn down a network device in the zone
731 * @param[in] client vasum-server's client
732 * @param[in] zone zone name
733 * @param[in] netdevId netdev id
734 * @return status of this function call
736 VsmStatus vsm_netdev_down(VsmClient client,
738 const char* netdevId);
742 * Create veth netdev in zone
744 * @param[in] client vasum-server's client
745 * @param[in] zone zone name
746 * @param[in] zoneDev Device ID in Zone network
747 * @param[in] hostDev Device ID in Host network
748 * @return status of this function call
750 VsmStatus vsm_create_netdev_veth(VsmClient client,
753 const char* hostDev);
755 * Create macvlan in zone
757 * @param[in] client vasum-server's client
758 * @param[in] zone Zone name
759 * @param[in] zoneDev Device ID in Zone network
760 * @param[in] hostDev Device ID in Host network
761 * @param[in] mode Mode with which macvlan will be created.
762 * @return status of this function call
766 VsmStatus vsm_create_netdev_macvlan(VsmClient client,
770 enum macvlan_mode mode);
772 * Create/move phys netdev in/to zone
774 * @param[in] client vasum-server's client
775 * @param[in] zone zone name
776 * @param[in] devId network device id
777 * @return status of this function call
779 VsmStatus vsm_create_netdev_phys(VsmClient client, const char* zone, const char* devId);
782 * Get netdev informations
784 * @param[in] client vasum-server's client
785 * @param[in] zone zone name
786 * @param[in] netdevId network device id
787 * @param[out] netdev netdev informations
788 * @return status of this function call
789 * @remark Use vsm_netdev_free() to free memory occupied by @p netdev.
791 VsmStatus vsm_lookup_netdev_by_name(VsmClient client,
793 const char* netdevId,
797 * Remove netdev from zone
799 * @param[in] client vasum-server's client
800 * @param[in] zone zone name
801 * @param[in] devId network device id
802 * @return status of this function call
804 VsmStatus vsm_destroy_netdev(VsmClient client, const char* zone, const char* devId);
807 * Create file, directory or pipe in zone
809 * Declare file, directory or pipe that will be created while zone startup
811 * @param[in] client vasum-server's client
812 * @param[in] type file type
813 * @param[in] zone zone id
814 * @param[in] path path to file
815 * @param[in] flags if O_CREAT bit is set then file will be created in zone,
816 * otherwise file will by copied from host;
817 * it is meaningful only when O_CREAT is set
818 * @param[in] mode mode of file
819 * @return status of this function call
821 VsmStatus vsm_declare_file(VsmClient client,
829 * Create mount point in zone
831 * Declare mount that will be created while zone startup
832 * Parameters are passed to mount system function
834 * @param[in] client vasum-server's client
835 * @param[in] source device path (path in host)
836 * @param[in] zone zone id
837 * @param[in] target mount point (path in zone)
838 * @param[in] type filesystem type
839 * @param[in] flags mount flags as in mount function
840 * @param[in] data additional data as in mount function
841 * @return status of this function call
843 VsmStatus vsm_declare_mount(VsmClient client,
852 * Create link in zone
854 * Declare link that will be created while zone startup
855 * Parameters are passed to link system function
857 * @param[in] client vasum-server's client
858 * @param[in] source path to link source (in host)
859 * @param[in] zone zone id
860 * @param[in] target path to link name (in zone)
861 * @return status of this function call
863 VsmStatus vsm_declare_link(VsmClient client,
869 * Get all declarations
871 * Gets all declarations of resourcies
872 * (@see ::vsm_declare_link, @see ::vsm_declare_mount, @see ::vsm_declare_file)
874 * @param[in] client vasum-server's client
875 * @param[in] zone zone id
876 * @param[out] declarations array of declarations id
877 * @return status of this function call
879 VsmStatus vsm_list_declarations(VsmClient client,
881 VsmArrayString* declarations);
886 * Removes given declaration by its id (@see ::vsm_list_declarations)
888 * @param[in] client vasum-server's client
889 * @param[in] zone zone id
890 * @param[in] declaration declaration id
891 * @return status of this function call
893 VsmStatus vsm_remove_declaration(VsmClient client,
895 VsmString declaration);
901 #endif /* __VASUM_WRAPPER_SOURCE__ */
907 #endif /* VASUM_CLIENT_H */