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;
190 * Completion status of libvasum-client's functions
193 VSMCLIENT_CUSTOM_ERROR, /**< User specified error */
194 VSMCLIENT_IO_ERROR, /**< Input/Output error */
195 VSMCLIENT_OPERATION_FAILED, /**< Operation failed */
196 VSMCLIENT_INVALID_ARGUMENT, /**< Invalid argument */
197 VSMCLIENT_OTHER_ERROR, /**< Other error */
198 VSMCLIENT_SUCCESS /**< Success */
204 typedef unsigned int VsmSubscriptionId;
226 typedef void* VsmZone;
229 * Netowrk device type
238 * Network device information
240 typedef void* VsmNetdev;
252 * Event dispacher types.
255 VSMDISPATCHER_EXTERNAL, /**< User must handle dispatching messages */
256 VSMDISPATCHER_INTERNAL /**< Library will take care of dispatching messages */
259 #ifndef __VASUM_WRAPPER_SOURCE__
262 * Get file descriptor associated with event dispatcher of zone client
264 * The function vsm_get_poll_fd() returns the file descriptor associated with the event dispatcher of vsm client.
265 * The file descriptor can be bound to another I/O multiplexing facilities like epoll, select, and poll.
267 * @param[in] client vsm client
268 * @param[out] fd epoll file descriptor
269 * @return status of this function call
271 VsmStatus vsm_get_poll_fd(VsmClient client, int* fd);
274 * Wait for an I/O event on a vsm client
276 * vsm_enter_eventloop() waits for event on the vsm client
278 * The call waits for a maximum time of timout milliseconds. Specifying a timeout of -1 makes vsm_enter_eventloop() wait indefinitely,
279 * while specifying a timeout equal to zero makes vsm_enter_eventloop() to return immediately even if no events are available.
281 * @param[in] client vasum-server's client
282 * @param[in] flags Reserved
283 * @param[in] timeout Timeout time (milisecond), -1 is infinite
284 * @return status of this function call
286 VsmStatus vsm_enter_eventloop(VsmClient client, int flags, int timeout);
289 * Set dispatching method
291 * @param[in] client vasum-server's client
292 * @param[in] dispacher dispatching method
293 * @return status of this function call
295 VsmStatus vsm_set_dispatcher_type(VsmClient client, VsmDispacherType dispacher);
298 * Get dispatching method
300 * @param[in] client vasum-server's client
301 * @param[out] dispacher dispatching method
302 * @return status of this function call
304 VsmStatus vsm_get_dispatcher_type(VsmClient client, VsmDispacherType* dispacher);
307 * Create a new vasum-server's client.
309 * @return Created client pointer or NULL on failure.
311 VsmClient vsm_client_create();
314 * Release client resources.
316 * @param[in] client vasum-server's client
318 void vsm_client_free(VsmClient client);
321 * Get status code of last vasum-server communication.
323 * @param[in] client vasum-server's client
324 * @return status of this function call
326 VsmStatus vsm_get_status(VsmClient client);
329 * Get status message of the last vasum-server communication.
331 * @param[in] client vasum-server's client
332 * @return last status message from vasum-server communication
334 const char* vsm_get_status_message(VsmClient client);
337 * Connect client to the vasum-server.
339 * @param[in] client vasum-server's client
340 * @return status of this function call
342 VsmStatus vsm_connect(VsmClient client);
345 * Connect client to the vasum-server via custom address.
347 * @param[in] client vasum-server's client
348 * @param[in] address dbus address
349 * @return status of this function call
351 VsmStatus vsm_connect_custom(VsmClient client, const char* address);
354 * Disconnect client from vasum-server.
356 * @param[in] client vasum-server's client
357 * @return status of this function call
359 VsmStatus vsm_disconnect(VsmClient client);
362 * Release VsmArrayString.
364 * @param[in] astring VsmArrayString
366 void vsm_array_string_free(VsmArrayString astring);
371 * @param string VsmString
373 void vsm_string_free(VsmString string);
376 * Get zone id (offline)
378 * @param zone VsmZone
381 VsmString vsm_zone_get_id(VsmZone zone);
384 * Get zone terminal (offline)
386 * @param zone VsmZone
387 * @return zone terminal
389 int vsm_zone_get_terminal(VsmZone zone);
392 * Get zone state (offline)
394 * @param zone VsmZone
397 VsmZoneState vsm_zone_get_state(VsmZone zone);
400 * Get zone rootfs path (offline)
402 * @param zone VsmZone
403 * @return zone rootfs path
405 VsmString vsm_zone_get_rootfs(VsmZone zone);
410 * @param zone VsmZone
412 void vsm_zone_free(VsmZone zone);
415 * Get netdev name (offline)
417 * @param netdev VsmNetdev
418 * @return netdev name
420 VsmString vsm_netdev_get_name(VsmNetdev netdev);
423 * Get netdev type (offline)
425 * @param netdev VsmNetdev
426 * @return netdev type
428 VsmNetdevType vsm_netdev_get_type(VsmNetdev netdev);
433 * @param netdev VsmNetdev
435 void vsm_netdev_free(VsmNetdev netdev);
438 * Zone's D-Bus state change callback function signature.
440 * @param[in] zoneId affected zone id
441 * @param[in] address new D-Bus address
442 * @param data custom user's data pointer passed to vsm_add_state_callback() function
444 typedef void (*VsmZoneDbusStateCallback)(const char* zoneId,
449 * Lock the command queue exclusively.
451 * @param[in] client vasum-server's client
452 * @return status of this function call
454 VsmStatus vsm_lock_queue(VsmClient client);
457 * Unlock the command queue.
459 * @param[in] client vasum-server's client
460 * @return status of this function call
462 VsmStatus vsm_unlock_queue(VsmClient client);
465 * Get dbus address of each zone.
467 * @param[in] client vasum-server's client
468 * @param[out] keys array of zones name
469 * @param[out] values array of zones dbus address
470 * @return status of this function call
471 * @post keys[i] corresponds to values[i]
472 * @remark Use vsm_array_string_free() to free memory occupied by @p keys and @p values.
474 VsmStatus vsm_get_zone_dbuses(VsmClient client, VsmArrayString* keys, VsmArrayString* values);
479 * @param[in] client vasum-server's client
480 * @param[out] array array of zones name
481 * @return status of this function call
482 * @remark Use vsm_array_string_free() to free memory occupied by @p array.
484 VsmStatus vsm_get_zone_ids(VsmClient client, VsmArrayString* array);
487 * Get active (foreground) zone name.
489 * @param[in] client vasum-server's client
490 * @param[out] id active zone name
491 * @return status of this function call
492 * @remark Use @p vsm_string_free() to free memory occupied by @p id.
494 VsmStatus vsm_get_active_zone_id(VsmClient client, VsmString* id);
497 * Get zone name of process with given pid.
499 * @param[in] client vasum-server's client
500 * @param[in] pid process id
501 * @param[out] id active zone name
502 * @return status of this function call
503 * @remark Use @p vsm_string_free() to free memory occupied by @p id.
505 VsmStatus vsm_lookup_zone_by_pid(VsmClient client, int pid, VsmString* id);
508 * Get zone informations of zone with given id.
510 * @param[in] client vasum-server's client
511 * @param[in] id zone name
512 * @param[out] zone zone informations
513 * @return status of this function call
514 * @remark Use @p vsm_zone_free() to free memory occupied by @p zone
516 VsmStatus vsm_lookup_zone_by_id(VsmClient client, const char* id, VsmZone* zone);
519 * Get zone name with given terminal.
521 * @param[in] client vasum-server's client
522 * @param[in] terminal terminal id
523 * @param[out] id zone name with given terminal
524 * @return status of this function call
525 * @remark Use @p vsm_string_free() to free memory occupied by @p id.
527 VsmStatus vsm_lookup_zone_by_terminal_id(VsmClient client, int terminal, VsmString* id);
530 * Set active (foreground) zone.
532 * @param[in] client vasum-server's client
533 * @param[in] id zone name
534 * @return status of this function call
536 VsmStatus vsm_set_active_zone(VsmClient client, const char* id);
539 * Create and add zone
541 * @param[in] client vasum-server's client
542 * @param[in] id zone id
543 * @param[in] tname template name, NULL is equivalent to "default"
544 * @return status of this function call
546 VsmStatus vsm_create_zone(VsmClient client, const char* id, const char* tname);
551 * @param[in] client vasum-server's client
552 * @param[in] id zone id
553 * @param[in] force if 0 data will be kept, otherwise data will be lost
554 * @return status of this function call
556 VsmStatus vsm_destroy_zone(VsmClient client, const char* id, int force);
561 * @param[in] client vasum-server's client
562 * @param[in] id zone name
563 * @return status of this function call
565 VsmStatus vsm_shutdown_zone(VsmClient client, const char* id);
570 * @param[in] client vasum-server's client
571 * @param[in] id zone name
572 * @return status of this function call
574 VsmStatus vsm_start_zone(VsmClient client, const char* id);
579 * @param[in] client vasum-server's client
580 * @param[in] id zone name
581 * @return status of this function call
583 VsmStatus vsm_lock_zone(VsmClient client, const char* id);
588 * @param[in] client vasum-server's client
589 * @param[in] id zone name
590 * @return status of this function call
592 VsmStatus vsm_unlock_zone(VsmClient client, const char* id);
595 * Register dbus state change callback function.
597 * @note The callback function will be invoked on a different thread.
599 * @param[in] client vasum-server's client
600 * @param[in] zoneDbusStateCallback callback function
601 * @param[in] data some extra data that will be passed to callback function
602 * @param[out] subscriptionId subscription identifier that can be used to unsubscribe signal,
603 * pointer can be NULL.
604 * @return status of this function call
606 VsmStatus vsm_add_state_callback(VsmClient client,
607 VsmZoneDbusStateCallback zoneDbusStateCallback,
609 VsmSubscriptionId* subscriptionId);
612 * Unregister dbus state change callback function.
614 * @param[in] client vasum-server's client
615 * @param[in] subscriptionId subscription identifier returned by vsm_add_state_callback
616 * @return status of this function call
618 VsmStatus vsm_del_state_callback(VsmClient client, VsmSubscriptionId subscriptionId);
621 * Grant access to device
623 * @param[in] client vasum-server's client
624 * @param[in] zone zone name
625 * @param[in] device device path
626 * @param[in] flags access flags
627 * @return status of this function call
629 VsmStatus vsm_grant_device(VsmClient client,
635 * Revoke access to device
637 * @param[in] client vasum-server's client
638 * @param[in] zone zone name
639 * @param[in] device device path
640 * @return status of this function call
642 VsmStatus vsm_revoke_device(VsmClient client, const char* zone, const char* device);
645 * Get array of netdev from given zone
647 * @param[in] client vasum-server's client
648 * @param[in] zone zone name
649 * @param[out] netdevIds array of netdev id
650 * @return status of this function call
651 * @remark Use vsm_array_string_free() to free memory occupied by @p netdevIds.
653 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] addr ipv4 address
662 * @return status of this function call
664 VsmStatus vsm_netdev_get_ipv4_addr(VsmClient client,
666 const char* netdevId,
667 struct in_addr *addr);
670 * Get ipv6 address for given netdevId
672 * @param[in] client vasum-server's client
673 * @param[in] zone zone name
674 * @param[in] netdevId netdev id
675 * @param[out] addr ipv6 address
676 * @return status of this function call
678 VsmStatus vsm_netdev_get_ipv6_addr(VsmClient client,
680 const char* netdevId,
681 struct in6_addr *addr);
684 * Set ipv4 address for given netdevId
686 * @param[in] client vasum-server's client
687 * @param[in] zone zone name
688 * @param[in] netdevId netdev id
689 * @param[in] addr ipv4 address
690 * @param[in] prefix bit-length of the network prefix
691 * @return status of this function call
693 VsmStatus vsm_netdev_set_ipv4_addr(VsmClient client,
695 const char* netdevId,
696 struct in_addr *addr,
700 * Set ipv6 address for given netdevId
702 * @param[in] client vasum-server's client
703 * @param[in] zone zone name
704 * @param[in] netdevId netdev id
705 * @param[in] addr ipv6 address
706 * @param[in] prefix bit-length of the network prefix
707 * @return status of this function call
709 VsmStatus vsm_netdev_set_ipv6_addr(VsmClient client,
711 const char* netdevId,
712 struct in6_addr *addr,
716 * Remove ipv4 address from netdev
718 * @param[in] client vasum-server's client
719 * @param[in] zone zone name
720 * @param[in] netdevId network device id
721 * @param[in] addr ipv4 address
722 * @param[in] prefix bit-length of the network prefix
723 * @return status of this function call
725 VsmStatus vsm_netdev_del_ipv4_addr(VsmClient client,
727 const char* netdevId,
728 struct in_addr* addr,
732 * Remove ipv6 address from netdev
734 * @param[in] client vasum-server's client
735 * @param[in] zone zone name
736 * @param[in] netdevId network device id
737 * @param[in] addr ipv6 address
738 * @param[in] prefix bit-length of the network prefix
739 * @return status of this function call
741 VsmStatus vsm_netdev_del_ipv6_addr(VsmClient client,
743 const char* netdevId,
744 struct in6_addr* addr,
748 * Turn up a network device in the zone
750 * @param[in] client vasum-server's client
751 * @param[in] zone zone name
752 * @param[in] netdevId netdev id
753 * @return status of this function call
755 VsmStatus vsm_netdev_up(VsmClient client,
757 const char* netdevId);
760 * Turn down a network device in the zone
762 * @param[in] client vasum-server's client
763 * @param[in] zone zone name
764 * @param[in] netdevId netdev id
765 * @return status of this function call
767 VsmStatus vsm_netdev_down(VsmClient client,
769 const char* netdevId);
773 * Create veth netdev in zone
775 * @param[in] client vasum-server's client
776 * @param[in] zone zone name
777 * @param[in] zoneDev Device ID in Zone network
778 * @param[in] hostDev Device ID in Host network
779 * @return status of this function call
781 VsmStatus vsm_create_netdev_veth(VsmClient client,
784 const char* hostDev);
786 * Create macvlan in zone
788 * @param[in] client vasum-server's client
789 * @param[in] zone Zone name
790 * @param[in] zoneDev Device ID in Zone network
791 * @param[in] hostDev Device ID in Host network
792 * @param[in] mode Mode with which macvlan will be created.
793 * @return status of this function call
797 VsmStatus vsm_create_netdev_macvlan(VsmClient client,
801 enum macvlan_mode mode);
803 * Create/move phys netdev in/to zone
805 * @param[in] client vasum-server's client
806 * @param[in] zone zone name
807 * @param[in] devId network device id
808 * @return status of this function call
810 VsmStatus vsm_create_netdev_phys(VsmClient client, const char* zone, const char* devId);
813 * Get netdev informations
815 * @param[in] client vasum-server's client
816 * @param[in] zone zone name
817 * @param[in] netdevId network device id
818 * @param[out] netdev netdev informations
819 * @return status of this function call
820 * @remark Use vsm_netdev_free() to free memory occupied by @p netdev.
822 VsmStatus vsm_lookup_netdev_by_name(VsmClient client,
824 const char* netdevId,
828 * Remove netdev from zone
830 * @param[in] client vasum-server's client
831 * @param[in] zone zone name
832 * @param[in] devId network device id
833 * @return status of this function call
835 VsmStatus vsm_destroy_netdev(VsmClient client, const char* zone, const char* devId);
838 * Create file, directory or pipe in zone
840 * Declare file, directory or pipe that will be created while zone startup
842 * @param[in] client vasum-server's client
843 * @param[in] type file type
844 * @param[in] zone zone id
845 * @param[in] path path to file
846 * @param[in] flags if O_CREAT bit is set then file will be created in zone,
847 * otherwise file will by copied from host;
848 * it is meaningful only when O_CREAT is set
849 * @param[in] mode mode of file
850 * @return status of this function call
852 VsmStatus vsm_declare_file(VsmClient client,
860 * Create mount point in zone
862 * Declare mount that will be created while zone startup
863 * Parameters are passed to mount system function
865 * @param[in] client vasum-server's client
866 * @param[in] source device path (path in host)
867 * @param[in] zone zone id
868 * @param[in] target mount point (path in zone)
869 * @param[in] type filesystem type
870 * @param[in] flags mount flags as in mount function
871 * @param[in] data additional data as in mount function
872 * @return status of this function call
874 VsmStatus vsm_declare_mount(VsmClient client,
883 * Create link in zone
885 * Declare link that will be created while zone startup
886 * Parameters are passed to link system function
888 * @param[in] client vasum-server's client
889 * @param[in] source path to link source (in host)
890 * @param[in] zone zone id
891 * @param[in] target path to link name (in zone)
892 * @return status of this function call
894 VsmStatus vsm_declare_link(VsmClient client,
900 * Get all declarations
902 * Gets all declarations of resourcies
903 * (@see ::vsm_declare_link, @see ::vsm_declare_mount, @see ::vsm_declare_file)
905 * @param[in] client vasum-server's client
906 * @param[in] zone zone id
907 * @param[out] declarations array of declarations id
908 * @return status of this function call
910 VsmStatus vsm_list_declarations(VsmClient client,
912 VsmArrayString* declarations);
917 * Removes given declaration by its id (@see ::vsm_list_declarations)
919 * @param[in] client vasum-server's client
920 * @param[in] zone zone id
921 * @param[in] declaration declaration id
922 * @return status of this function call
924 VsmStatus vsm_remove_declaration(VsmClient client,
926 VsmString declaration);
929 * Clean up zones root directory
931 * Removes all unknown zones root directory entry
932 * @return status of this function call
934 VsmStatus vsm_clean_up_zones_root(VsmClient client);
936 #endif /* __VASUM_WRAPPER_SOURCE__ */
942 #endif /* VASUM_CLIENT_H */