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
24 * @defgroup libvasum-client libvasum-client
25 * @brief C library for interfacing Vasum
27 * All functionalities that are possible using the Vasum's Command Line Interface can also be done with libvasum-client's calls.
31 * - Create VsmClient with vsm_client_create(). It'll be needed for all communication with Vasum.
32 * - Establish the connection with the daemon using vsm_connect()
33 * - Do what you need to do with the zones
34 * - Free the client with vsm_client_free()
38 #include <vasum-client.h>
40 int main(int argc, char** argv)
44 VsmArrayString values = NULL;
47 // Create client handle
48 client = vsm_client_create();
56 status = vsm_connect(client);
57 if (VSMCLIENT_SUCCESS != status) {
63 status = vsm_get_zone_ids(client, &values);
64 if (VSMCLIENT_SUCCESS != status) {
71 for (VsmArrayString iValues = values; *iValues; iValues++) {
72 printf("%s\n", *iValues);
76 vsm_array_string_free(values); // free memory
77 vsm_client_free(client); // destroy client handle
84 * By default libVasum will create a separate thread for his communication with Vasum. Most of the time it'll sleep so it shouldn't be a concern.
85 * It's also possible to connect to an existing polling loop. To do this you'll need to:
86 * - Get the poll file descriptor with vsm_get_poll_fd()
87 * - Use epoll/poll/select to wait for events on the file descriptor
88 * - Call vsm_enter_eventloop() every time there's an event
96 #include <sys/epoll.h>
97 #include <vasum-client.h>
99 volatile static sig_atomic_t running;
100 static int LOOP_INTERVAL = 1000; // ms
102 void* main_loop(void* client)
105 VsmStatus status = vsm_get_poll_fd(client, &fd);
106 assert(VSMCLIENT_SUCCESS == status);
109 struct epoll_event event;
110 int num = epoll_wait(fd, &event, 1, LOOP_INTERVAL);
112 status = vsm_enter_eventloop(client, 0 , 0);
113 assert(VSMCLIENT_SUCCESS == status);
119 int main(int argc, char** argv)
126 client = vsm_client_create();
129 status = vsm_set_dispatcher_type(client, VSMDISPATCHER_EXTERNAL);
130 assert(VSMCLIENT_SUCCESS == status);
132 status = vsm_connect(client);
133 assert(VSMCLIENT_SUCCESS == status);
137 ret = pthread_create(&loop, NULL, main_loop, client);
140 // make vsm_* calls on client
143 status = vsm_disconnect(client);
144 assert(VSMCLIENT_SUCCESS == status);
148 ret = pthread_join(loop, NULL);
151 vsm_client_free(client); // destroy client handle
157 #ifndef VASUM_CLIENT_H
158 #define VASUM_CLIENT_H
161 #include <sys/stat.h>
162 #include <netinet/ip.h>
163 #include <linux/if_link.h>
171 * vasum-server's opaque client pointer.
173 typedef void* VsmClient;
176 * NULL-terminated string type.
178 * @sa vsm_string_free
180 typedef char* VsmString;
183 * NULL-terminated array of strings type.
185 * @sa vsm_array_string_free
187 typedef VsmString* VsmArrayString;
189 typedef void *VsmAddrList;
191 * Completion status of libvasum-client's functions
194 VSMCLIENT_CUSTOM_ERROR, /**< User specified error */
195 VSMCLIENT_IO_ERROR, /**< Input/Output error */
196 VSMCLIENT_OPERATION_FAILED, /**< Operation failed */
197 VSMCLIENT_INVALID_ARGUMENT, /**< Invalid argument */
198 VSMCLIENT_OTHER_ERROR, /**< Other error */
199 VSMCLIENT_SUCCESS /**< Success */
205 typedef unsigned int VsmSubscriptionId;
227 typedef void* VsmZone;
230 * Netowrk device type
239 * Network device information
241 typedef void* VsmNetdev;
253 * Event dispacher types.
256 VSMDISPATCHER_EXTERNAL, /**< User must handle dispatching messages */
257 VSMDISPATCHER_INTERNAL /**< Library will take care of dispatching messages */
260 #ifndef __VASUM_WRAPPER_SOURCE__
263 * Get file descriptor associated with event dispatcher of zone client
265 * The function vsm_get_poll_fd() returns the file descriptor associated with the event dispatcher of vsm client.
266 * The file descriptor can be bound to another I/O multiplexing facilities like epoll, select, and poll.
268 * @param[in] client vsm client
269 * @param[out] fd epoll file descriptor
270 * @return status of this function call
272 VsmStatus vsm_get_poll_fd(VsmClient client, int* fd);
275 * Wait for an I/O event on a vsm client
277 * vsm_enter_eventloop() waits for event on the vsm client
279 * The call waits for a maximum time of timout milliseconds. Specifying a timeout of -1 makes vsm_enter_eventloop() wait indefinitely,
280 * while specifying a timeout equal to zero makes vsm_enter_eventloop() to return immediately even if no events are available.
282 * @param[in] client vasum-server's client
283 * @param[in] flags Reserved
284 * @param[in] timeout Timeout time (milisecond), -1 is infinite
285 * @return status of this function call
287 VsmStatus vsm_enter_eventloop(VsmClient client, int flags, int timeout);
290 * Set dispatching method
292 * @param[in] client vasum-server's client
293 * @param[in] dispacher dispatching method
294 * @return status of this function call
296 VsmStatus vsm_set_dispatcher_type(VsmClient client, VsmDispacherType dispacher);
299 * Get dispatching method
301 * @param[in] client vasum-server's client
302 * @param[out] dispacher dispatching method
303 * @return status of this function call
305 VsmStatus vsm_get_dispatcher_type(VsmClient client, VsmDispacherType* dispacher);
308 * Create a new vasum-server's client.
310 * @return Created client pointer or NULL on failure.
312 VsmClient vsm_client_create();
315 * Release client resources.
317 * @param[in] client vasum-server's client
319 void vsm_client_free(VsmClient client);
322 * Get status code of last vasum-server communication.
324 * @param[in] client vasum-server's client
325 * @return status of this function call
327 VsmStatus vsm_get_status(VsmClient client);
330 * Get status message of the last vasum-server communication.
332 * @param[in] client vasum-server's client
333 * @return last status message from vasum-server communication
335 const char* vsm_get_status_message(VsmClient client);
338 * Connect client to the vasum-server.
340 * @param[in] client vasum-server's client
341 * @return status of this function call
343 VsmStatus vsm_connect(VsmClient client);
346 * Connect client to the vasum-server via custom address.
348 * @param[in] client vasum-server's client
349 * @param[in] address dbus address
350 * @return status of this function call
352 VsmStatus vsm_connect_custom(VsmClient client, const char* address);
355 * Disconnect client from vasum-server.
357 * @param[in] client vasum-server's client
358 * @return status of this function call
360 VsmStatus vsm_disconnect(VsmClient client);
363 * Release VsmArrayString.
365 * @param[in] astring VsmArrayString
367 void vsm_array_string_free(VsmArrayString astring);
372 * @param string VsmString
374 void vsm_string_free(VsmString string);
377 * Get zone id (offline)
379 * @param zone VsmZone
382 VsmString vsm_zone_get_id(VsmZone zone);
385 * Get zone terminal (offline)
387 * @param zone VsmZone
388 * @return zone terminal
390 int vsm_zone_get_terminal(VsmZone zone);
393 * Get zone state (offline)
395 * @param zone VsmZone
398 VsmZoneState vsm_zone_get_state(VsmZone zone);
401 * Get zone rootfs path (offline)
403 * @param zone VsmZone
404 * @return zone rootfs path
406 VsmString vsm_zone_get_rootfs(VsmZone zone);
411 * @param zone VsmZone
413 void vsm_zone_free(VsmZone zone);
416 * Get netdev name (offline)
418 * @param netdev VsmNetdev
419 * @return netdev name
421 VsmString vsm_netdev_get_name(VsmNetdev netdev);
424 * Get netdev type (offline)
426 * @param netdev VsmNetdev
427 * @return netdev type
429 VsmNetdevType vsm_netdev_get_type(VsmNetdev netdev);
434 * @param netdev VsmNetdev
436 void vsm_netdev_free(VsmNetdev netdev);
439 * Zone's D-Bus state change callback function signature.
441 * @param[in] zoneId affected zone id
442 * @param[in] address new D-Bus address
443 * @param data custom user's data pointer passed to vsm_add_state_callback() function
445 typedef void (*VsmZoneDbusStateCallback)(const char* zoneId,
450 * Lock the command queue exclusively.
452 * @param[in] client vasum-server's client
453 * @return status of this function call
455 VsmStatus vsm_lock_queue(VsmClient client);
458 * Unlock the command queue.
460 * @param[in] client vasum-server's client
461 * @return status of this function call
463 VsmStatus vsm_unlock_queue(VsmClient client);
466 * Get dbus address of each zone.
468 * @param[in] client vasum-server's client
469 * @param[out] keys array of zones name
470 * @param[out] values array of zones dbus address
471 * @return status of this function call
472 * @post keys[i] corresponds to values[i]
473 * @remark Use vsm_array_string_free() to free memory occupied by @p keys and @p values.
475 VsmStatus vsm_get_zone_dbuses(VsmClient client, VsmArrayString* keys, VsmArrayString* values);
480 * @param[in] client vasum-server's client
481 * @param[out] array array of zones name
482 * @return status of this function call
483 * @remark Use vsm_array_string_free() to free memory occupied by @p array.
485 VsmStatus vsm_get_zone_ids(VsmClient client, VsmArrayString* array);
488 * Get active (foreground) zone name.
490 * @param[in] client vasum-server's client
491 * @param[out] id active zone name
492 * @return status of this function call
493 * @remark Use @p vsm_string_free() to free memory occupied by @p id.
495 VsmStatus vsm_get_active_zone_id(VsmClient client, VsmString* id);
498 * Get zone name of process with given pid.
500 * @param[in] client vasum-server's client
501 * @param[in] pid process id
502 * @param[out] id active zone name
503 * @return status of this function call
504 * @remark Use @p vsm_string_free() to free memory occupied by @p id.
506 VsmStatus vsm_lookup_zone_by_pid(VsmClient client, int pid, VsmString* id);
509 * Get zone informations of zone with given id.
511 * @param[in] client vasum-server's client
512 * @param[in] id zone name
513 * @param[out] zone zone informations
514 * @return status of this function call
515 * @remark Use @p vsm_zone_free() to free memory occupied by @p zone
517 VsmStatus vsm_lookup_zone_by_id(VsmClient client, const char* id, VsmZone* zone);
520 * Get zone name with given terminal.
522 * @param[in] client vasum-server's client
523 * @param[in] terminal terminal id
524 * @param[out] id zone name with given terminal
525 * @return status of this function call
526 * @remark Use @p vsm_string_free() to free memory occupied by @p id.
528 VsmStatus vsm_lookup_zone_by_terminal_id(VsmClient client, int terminal, VsmString* id);
531 * Set active (foreground) zone.
533 * @param[in] client vasum-server's client
534 * @param[in] id zone name
535 * @return status of this function call
537 VsmStatus vsm_set_active_zone(VsmClient client, const char* id);
540 * Create and add zone
542 * @param[in] client vasum-server's client
543 * @param[in] id zone id
544 * @param[in] tname template name, NULL is equivalent to "default"
545 * @return status of this function call
547 VsmStatus vsm_create_zone(VsmClient client, const char* id, const char* tname);
552 * @param[in] client vasum-server's client
553 * @param[in] id zone id
554 * @param[in] force if 0 data will be kept, otherwise data will be lost
555 * @return status of this function call
557 VsmStatus vsm_destroy_zone(VsmClient client, const char* id, int force);
562 * @param[in] client vasum-server's client
563 * @param[in] id zone name
564 * @return status of this function call
566 VsmStatus vsm_shutdown_zone(VsmClient client, const char* id);
571 * @param[in] client vasum-server's client
572 * @param[in] id zone name
573 * @return status of this function call
575 VsmStatus vsm_start_zone(VsmClient client, const char* id);
580 * @param[in] client vasum-server's client
581 * @param[in] id zone name
582 * @return status of this function call
584 VsmStatus vsm_lock_zone(VsmClient client, const char* id);
589 * @param[in] client vasum-server's client
590 * @param[in] id zone name
591 * @return status of this function call
593 VsmStatus vsm_unlock_zone(VsmClient client, const char* id);
596 * Register dbus state change callback function.
598 * @note The callback function will be invoked on a different thread.
600 * @param[in] client vasum-server's client
601 * @param[in] zoneDbusStateCallback callback function
602 * @param[in] data some extra data that will be passed to callback function
603 * @param[out] subscriptionId subscription identifier that can be used to unsubscribe signal,
604 * pointer can be NULL.
605 * @return status of this function call
607 VsmStatus vsm_add_state_callback(VsmClient client,
608 VsmZoneDbusStateCallback zoneDbusStateCallback,
610 VsmSubscriptionId* subscriptionId);
613 * Unregister dbus state change callback function.
615 * @param[in] client vasum-server's client
616 * @param[in] subscriptionId subscription identifier returned by vsm_add_state_callback
617 * @return status of this function call
619 VsmStatus vsm_del_state_callback(VsmClient client, VsmSubscriptionId subscriptionId);
622 * Grant access to device
624 * @param[in] client vasum-server's client
625 * @param[in] zone zone name
626 * @param[in] device device path
627 * @param[in] flags access flags
628 * @return status of this function call
630 VsmStatus vsm_grant_device(VsmClient client,
636 * Revoke access to device
638 * @param[in] client vasum-server's client
639 * @param[in] zone zone name
640 * @param[in] device device path
641 * @return status of this function call
643 VsmStatus vsm_revoke_device(VsmClient client, const char* zone, const char* device);
646 * Get array of netdev from given zone
648 * @param[in] client vasum-server's client
649 * @param[in] zone zone name
650 * @param[out] netdevIds array of netdev id
651 * @return status of this function call
652 * @remark Use vsm_array_string_free() to free memory occupied by @p netdevIds.
654 VsmStatus vsm_zone_get_netdevs(VsmClient client, const char* zone, VsmArrayString* netdevIds);
658 * Get ipv4 address for given netdevId
660 * @param[in] client vasum-server's client
661 * @param[in] zone zone name
662 * @param[in] netdevId netdev id
663 * @param[out] addrs ip address array
664 * @return status of this function call
665 * @remark Use vsm_netdev_addr_free() to free memory occupied by address array.
667 VsmStatus vsm_netdev_get_ip_addr(VsmClient client,
669 const char* netdevId,
673 * Release VsmAddrList
675 * @param addrs VsmAddrList
677 void vsm_addrlist_free(VsmAddrList addrs);
680 * Get ipv4 address for given netdevId
682 * @param[in] client vasum-server's client
683 * @param[in] zone zone name
684 * @param[in] netdevId netdev id
685 * @param[out] addr ipv4 address
686 * @return status of this function call
688 VsmStatus vsm_netdev_get_ipv4_addr(VsmClient client,
690 const char* netdevId,
691 struct in_addr *addr);
694 * Get ipv6 address for given netdevId
696 * @param[in] client vasum-server's client
697 * @param[in] zone zone name
698 * @param[in] netdevId netdev id
699 * @param[out] addr ipv6 address
700 * @return status of this function call
702 VsmStatus vsm_netdev_get_ipv6_addr(VsmClient client,
704 const char* netdevId,
705 struct in6_addr *addr);
708 * Add ipv4 address for given netdevId
710 * @param[in] client vasum-server's client
711 * @param[in] zone zone name
712 * @param[in] netdevId netdev id
713 * @param[in] addr ipv4 address
714 * @param[in] prefix bit-length of the network prefix
715 * @return status of this function call
717 VsmStatus vsm_netdev_add_ipv4_addr(VsmClient client,
719 const char* netdevId,
720 struct in_addr *addr,
724 * Add ipv6 address for given netdevId
726 * @param[in] client vasum-server's client
727 * @param[in] zone zone name
728 * @param[in] netdevId netdev id
729 * @param[in] addr ipv6 address
730 * @param[in] prefix bit-length of the network prefix
731 * @return status of this function call
733 VsmStatus vsm_netdev_add_ipv6_addr(VsmClient client,
735 const char* netdevId,
736 struct in6_addr *addr,
740 * Remove ipv4 address from netdev
742 * @param[in] client vasum-server's client
743 * @param[in] zone zone name
744 * @param[in] netdevId network device id
745 * @param[in] addr ipv4 address
746 * @param[in] prefix bit-length of the network prefix
747 * @return status of this function call
749 VsmStatus vsm_netdev_del_ipv4_addr(VsmClient client,
751 const char* netdevId,
752 struct in_addr* addr,
756 * Remove ipv6 address from netdev
758 * @param[in] client vasum-server's client
759 * @param[in] zone zone name
760 * @param[in] netdevId network device id
761 * @param[in] addr ipv6 address
762 * @param[in] prefix bit-length of the network prefix
763 * @return status of this function call
765 VsmStatus vsm_netdev_del_ipv6_addr(VsmClient client,
767 const char* netdevId,
768 struct in6_addr* addr,
772 * Turn up a network device in the zone
774 * @param[in] client vasum-server's client
775 * @param[in] zone zone name
776 * @param[in] netdevId netdev id
777 * @return status of this function call
779 VsmStatus vsm_netdev_up(VsmClient client,
781 const char* netdevId);
784 * Turn down a network device in the zone
786 * @param[in] client vasum-server's client
787 * @param[in] zone zone name
788 * @param[in] netdevId netdev id
789 * @return status of this function call
791 VsmStatus vsm_netdev_down(VsmClient client,
793 const char* netdevId);
797 * Create veth netdev in zone
799 * @param[in] client vasum-server's client
800 * @param[in] zone zone name
801 * @param[in] zoneDev Device ID in Zone network
802 * @param[in] hostDev Device ID in Host network
803 * @return status of this function call
805 VsmStatus vsm_create_netdev_veth(VsmClient client,
808 const char* hostDev);
810 * Create macvlan in zone
812 * @param[in] client vasum-server's client
813 * @param[in] zone Zone name
814 * @param[in] zoneDev Device ID in Zone network
815 * @param[in] hostDev Device ID in Host network
816 * @param[in] mode Mode with which macvlan will be created.
817 * @return status of this function call
821 VsmStatus vsm_create_netdev_macvlan(VsmClient client,
825 enum macvlan_mode mode);
827 * Create/move phys netdev in/to zone
829 * @param[in] client vasum-server's client
830 * @param[in] zone zone name
831 * @param[in] devId network device id
832 * @return status of this function call
834 VsmStatus vsm_create_netdev_phys(VsmClient client, const char* zone, const char* devId);
837 * Get netdev informations
839 * @param[in] client vasum-server's client
840 * @param[in] zone zone name
841 * @param[in] netdevId network device id
842 * @param[out] netdev netdev informations
843 * @return status of this function call
844 * @remark Use vsm_netdev_free() to free memory occupied by @p netdev.
846 VsmStatus vsm_lookup_netdev_by_name(VsmClient client,
848 const char* netdevId,
852 * Remove netdev from zone
854 * @param[in] client vasum-server's client
855 * @param[in] zone zone name
856 * @param[in] devId network device id
857 * @return status of this function call
859 VsmStatus vsm_destroy_netdev(VsmClient client, const char* zone, const char* devId);
862 * Create file, directory or pipe in zone
864 * Declare file, directory or pipe that will be created while zone startup
866 * @param[in] client vasum-server's client
867 * @param[in] type file type
868 * @param[in] zone zone id
869 * @param[in] path path to file
870 * @param[in] flags if O_CREAT bit is set then file will be created in zone,
871 * otherwise file will by copied from host;
872 * it is meaningful only when O_CREAT is set
873 * @param[in] mode mode of file
874 * @return status of this function call
876 VsmStatus vsm_declare_file(VsmClient client,
884 * Create mount point in zone
886 * Declare mount that will be created while zone startup
887 * Parameters are passed to mount system function
889 * @param[in] client vasum-server's client
890 * @param[in] source device path (path in host)
891 * @param[in] zone zone id
892 * @param[in] target mount point (path in zone)
893 * @param[in] type filesystem type
894 * @param[in] flags mount flags as in mount function
895 * @param[in] data additional data as in mount function
896 * @return status of this function call
898 VsmStatus vsm_declare_mount(VsmClient client,
907 * Create link in zone
909 * Declare link that will be created while zone startup
910 * Parameters are passed to link system function
912 * @param[in] client vasum-server's client
913 * @param[in] source path to link source (in host)
914 * @param[in] zone zone id
915 * @param[in] target path to link name (in zone)
916 * @return status of this function call
918 VsmStatus vsm_declare_link(VsmClient client,
924 * Get all declarations
926 * Gets all declarations of resourcies
927 * (@see ::vsm_declare_link, @see ::vsm_declare_mount, @see ::vsm_declare_file)
929 * @param[in] client vasum-server's client
930 * @param[in] zone zone id
931 * @param[out] declarations array of declarations id
932 * @return status of this function call
934 VsmStatus vsm_list_declarations(VsmClient client,
936 VsmArrayString* declarations);
941 * Removes given declaration by its id (@see ::vsm_list_declarations)
943 * @param[in] client vasum-server's client
944 * @param[in] zone zone id
945 * @param[in] declaration declaration id
946 * @return status of this function call
948 VsmStatus vsm_remove_declaration(VsmClient client,
950 VsmString declaration);
953 * Clean up zones root directory
955 * Removes all unknown zones root directory entry
956 * @return status of this function call
958 VsmStatus vsm_clean_up_zones_root(VsmClient client);
961 * Retrieve array size
965 unsigned int vsm_addrlist_size(VsmAddrList addrs);
968 * Get address type for i'th entry
970 * @return network type (AF_INET or AF_INET6)
972 int vsm_addrlist_get_type(VsmAddrList addrs, unsigned int i);
975 * Get pointer to in_addr property for i'th entry
976 * see inet_ntop man pages
978 * @return poiner of in_addr
980 const void *vsm_addrlist_get_addr(VsmAddrList addrs, unsigned int i);
983 * Get address prefix for i'th entry
985 * @return adress prefix (mask bits count)
987 unsigned int vsm_addrlist_get_prefix(VsmAddrList addrs, unsigned int i);
989 #endif /* __VASUM_WRAPPER_SOURCE__ */
995 #endif /* VASUM_CLIENT_H */