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 */
261 * Get file descriptor associated with event dispatcher of zone client
263 * The function vsm_get_poll_fd() returns the file descriptor associated with the event dispatcher of vsm client.
264 * The file descriptor can be bound to another I/O multiplexing facilities like epoll, select, and poll.
266 * @param[in] client vsm client
267 * @param[out] fd epoll file descriptor
268 * @return status of this function call
270 VsmStatus vsm_get_poll_fd(VsmClient client, int* fd);
273 * Wait for an I/O event on a vsm client
275 * vsm_enter_eventloop() waits for event on the vsm client
277 * The call waits for a maximum time of timout milliseconds. Specifying a timeout of -1 makes vsm_enter_eventloop() wait indefinitely,
278 * while specifying a timeout equal to zero makes vsm_enter_eventloop() to return immediately even if no events are available.
280 * @param[in] client vasum-server's client
281 * @param[in] flags Reserved
282 * @param[in] timeout Timeout time (milisecond), -1 is infinite
283 * @return status of this function call
285 VsmStatus vsm_enter_eventloop(VsmClient client, int flags, int timeout);
288 * Set dispatching method
290 * @param[in] client vasum-server's client
291 * @param[in] dispacher dispatching method
292 * @return status of this function call
294 VsmStatus vsm_set_dispatcher_type(VsmClient client, VsmDispacherType dispacher);
297 * Get dispatching method
299 * @param[in] client vasum-server's client
300 * @param[out] dispacher dispatching method
301 * @return status of this function call
303 VsmStatus vsm_get_dispatcher_type(VsmClient client, VsmDispacherType* dispacher);
306 * Create a new vasum-server's client.
308 * @return Created client pointer or NULL on failure.
310 VsmClient vsm_client_create();
313 * Release client resources.
315 * @param[in] client vasum-server's client
317 void vsm_client_free(VsmClient client);
320 * Get status code of last vasum-server communication.
322 * @param[in] client vasum-server's client
323 * @return status of this function call
325 VsmStatus vsm_get_status(VsmClient client);
328 * Get status message of the last vasum-server communication.
330 * @param[in] client vasum-server's client
331 * @return last status message from vasum-server communication
333 const char* vsm_get_status_message(VsmClient client);
336 * Connect client to the vasum-server.
338 * @param[in] client vasum-server's client
339 * @return status of this function call
341 VsmStatus vsm_connect(VsmClient client);
344 * Connect client to the vasum-server via custom address.
346 * @param[in] client vasum-server's client
347 * @param[in] address dbus address
348 * @return status of this function call
350 VsmStatus vsm_connect_custom(VsmClient client, const char* address);
353 * Disconnect client from vasum-server.
355 * @param[in] client vasum-server's client
356 * @return status of this function call
358 VsmStatus vsm_disconnect(VsmClient client);
361 * Release VsmArrayString.
363 * @param[in] astring VsmArrayString
365 void vsm_array_string_free(VsmArrayString astring);
370 * @param string VsmString
372 void vsm_string_free(VsmString string);
375 * Get zone id (offline)
377 * @param zone VsmZone
380 VsmString vsm_zone_get_id(VsmZone zone);
383 * Get zone terminal (offline)
385 * @param zone VsmZone
386 * @return zone terminal
388 int vsm_zone_get_terminal(VsmZone zone);
391 * Get zone state (offline)
393 * @param zone VsmZone
396 VsmZoneState vsm_zone_get_state(VsmZone zone);
399 * Get zone rootfs path (offline)
401 * @param zone VsmZone
402 * @return zone rootfs path
404 VsmString vsm_zone_get_rootfs(VsmZone zone);
409 * @param zone VsmZone
411 void vsm_zone_free(VsmZone zone);
414 * Get netdev name (offline)
416 * @param netdev VsmNetdev
417 * @return netdev name
419 VsmString vsm_netdev_get_name(VsmNetdev netdev);
422 * Get netdev type (offline)
424 * @param netdev VsmNetdev
425 * @return netdev type
427 VsmNetdevType vsm_netdev_get_type(VsmNetdev netdev);
432 * @param netdev VsmNetdev
434 void vsm_netdev_free(VsmNetdev netdev);
437 * Zone's D-Bus state change callback function signature.
439 * @param[in] zoneId affected zone id
440 * @param[in] address new D-Bus address
441 * @param data custom user's data pointer passed to vsm_add_state_callback() function
443 typedef void (*VsmZoneDbusStateCallback)(const char* zoneId,
448 * Lock the command queue exclusively.
450 * @param[in] client vasum-server's client
451 * @return status of this function call
453 VsmStatus vsm_lock_queue(VsmClient client);
456 * Unlock the command queue.
458 * @param[in] client vasum-server's client
459 * @return status of this function call
461 VsmStatus vsm_unlock_queue(VsmClient client);
464 * Get dbus address of each zone.
466 * @param[in] client vasum-server's client
467 * @param[out] keys array of zones name
468 * @param[out] values array of zones dbus address
469 * @return status of this function call
470 * @post keys[i] corresponds to values[i]
471 * @remark Use vsm_array_string_free() to free memory occupied by @p keys and @p values.
473 VsmStatus vsm_get_zone_dbuses(VsmClient client, VsmArrayString* keys, VsmArrayString* values);
478 * @param[in] client vasum-server's client
479 * @param[out] array array of zones name
480 * @return status of this function call
481 * @remark Use vsm_array_string_free() to free memory occupied by @p array.
483 VsmStatus vsm_get_zone_ids(VsmClient client, VsmArrayString* array);
486 * Get active (foreground) zone name.
488 * @param[in] client vasum-server's client
489 * @param[out] id active zone name
490 * @return status of this function call
491 * @remark Use @p vsm_string_free() to free memory occupied by @p id.
493 VsmStatus vsm_get_active_zone_id(VsmClient client, VsmString* id);
496 * Get zone name of process with given pid.
498 * @param[in] client vasum-server's client
499 * @param[in] pid process id
500 * @param[out] id active zone name
501 * @return status of this function call
502 * @remark Use @p vsm_string_free() to free memory occupied by @p id.
504 VsmStatus vsm_lookup_zone_by_pid(VsmClient client, int pid, VsmString* id);
507 * Get zone informations of zone with given id.
509 * @param[in] client vasum-server's client
510 * @param[in] id zone name
511 * @param[out] zone zone informations
512 * @return status of this function call
513 * @remark Use @p vsm_zone_free() to free memory occupied by @p zone
515 VsmStatus vsm_lookup_zone_by_id(VsmClient client, const char* id, VsmZone* zone);
518 * Get zone name with given terminal.
520 * @param[in] client vasum-server's client
521 * @param[in] terminal terminal id
522 * @param[out] id zone name with given terminal
523 * @return status of this function call
524 * @remark Use @p vsm_string_free() to free memory occupied by @p id.
526 VsmStatus vsm_lookup_zone_by_terminal_id(VsmClient client, int terminal, VsmString* id);
529 * Set active (foreground) zone.
531 * @param[in] client vasum-server's client
532 * @param[in] id zone name
533 * @return status of this function call
535 VsmStatus vsm_set_active_zone(VsmClient client, const char* id);
538 * Create and add zone
540 * @param[in] client vasum-server's client
541 * @param[in] id zone id
542 * @param[in] tname template name, NULL is equivalent to "default"
543 * @return status of this function call
545 VsmStatus vsm_create_zone(VsmClient client, const char* id, const char* tname);
550 * @param[in] client vasum-server's client
551 * @param[in] id zone id
552 * @param[in] force if 0 data will be kept, otherwise data will be lost
553 * @return status of this function call
555 VsmStatus vsm_destroy_zone(VsmClient client, const char* id, int force);
560 * @param[in] client vasum-server's client
561 * @param[in] id zone name
562 * @return status of this function call
564 VsmStatus vsm_shutdown_zone(VsmClient client, const char* id);
569 * @param[in] client vasum-server's client
570 * @param[in] id zone name
571 * @return status of this function call
573 VsmStatus vsm_start_zone(VsmClient client, const char* id);
578 * @param[in] client vasum-server's client
579 * @param[in] id zone name
580 * @return status of this function call
582 VsmStatus vsm_lock_zone(VsmClient client, const char* id);
587 * @param[in] client vasum-server's client
588 * @param[in] id zone name
589 * @return status of this function call
591 VsmStatus vsm_unlock_zone(VsmClient client, const char* id);
594 * Register dbus state change callback function.
596 * @note The callback function will be invoked on a different thread.
598 * @param[in] client vasum-server's client
599 * @param[in] zoneDbusStateCallback callback function
600 * @param[in] data some extra data that will be passed to callback function
601 * @param[out] subscriptionId subscription identifier that can be used to unsubscribe signal,
602 * pointer can be NULL.
603 * @return status of this function call
605 VsmStatus vsm_add_state_callback(VsmClient client,
606 VsmZoneDbusStateCallback zoneDbusStateCallback,
608 VsmSubscriptionId* subscriptionId);
611 * Unregister dbus state change callback function.
613 * @param[in] client vasum-server's client
614 * @param[in] subscriptionId subscription identifier returned by vsm_add_state_callback
615 * @return status of this function call
617 VsmStatus vsm_del_state_callback(VsmClient client, VsmSubscriptionId subscriptionId);
620 * Grant access to device
622 * @param[in] client vasum-server's client
623 * @param[in] zone zone name
624 * @param[in] device device path
625 * @param[in] flags access flags
626 * @return status of this function call
628 VsmStatus vsm_grant_device(VsmClient client,
634 * Revoke access to device
636 * @param[in] client vasum-server's client
637 * @param[in] zone zone name
638 * @param[in] device device path
639 * @return status of this function call
641 VsmStatus vsm_revoke_device(VsmClient client, const char* zone, const char* device);
644 * Get array of netdev from given zone
646 * @param[in] client vasum-server's client
647 * @param[in] zone zone name
648 * @param[out] netdevIds array of netdev id
649 * @return status of this function call
650 * @remark Use vsm_array_string_free() to free memory occupied by @p netdevIds.
652 VsmStatus vsm_zone_get_netdevs(VsmClient client, const char* zone, VsmArrayString* netdevIds);
656 * Get ipv4 address for given netdevId
658 * @param[in] client vasum-server's client
659 * @param[in] zone zone name
660 * @param[in] netdevId netdev id
661 * @param[out] addrs ip address array
662 * @return status of this function call
663 * @remark Use vsm_netdev_addr_free() to free memory occupied by address array.
665 VsmStatus vsm_netdev_get_ip_addr(VsmClient client,
667 const char* netdevId,
671 * Release VsmAddrList
673 * @param addrs VsmAddrList
675 void vsm_addrlist_free(VsmAddrList addrs);
678 * Get ipv4 address for given netdevId
680 * @param[in] client vasum-server's client
681 * @param[in] zone zone name
682 * @param[in] netdevId netdev id
683 * @param[out] addr ipv4 address
684 * @return status of this function call
686 VsmStatus vsm_netdev_get_ipv4_addr(VsmClient client,
688 const char* netdevId,
689 struct in_addr *addr);
692 * Get ipv6 address for given netdevId
694 * @param[in] client vasum-server's client
695 * @param[in] zone zone name
696 * @param[in] netdevId netdev id
697 * @param[out] addr ipv6 address
698 * @return status of this function call
700 VsmStatus vsm_netdev_get_ipv6_addr(VsmClient client,
702 const char* netdevId,
703 struct in6_addr *addr);
706 * Add ipv4 address for given netdevId
708 * @param[in] client vasum-server's client
709 * @param[in] zone zone name
710 * @param[in] netdevId netdev id
711 * @param[in] addr ipv4 address
712 * @param[in] prefix bit-length of the network prefix
713 * @return status of this function call
715 VsmStatus vsm_netdev_add_ipv4_addr(VsmClient client,
717 const char* netdevId,
718 struct in_addr *addr,
722 * Add ipv6 address for given netdevId
724 * @param[in] client vasum-server's client
725 * @param[in] zone zone name
726 * @param[in] netdevId netdev id
727 * @param[in] addr ipv6 address
728 * @param[in] prefix bit-length of the network prefix
729 * @return status of this function call
731 VsmStatus vsm_netdev_add_ipv6_addr(VsmClient client,
733 const char* netdevId,
734 struct in6_addr *addr,
738 * Remove ipv4 address from netdev
740 * @param[in] client vasum-server's client
741 * @param[in] zone zone name
742 * @param[in] netdevId network device id
743 * @param[in] addr ipv4 address
744 * @param[in] prefix bit-length of the network prefix
745 * @return status of this function call
747 VsmStatus vsm_netdev_del_ipv4_addr(VsmClient client,
749 const char* netdevId,
750 struct in_addr* addr,
754 * Remove ipv6 address from netdev
756 * @param[in] client vasum-server's client
757 * @param[in] zone zone name
758 * @param[in] netdevId network device id
759 * @param[in] addr ipv6 address
760 * @param[in] prefix bit-length of the network prefix
761 * @return status of this function call
763 VsmStatus vsm_netdev_del_ipv6_addr(VsmClient client,
765 const char* netdevId,
766 struct in6_addr* addr,
770 * Turn up a network device in the zone
772 * @param[in] client vasum-server's client
773 * @param[in] zone zone name
774 * @param[in] netdevId netdev id
775 * @return status of this function call
777 VsmStatus vsm_netdev_up(VsmClient client,
779 const char* netdevId);
782 * Turn down a network device in the zone
784 * @param[in] client vasum-server's client
785 * @param[in] zone zone name
786 * @param[in] netdevId netdev id
787 * @return status of this function call
789 VsmStatus vsm_netdev_down(VsmClient client,
791 const char* netdevId);
795 * Create veth netdev in zone
797 * @param[in] client vasum-server's client
798 * @param[in] zone zone name
799 * @param[in] zoneDev Device ID in Zone network
800 * @param[in] hostDev Device ID in Host network
801 * @return status of this function call
803 VsmStatus vsm_create_netdev_veth(VsmClient client,
806 const char* hostDev);
808 * Create macvlan in zone
810 * @param[in] client vasum-server's client
811 * @param[in] zone Zone name
812 * @param[in] zoneDev Device ID in Zone network
813 * @param[in] hostDev Device ID in Host network
814 * @param[in] mode Mode with which macvlan will be created.
815 * @return status of this function call
819 VsmStatus vsm_create_netdev_macvlan(VsmClient client,
823 enum macvlan_mode mode);
825 * Create/move phys netdev in/to zone
827 * @param[in] client vasum-server's client
828 * @param[in] zone zone name
829 * @param[in] devId network device id
830 * @return status of this function call
832 VsmStatus vsm_create_netdev_phys(VsmClient client, const char* zone, const char* devId);
835 * Get netdev informations
837 * @param[in] client vasum-server's client
838 * @param[in] zone zone name
839 * @param[in] netdevId network device id
840 * @param[out] netdev netdev informations
841 * @return status of this function call
842 * @remark Use vsm_netdev_free() to free memory occupied by @p netdev.
844 VsmStatus vsm_lookup_netdev_by_name(VsmClient client,
846 const char* netdevId,
850 * Remove netdev from zone
852 * @param[in] client vasum-server's client
853 * @param[in] zone zone name
854 * @param[in] devId network device id
855 * @return status of this function call
857 VsmStatus vsm_destroy_netdev(VsmClient client, const char* zone, const char* devId);
860 * Create file, directory or pipe in zone
862 * Declare file, directory or pipe that will be created while zone startup
864 * @param[in] client vasum-server's client
865 * @param[in] type file type
866 * @param[in] zone zone id
867 * @param[in] path path to file
868 * @param[in] flags if O_CREAT bit is set then file will be created in zone,
869 * otherwise file will by copied from host;
870 * it is meaningful only when O_CREAT is set
871 * @param[in] mode mode of file
872 * @return status of this function call
874 VsmStatus vsm_declare_file(VsmClient client,
882 * Create mount point in zone
884 * Declare mount that will be created while zone startup
885 * Parameters are passed to mount system function
887 * @param[in] client vasum-server's client
888 * @param[in] source device path (path in host)
889 * @param[in] zone zone id
890 * @param[in] target mount point (path in zone)
891 * @param[in] type filesystem type
892 * @param[in] flags mount flags as in mount function
893 * @param[in] data additional data as in mount function
894 * @return status of this function call
896 VsmStatus vsm_declare_mount(VsmClient client,
905 * Create link in zone
907 * Declare link that will be created while zone startup
908 * Parameters are passed to link system function
910 * @param[in] client vasum-server's client
911 * @param[in] source path to link source (in host)
912 * @param[in] zone zone id
913 * @param[in] target path to link name (in zone)
914 * @return status of this function call
916 VsmStatus vsm_declare_link(VsmClient client,
922 * Get all declarations
924 * Gets all declarations of resourcies
925 * (@see ::vsm_declare_link, @see ::vsm_declare_mount, @see ::vsm_declare_file)
927 * @param[in] client vasum-server's client
928 * @param[in] zone zone id
929 * @param[out] declarations array of declarations id
930 * @return status of this function call
932 VsmStatus vsm_list_declarations(VsmClient client,
934 VsmArrayString* declarations);
939 * Removes given declaration by its id (@see ::vsm_list_declarations)
941 * @param[in] client vasum-server's client
942 * @param[in] zone zone id
943 * @param[in] declaration declaration id
944 * @return status of this function call
946 VsmStatus vsm_remove_declaration(VsmClient client,
948 VsmString declaration);
951 * Clean up zones root directory
953 * Removes all unknown zones root directory entry
954 * @return status of this function call
956 VsmStatus vsm_clean_up_zones_root(VsmClient client);
959 * Retrieve array size
963 unsigned int vsm_addrlist_size(VsmAddrList addrs);
966 * Get address type for i'th entry
968 * @return network type (AF_INET or AF_INET6)
970 int vsm_addrlist_get_type(VsmAddrList addrs, unsigned int i);
973 * Get pointer to in_addr property for i'th entry
974 * see inet_ntop man pages
976 * @return poiner of in_addr
978 const void *vsm_addrlist_get_addr(VsmAddrList addrs, unsigned int i);
981 * Get address prefix for i'th entry
983 * @return adress prefix (mask bits count)
985 unsigned int vsm_addrlist_get_prefix(VsmAddrList addrs, unsigned int i);
991 #endif /* VASUM_CLIENT_H */