int main(int argc, char *argv[]) { int ret_cod; /* step 1: handle declaration */ flom_handle_t my_handle; /* step 2: handle initialization */ if (FLOM_RC_OK != (ret_cod = flom_handle_init(&my_handle))) { fprintf(stderr, "flom_handle_init() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* step 3a: set a different AF_UNIX/PF_LOCAL socket to connect to FLoM daemon */ if (FLOM_RC_OK != (ret_cod = flom_handle_set_socket_name( &my_handle, "/tmp/my_socket_name"))) { fprintf(stderr, "flom_handle_set_socket_name() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* step 3b: set a different (non default) resource name to lock */ if (FLOM_RC_OK != (ret_cod = flom_handle_set_resource_name( &my_handle, "Red.Blue.Green"))) { fprintf(stderr, "flom_handle_set_resource_name() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* step 4: lock acquisition */ if (FLOM_RC_OK != (ret_cod = flom_handle_lock(&my_handle))) { fprintf(stderr, "flom_handle_lock() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } else if (NULL != flom_handle_get_locked_element(&my_handle)) { printf("flom_handle_get_locked_element(): '%s'\n", flom_handle_get_locked_element(&my_handle)); } /* step 5: execute the code that needs lock protection */ /* step 6: lock release */ if (FLOM_RC_OK != (ret_cod = flom_handle_unlock(&my_handle))) { fprintf(stderr, "flom_handle_unlock() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* step 7: handle clean-up (memory release) */ if (FLOM_RC_OK != (ret_cod = flom_handle_clean(&my_handle))) { fprintf(stderr, "flom_handle_clean() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* exit */ return 0; }
/* * Happy path usage with a static handle */ void static_handle_happy_path(void) { int ret_cod; flom_handle_t my_handle; /* initialize a new handle */ if (FLOM_RC_OK != (ret_cod = flom_handle_init(&my_handle))) { fprintf(stderr, "flom_handle_init() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* get current AF_UNIX/PF_LOCAL socket_name */ printf("flom_handle_get_socket_name() = '%s'\n", flom_handle_get_socket_name(&my_handle)); /* set a new AF_UNIX/PF_LOCAL socket_name */ if (FLOM_RC_OK != (ret_cod = flom_handle_set_socket_name( &my_handle, nd_socket_name))) { fprintf(stderr, "flom_handle_set_socket_name() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* get new AF_UNIX/PF_LOCAL socket_name */ printf("flom_handle_get_socket_name() = '%s'\n", flom_handle_get_socket_name(&my_handle)); /* check socket name */ if (strcmp(nd_socket_name, flom_handle_get_socket_name(&my_handle))) { fprintf(stderr, "Unexpected result from flom_handle_set/get_socket_name\n"); exit(1); } /* we don't get current trace filename because it can be altered by a global config file */ /* set a new trace filename */ flom_handle_set_trace_filename(&my_handle, nd_trace_filename); /* get new trace filename */ printf("flom_handle_get_trace_filename() = '%s'\n", flom_handle_get_trace_filename(&my_handle)); /* check trace filename */ if (strcmp(nd_trace_filename, flom_handle_get_trace_filename(&my_handle))) { fprintf(stderr, "Unexpected result from flom_handle_set/get_trace_filename\n"); exit(1); } /* get current resource name */ printf("flom_handle_get_resource_name() = '%s'\n", flom_handle_get_resource_name(&my_handle)); /* set a new resource name */ if (FLOM_RC_OK != (ret_cod = flom_handle_set_resource_name( &my_handle, nd_resource_name))) { fprintf(stderr, "flom_handle_set_resource_name() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* get new resource name */ printf("flom_handle_get_resource_name() = '%s'\n", flom_handle_get_resource_name(&my_handle)); /* check resource name */ if (strcmp(nd_resource_name, flom_handle_get_resource_name(&my_handle))) { fprintf(stderr, "Unexpected result from flom_handle_set/get_resource_name\n"); exit(1); } /* get current value for resource create property */ printf("flom_handle_get_resource_create() = %d\n", flom_handle_get_resource_create(&my_handle)); /* set a new value for resource create property */ flom_handle_set_resource_create(&my_handle, FALSE); /* get new value for resource create property */ printf("flom_handle_get_resource_create() = %d\n", flom_handle_get_resource_create(&my_handle)); /* check resource create 1/2 */ if (flom_handle_get_resource_create(&my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/get_resource_create\n"); exit(1); } /* set a new value for resource create property */ flom_handle_set_resource_create(&my_handle, TRUE); /* get new value for resource create property */ printf("flom_handle_get_resource_create() = %d\n", flom_handle_get_resource_create(&my_handle)); /* check resource create 2/2 */ if (!flom_handle_get_resource_create(&my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_resource_create\n"); exit(1); } /* get current value for resource timeout property */ printf("flom_handle_get_resource_timeout() = %d\n", flom_handle_get_resource_timeout(&my_handle)); /* set a new value for resource timeout property */ flom_handle_set_resource_timeout(&my_handle, -1); /* get new value for resource timeout property */ printf("flom_handle_get_resource_timeout() = %d\n", flom_handle_get_resource_timeout(&my_handle)); /* check resource timeout */ if (-1 != flom_handle_get_resource_timeout(&my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_resource_timeout\n"); exit(1); } /* get current value for resource quantity property */ printf("flom_handle_get_resource_quantity() = %d\n", flom_handle_get_resource_quantity(&my_handle)); /* set a new value for resource quantity property */ flom_handle_set_resource_quantity(&my_handle, 3); /* get new value for resource quantity property */ printf("flom_handle_get_resource_quantity() = %d\n", flom_handle_get_resource_quantity(&my_handle)); /* check resource quantity */ if (3 != flom_handle_get_resource_quantity(&my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_resource_quantity\n"); exit(1); } /* get current value for resource lock mode property */ printf("flom_handle_get_lock_mode() = %d\n", flom_handle_get_lock_mode(&my_handle)); /* set a new value for resource lock mode property */ flom_handle_set_lock_mode(&my_handle, FLOM_LOCK_MODE_PW); /* get new value for resource lock mode property */ printf("flom_handle_get_lock_mode() = %d\n", flom_handle_get_lock_mode(&my_handle)); /* check resource lock mode */ if (FLOM_LOCK_MODE_PW != flom_handle_get_lock_mode(&my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/get_lock_mode\n"); exit(1); } /* get current value for resource idle lifespan */ printf("flom_handle_get_resource_idle_lifespan() = %d\n", flom_handle_get_resource_idle_lifespan(&my_handle)); /* set a new value for resource idle lifespan */ flom_handle_set_resource_idle_lifespan(&my_handle, 10000); /* get new value for resource idle lifespan */ printf("flom_handle_get_resource_idle_lifespan() = %d\n", flom_handle_get_resource_idle_lifespan(&my_handle)); /* check resource idle lifespan */ if (10000 != flom_handle_get_resource_idle_lifespan(&my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_resource_idle_lifespan\n"); exit(1); } /* get current unicast address */ printf("flom_handle_get_unicast_address() = '%s'\n", flom_handle_get_unicast_address(&my_handle)); /* set a new unicast_address */ flom_handle_set_unicast_address(&my_handle, nd_unicast_address); /* get new unicast address */ printf("flom_handle_get_unicast_address() = '%s'\n", flom_handle_get_unicast_address(&my_handle)); /* check unicast address */ if (strcmp(nd_unicast_address, flom_handle_get_unicast_address(&my_handle))) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_unicast_address\n"); exit(1); } /* get current multicast address */ printf("flom_handle_get_multicast_address() = '%s'\n", flom_handle_get_multicast_address(&my_handle)); /* set a new multicast_address */ flom_handle_set_multicast_address(&my_handle, nd_multicast_address); /* get new multicast address */ printf("flom_handle_get_multicast_address() = '%s'\n", flom_handle_get_multicast_address(&my_handle)); /* check multicast address */ if (strcmp(nd_multicast_address, flom_handle_get_multicast_address(&my_handle))) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_multicast_address\n"); exit(1); } /* set AF_UNIX/PF_LOCAL socket_name again */ if (FLOM_RC_OK != (ret_cod = flom_handle_set_socket_name( &my_handle, nd_socket_name))) { fprintf(stderr, "flom_handle_set_socket_name() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* get current value for unicast port */ printf("flom_handle_get_unicast_port() = %d\n", flom_handle_get_unicast_port(&my_handle)); /* set a new value for unicast_port */ flom_handle_set_unicast_port(&my_handle, 7777); /* get new value for unicast port */ printf("flom_handle_get_unicast_port() = %d\n", flom_handle_get_unicast_port(&my_handle)); /* check unicast port */ if (7777 != flom_handle_get_unicast_port(&my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/get_unicast_port\n"); exit(1); } /* get current value for multicast port */ printf("flom_handle_get_multicast_port() = %d\n", flom_handle_get_multicast_port(&my_handle)); /* set a new value for multicast_port */ flom_handle_set_multicast_port(&my_handle, 8888); /* get new value for multicast port */ printf("flom_handle_get_multicast_port() = %d\n", flom_handle_get_multicast_port(&my_handle)); /* check multicast port */ if (8888 != flom_handle_get_multicast_port(&my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/get_multicast_port\n"); exit(1); } /* get current value for discovery attempts property */ printf("flom_handle_get_discovery_attempts() = %d\n", flom_handle_get_discovery_attempts(&my_handle)); /* set a new value for discovery attempts property */ flom_handle_set_discovery_attempts(&my_handle, 5); /* get new value for discovery attempts */ printf("flom_handle_get_discovery_attempts() = %d\n", flom_handle_get_discovery_attempts(&my_handle)); /* check discovery attempts */ if (5 != flom_handle_get_discovery_attempts(&my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_discovery_attempts\n"); exit(1); } /* get current value for discovery timeout property */ printf("flom_handle_get_discovery_timeout() = %d\n", flom_handle_get_discovery_timeout(&my_handle)); /* set a new value for discovery timeout property */ flom_handle_set_discovery_timeout(&my_handle, 750); /* get new value for discovery timeout */ printf("flom_handle_get_discovery_timeout() = %d\n", flom_handle_get_discovery_timeout(&my_handle)); /* check discovery timeout */ if (750 != flom_handle_get_discovery_timeout(&my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_discovery_timeout\n"); exit(1); } /* get current value for discovery ttl property */ printf("flom_handle_get_discovery_ttl() = %d\n", flom_handle_get_discovery_ttl(&my_handle)); /* set a new value for discovery ttl property */ flom_handle_set_discovery_ttl(&my_handle, 2); /* get new value for discovery ttl */ printf("flom_handle_get_discovery_ttl() = %d\n", flom_handle_get_discovery_ttl(&my_handle)); /* check discovery ttl */ if (2 != flom_handle_get_discovery_ttl(&my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/get_discovery_ttl\n"); exit(1); } /* lock acquisition */ if (FLOM_RC_OK != (ret_cod = flom_handle_lock(&my_handle))) { fprintf(stderr, "flom_handle_lock() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } else if (NULL != flom_handle_get_locked_element(&my_handle)) { printf("staticHandleHappyPath locked element is %s\n", flom_handle_get_locked_element(&my_handle)); } /* lock release */ if (FLOM_RC_OK != (ret_cod = flom_handle_unlock(&my_handle))) { fprintf(stderr, "flom_handle_unlock() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* handle clean-up (memory release) */ if (FLOM_RC_OK != (ret_cod = flom_handle_clean(&my_handle))) { fprintf(stderr, "flom_handle_clean() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } }
/* * Happy path usage with a dynamic handle */ void dynamic_handle_happy_path(const char *nd_network_interface) { int ret_cod; flom_handle_t *my_handle = NULL; /* create a new handle */ if (NULL == (my_handle = flom_handle_new())) { fprintf(stderr, "flom_handle_init() returned %p\n", my_handle); exit(1); } /* get current AF_UNIX/PF_LOCAL socket_name */ printf("flom_handle_get_socket_name() = '%s'\n", flom_handle_get_socket_name(my_handle)); /* set a new AF_UNIX/PF_LOCAL socket_name */ if (FLOM_RC_OK != (ret_cod = flom_handle_set_socket_name( my_handle, nd_socket_name))) { fprintf(stderr, "flom_handle_set_socket_name() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* get new AF_UNIX/PF_LOCAL socket_name */ printf("flom_handle_get_socket_name() = '%s'\n", flom_handle_get_socket_name(my_handle)); /* check socket name */ if (strcmp(nd_socket_name, flom_handle_get_socket_name(my_handle))) { fprintf(stderr, "Unexpected result from flom_handle_set/get_socket_name\n"); exit(1); } /* we don't get current trace filename because it can be altered by a global config file */ /* set a new trace filename */ flom_handle_set_trace_filename(my_handle, nd_trace_filename); /* get new trace filename */ printf("flom_handle_get_trace_filename() = '%s'\n", flom_handle_get_trace_filename(my_handle)); /* check trace filename */ if (strcmp(nd_trace_filename, flom_handle_get_trace_filename(my_handle))) { fprintf(stderr, "Unexpected result from flom_handle_set/get_trace_filename\n"); exit(1); } /* get current resource name */ printf("flom_handle_get_resource_name() = '%s'\n", flom_handle_get_resource_name(my_handle)); /* set a new resource name */ if (FLOM_RC_OK != (ret_cod = flom_handle_set_resource_name( my_handle, nd_resource_name))) { fprintf(stderr, "flom_handle_set_resource_name() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* get new resource name */ printf("flom_handle_get_resource_name() = '%s'\n", flom_handle_get_resource_name(my_handle)); /* check resource name */ if (strcmp(nd_resource_name, flom_handle_get_resource_name(my_handle))) { fprintf(stderr, "Unexpected result from flom_handle_set/get_resource_name\n"); exit(1); } /* get current value for resource create property */ printf("flom_handle_get_resource_create() = %d\n", flom_handle_get_resource_create(my_handle)); /* set a new value for resource create property */ flom_handle_set_resource_create(my_handle, FALSE); /* get new value for resource create property */ printf("flom_handle_get_resource_create() = %d\n", flom_handle_get_resource_create(my_handle)); /* check resource create 1/2 */ if (flom_handle_get_resource_create(my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_resource_create\n"); exit(1); } /* set a new value for resource create property */ flom_handle_set_resource_create(my_handle, TRUE); /* get new value for resource create property */ printf("flom_handle_get_resource_create() = %d\n", flom_handle_get_resource_create(my_handle)); /* check resource create 2/2 */ if (!flom_handle_get_resource_create(my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_resource_create\n"); exit(1); } /* get current value for resource timeout property */ printf("flom_handle_get_resource_timeout() = %d\n", flom_handle_get_resource_timeout(my_handle)); /* set a new value for resource timeout property */ flom_handle_set_resource_timeout(my_handle, -1); /* get new value for resource timeout property */ printf("flom_handle_get_resource_timeout() = %d\n", flom_handle_get_resource_timeout(my_handle)); /* check resource timeout */ if (-1 != flom_handle_get_resource_timeout(my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_resource_timeout\n"); exit(1); } /* get current value for resource quantity property */ printf("flom_handle_get_resource_quantity() = %d\n", flom_handle_get_resource_quantity(my_handle)); /* set a new value for resource quantity property */ flom_handle_set_resource_quantity(my_handle, 3); /* get new value for resource quantity property */ printf("flom_handle_get_resource_quantity() = %d\n", flom_handle_get_resource_quantity(my_handle)); /* check resource quantity */ if (3 != flom_handle_get_resource_quantity(my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_resource_quantity\n"); exit(1); } /* get current value for resource lock mode property */ printf("flom_handle_get_lock_mode() = %d\n", flom_handle_get_lock_mode(my_handle)); /* set a new value for resource lock mode property */ flom_handle_set_lock_mode(my_handle, FLOM_LOCK_MODE_PW); /* get new value for resource lock mode property */ printf("flom_handle_get_lock_mode() = %d\n", flom_handle_get_lock_mode(my_handle)); /* check resource lock mode */ if (FLOM_LOCK_MODE_PW != flom_handle_get_lock_mode(my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/get_lock_mode\n"); exit(1); } /* get current value for resource idle lifespan */ printf("flom_handle_get_resource_idle_lifespan() = %d\n", flom_handle_get_resource_idle_lifespan(my_handle)); /* set a new value for resource idle lifespan */ flom_handle_set_resource_idle_lifespan(my_handle, 10000); /* get new value for resource idle lifespan */ printf("flom_handle_get_resource_idle_lifespan() = %d\n", flom_handle_get_resource_idle_lifespan(my_handle)); /* get current unicast address */ printf("flom_handle_get_unicast_address() = '%s'\n", flom_handle_get_unicast_address(my_handle)); /* check resource idle lifespan */ if (10000 != flom_handle_get_resource_idle_lifespan(my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_resource_idle_lifespan\n"); exit(1); } /* set a new unicast_address */ flom_handle_set_unicast_address(my_handle, nd_unicast_address); /* get new unicast address */ printf("flom_handle_get_unicast_address() = '%s'\n", flom_handle_get_unicast_address(my_handle)); /* check unicast address */ if (strcmp(nd_unicast_address, flom_handle_get_unicast_address(my_handle))) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_unicast_address\n"); exit(1); } /* get current multicast address */ printf("flom_handle_get_multicast_address() = '%s'\n", flom_handle_get_multicast_address(my_handle)); /* set a new multicast_address */ flom_handle_set_multicast_address(my_handle, nd_multicast_address); /* get new multicast address */ printf("flom_handle_get_multicast_address() = '%s'\n", flom_handle_get_multicast_address(my_handle)); /* check multicast address */ if (strcmp(nd_multicast_address, flom_handle_get_multicast_address(my_handle))) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_multicast_address\n"); exit(1); } /* get current network interface */ fprintf(stderr, "flom_handle_get_network_interface() = '%s'\n", flom_handle_get_network_interface(my_handle)); /* set a new network interface */ if (FLOM_RC_OK == flom_handle_set_network_interface( my_handle, nd_network_interface)) { /* get new network interface */ fprintf(stderr, "flom_handle_get_network_interface() = '%s'\n", flom_handle_get_network_interface(my_handle)); /* check network interface */ if (strcmp(nd_network_interface, flom_handle_get_network_interface(my_handle))) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_network_interface\n"); exit(1); } } else { fprintf(stderr, "'%s' is not a valid IPv6 network interface for " "this system\n", nd_network_interface); exit(1); } /* set AF_UNIX/PF_LOCAL socket_name again */ if (FLOM_RC_OK != (ret_cod = flom_handle_set_socket_name( my_handle, nd_socket_name))) { fprintf(stderr, "flom_handle_set_socket_name() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* get current value for unicast port */ printf("flom_handle_get_unicast_port() = %d\n", flom_handle_get_unicast_port(my_handle)); /* set a new value for unicast_port */ flom_handle_set_unicast_port(my_handle, 7777); /* get new value for unicast port */ printf("flom_handle_get_unicast_port() = %d\n", flom_handle_get_unicast_port(my_handle)); /* check unicast port */ if (7777 != flom_handle_get_unicast_port(my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/get_unicast_port\n"); exit(1); } /* get current value for multicast port */ printf("flom_handle_get_multicast_port() = %d\n", flom_handle_get_multicast_port(my_handle)); /* set a new value for multicast_port */ flom_handle_set_multicast_port(my_handle, 8888); /* get new value for multicast port */ printf("flom_handle_get_multicast_port() = %d\n", flom_handle_get_multicast_port(my_handle)); /* check multicast port */ if (8888 != flom_handle_get_multicast_port(my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_multicast_port\n"); exit(1); } /* get current value for discovery attempts property */ printf("flom_handle_get_discovery_attempts() = %d\n", flom_handle_get_discovery_attempts(my_handle)); /* set a new value for discovery attempts property */ flom_handle_set_discovery_attempts(my_handle, 5); /* get new value for discovery attempts */ printf("flom_handle_get_discovery_attempts() = %d\n", flom_handle_get_discovery_attempts(my_handle)); /* check discovery attempts */ if (5 != flom_handle_get_discovery_attempts(my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_discovery_attempts\n"); exit(1); } /* get current value for discovery timeout property */ printf("flom_handle_get_discovery_timeout() = %d\n", flom_handle_get_discovery_timeout(my_handle)); /* set a new value for discovery timeout property */ flom_handle_set_discovery_timeout(my_handle, 750); /* get new value for discovery timeout */ printf("flom_handle_get_discovery_timeout() = %d\n", flom_handle_get_discovery_timeout(my_handle)); /* check discovery timeout */ if (750 != flom_handle_get_discovery_timeout(my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_discovery_timeout\n"); exit(1); } /* get current value for discovery ttl property */ printf("flom_handle_get_discovery_ttl() = %d\n", flom_handle_get_discovery_ttl(my_handle)); /* set a new value for discovery ttl property */ flom_handle_set_discovery_ttl(my_handle, 2); /* get new value for discovery ttl */ printf("flom_handle_get_discovery_ttl() = %d\n", flom_handle_get_discovery_ttl(my_handle)); /* check discovery ttl */ if (2 != flom_handle_get_discovery_ttl(my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/get_discovery_ttl\n"); exit(1); } /* get current value for TLS certificate */ fprintf(stderr, "flom_handle_get_tls_certificate() = '%s'\n", flom_handle_get_tls_certificate(my_handle)); /* set a new TLS certificate */ if (FLOM_RC_OK != (ret_cod = flom_handle_set_tls_certificate( my_handle, nd_tls_certificate))) { fprintf(stderr, "flom_handle_set_tls_certificate() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* get new TLS certificate */ fprintf(stderr, "flom_handle_get_tls_certificate() = '%s'\n", flom_handle_get_tls_certificate(my_handle)); /* get current value for TLS private key */ fprintf(stderr, "flom_handle_get_tls_private_key() = '%s'\n", flom_handle_get_tls_private_key(my_handle)); /* set a new TLS private key */ if (FLOM_RC_OK != (ret_cod = flom_handle_set_tls_private_key( my_handle, nd_tls_private_key))) { fprintf(stderr, "flom_handle_set_tls_private_key() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* get new TLS private key */ fprintf(stderr, "flom_handle_get_tls_private_key() = '%s'\n", flom_handle_get_tls_private_key(my_handle)); /* get current value for TLS CA certificate */ fprintf(stderr, "flom_handle_get_tls_ca_certificate() = '%s'\n", flom_handle_get_tls_ca_certificate(my_handle)); /* set a new TLS CA certificate */ if (FLOM_RC_OK != (ret_cod = flom_handle_set_tls_ca_certificate( my_handle, nd_tls_ca_certificate))) { fprintf(stderr, "flom_handle_set_tls_ca_certificate() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* get new TLS CA certificate */ fprintf(stderr, "flom_handle_get_tls_ca_certificate() = '%s'\n", flom_handle_get_tls_ca_certificate(my_handle)); /* get current value for TLS check peer ID property */ printf("flom_handle_get_tls_check_peer_id() = %d\n", flom_handle_get_tls_check_peer_id(my_handle)); /* set a new value for TLS check peer ID property */ flom_handle_set_tls_check_peer_id(my_handle, FALSE); /* get new value for TLS check peer ID property */ printf("flom_handle_get_tls_check_peer_id() = %d\n", flom_handle_get_tls_check_peer_id(my_handle)); /* check TLS check peer ID 1/2 */ if (flom_handle_get_tls_check_peer_id(my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_tls_check_peer_id\n"); exit(1); } /* set a new value for TLS check peer ID property */ flom_handle_set_tls_check_peer_id(my_handle, TRUE); /* get new value for TLS check peer ID property */ printf("flom_handle_get_tls_check_peer_id() = %d\n", flom_handle_get_tls_check_peer_id(my_handle)); /* check TLS check peer ID 2/2 */ if (!flom_handle_get_tls_check_peer_id(my_handle)) { fprintf(stderr, "Unexpected result from flom_handle_set/" "get_tls_check_peer_id\n"); exit(1); } /* lock acquisition */ if (FLOM_RC_OK != (ret_cod = flom_handle_lock(my_handle))) { fprintf(stderr, "flom_handle_lock() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } else if (NULL != flom_handle_get_locked_element(my_handle)) { printf("dynamicHandleHappyPath locked element is %s\n", flom_handle_get_locked_element(my_handle)); } /* lock release */ if (FLOM_RC_OK != (ret_cod = flom_handle_unlock(my_handle))) { fprintf(stderr, "flom_handle_unlock() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* delete the handle */ flom_handle_delete(my_handle); }
/* * Happy path usage with a static handle */ void unlock_different_resource(void) { int ret_cod; flom_handle_t my_handle; /* initialize a new handle */ if (FLOM_RC_OK != (ret_cod = flom_handle_init(&my_handle))) { fprintf(stderr, "flom_handle_init() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* get current resource name */ printf("flom_handle_get_resource_name() = '%s'\n", flom_handle_get_resource_name(&my_handle)); /* set a new resource name */ if (FLOM_RC_OK != (ret_cod = flom_handle_set_resource_name( &my_handle, nd_resource_name1))) { fprintf(stderr, "flom_handle_set_resource_name() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* get new resource name */ printf("flom_handle_get_resource_name() = '%s'\n", flom_handle_get_resource_name(&my_handle)); /* check resource name */ if (strcmp(nd_resource_name1, flom_handle_get_resource_name(&my_handle))) { fprintf(stderr, "Unexpected result from flom_handle_set/get_resource_name\n"); exit(1); } /* lock acquisition */ if (FLOM_RC_OK != (ret_cod = flom_handle_lock(&my_handle))) { fprintf(stderr, "flom_handle_lock() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } else if (NULL != flom_handle_get_locked_element(&my_handle)) { printf("staticHandleHappyPath locked element is %s\n", flom_handle_get_locked_element(&my_handle)); } /* set a different resource name */ if (FLOM_RC_API_IMMUTABLE_HANDLE != ( ret_cod = flom_handle_set_resource_name( &my_handle, nd_resource_name2))) { fprintf(stderr, "flom_handle_set_resource_name() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* get new resource name */ printf("flom_handle_get_resource_name() = '%s'\n", flom_handle_get_resource_name(&my_handle)); /* check resource name: it should not be changed because the resource is already locked */ if (strcmp(nd_resource_name1, flom_handle_get_resource_name(&my_handle))) { fprintf(stderr, "Unexpected result from flom_handle_set/get_resource_name\n"); exit(1); } /* lock release */ if (FLOM_RC_OK != (ret_cod = flom_handle_unlock(&my_handle))) { fprintf(stderr, "flom_handle_unlock() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } /* handle clean-up (memory release) */ if (FLOM_RC_OK != (ret_cod = flom_handle_clean(&my_handle))) { fprintf(stderr, "flom_handle_clean() returned %d, '%s'\n", ret_cod, flom_strerror(ret_cod)); exit(1); } }
int main (int argc, char *argv[]) { GError *error = NULL; GOptionContext *option_context; int child_status = 0; int ret_cod = FLOM_RC_INTERNAL_ERROR; struct flom_conn_data_s cd; char locked_element[100]; option_context = g_option_context_new("[-- command to execute]"); g_option_context_add_main_entries(option_context, entries, NULL); if (!g_option_context_parse(option_context, &argc, &argv, &error)) { g_print("option parsing failed: %s\n", error->message); exit(FLOM_ES_GENERIC_ERROR); } g_option_context_free(option_context); if (print_version) { g_print("FLoM: Free LOck Manager\n" "Copyright (c) 2013-2014, Christian Ferrari; " "all rights reserved.\n" "License: GPL (GNU Public License) version 2\n" "Package name: %s; package version: %s; release date: %s\n" "Access http://sourceforge.net/projects/flom/ for " "project community activities\n", FLOM_PACKAGE_NAME, FLOM_PACKAGE_VERSION, FLOM_PACKAGE_DATE); exit(FLOM_ES_OK); } /* initialize trace destination if necessary */ FLOM_TRACE_INIT; /* initialize regular expression table */ if (FLOM_RC_OK != (ret_cod = global_res_name_preg_init())) { g_print("global_res_name_preg_init: ret_cod=%d\n", ret_cod); exit(FLOM_ES_GENERIC_ERROR); } /* reset global configuration */ flom_config_reset(NULL); /* initialize configuration with standard system, standard user and user customized config files */ if (FLOM_RC_OK != (ret_cod = flom_config_init(NULL, config_file))) { g_print("flom_config_init: ret_cod=%d\n", ret_cod); exit(FLOM_ES_GENERIC_ERROR); } /* overrides configuration with command line passed arguments */ if (NULL != daemon_trace_file) flom_config_set_daemon_trace_file(NULL, daemon_trace_file); if (NULL != command_trace_file) flom_config_set_command_trace_file(NULL, command_trace_file); if (verbose) flom_config_set_verbose(NULL, verbose); if (NULL != resource_name) if (FLOM_RC_OK != (ret_cod = flom_config_set_resource_name( NULL, resource_name))) { g_print("flom_config_set_resource_name: ret_cod=%d\n", ret_cod); exit(FLOM_ES_GENERIC_ERROR); } if (0 > resource_quantity) g_warning("Resource quantity ignored because negative values (%d) " "are meaningless\n", resource_quantity); else if (0 < resource_quantity) flom_config_set_resource_quantity(NULL, resource_quantity); if (NULL != resource_wait) { flom_bool_value_t fbv; if (FLOM_BOOL_INVALID == ( fbv = flom_bool_value_retrieve(resource_wait))) { g_print("resource-wait: '%s' is an invalid value\n", resource_wait); exit(FLOM_ES_GENERIC_ERROR); } flom_config_set_resource_wait(NULL, fbv); } if (FLOM_NETWORK_WAIT_TIMEOUT != resource_timeout) { /* timeout is useless if no wait was specified */ if (FLOM_BOOL_NO == flom_config_get_resource_wait(NULL)) g_warning("Timeout ignored because 'no wait' behavior was " "specified\n"); else flom_config_set_resource_timeout(NULL, resource_timeout); } if (NULL != lock_mode) { flom_lock_mode_t flm; if (FLOM_LOCK_MODE_INVALID == ( flm = flom_lock_mode_retrieve(lock_mode))) { g_print("lock-mode: '%s' is an invalid value\n", lock_mode); exit(FLOM_ES_GENERIC_ERROR); } flom_config_set_lock_mode(NULL, flm); } if (NULL != resource_create) { flom_bool_value_t fbv; if (FLOM_BOOL_INVALID == ( fbv = flom_bool_value_retrieve(resource_create))) { g_print("resource-create: '%s' is an invalid value\n", resource_create); exit(FLOM_ES_GENERIC_ERROR); } flom_config_set_resource_create(NULL, fbv); } flom_config_set_resource_idle_lifespan(NULL, resource_idle_lifespan); if (NULL != socket_name) { if (FLOM_RC_OK != (ret_cod = flom_config_set_socket_name( NULL, socket_name))) { g_print("socket-name: '%s' is an invalid value\n", socket_name); g_print("flom_client_connect: ret_cod=%d (%s)\n", ret_cod, flom_strerror(ret_cod)); exit(FLOM_ES_GENERIC_ERROR); } } if (_DEFAULT_DAEMON_LIFESPAN != daemon_lifespan) { flom_config_set_lifespan(NULL, daemon_lifespan); } if (NULL != unicast_address) { flom_config_set_unicast_address(NULL, unicast_address); } if (_DEFAULT_DAEMON_PORT != unicast_port) { flom_config_set_unicast_port(NULL, unicast_port); } if (NULL != multicast_address) { flom_config_set_multicast_address(NULL, multicast_address); } if (_DEFAULT_DAEMON_PORT != multicast_port) { flom_config_set_multicast_port(NULL, multicast_port); } if (_DEFAULT_DISCOVERY_ATTEMPTS != discovery_attempts) { flom_config_set_discovery_attempts(NULL, discovery_attempts); } if (_DEFAULT_DISCOVERY_TIMEOUT != discovery_timeout) { flom_config_set_discovery_timeout(NULL, discovery_timeout); } if (_DEFAULT_DISCOVERY_TTL != discovery_ttl) { flom_config_set_discovery_ttl(NULL, discovery_ttl); } if (_DEFAULT_TCP_KEEPALIVE_TIME != tcp_keepalive_time) { flom_config_set_tcp_keepalive_time(NULL, tcp_keepalive_time); } if (_DEFAULT_TCP_KEEPALIVE_INTVL != tcp_keepalive_intvl) { flom_config_set_tcp_keepalive_intvl(NULL, tcp_keepalive_intvl); } if (_DEFAULT_TCP_KEEPALIVE_PROBES != tcp_keepalive_probes) { flom_config_set_tcp_keepalive_probes(NULL, tcp_keepalive_probes); } if (NULL != flom_config_get_command_trace_file(NULL)) /* change trace destination if necessary */ FLOM_TRACE_REOPEN(flom_config_get_command_trace_file(NULL)); /* print configuration */ if (flom_config_get_verbose(NULL)) flom_config_print(NULL); /* check configuration */ if (FLOM_RC_OK != (ret_cod = flom_config_check(NULL))) { g_warning("Configuration is not valid, cannot go on!\n"); exit(FLOM_ES_GENERIC_ERROR); } /* check if the command is asking daemon termination */ if (quiesce_exit || immediate_exit) { g_print("Starting FLoM daemon %s shutdown...\n", immediate_exit ? "immediate" : "quiesce"); flom_client_shutdown(NULL, immediate_exit); exit(0); } /* check the command is not null */ if (NULL == command_argv) { g_warning("No command to execute!\n"); exit(FLOM_ES_UNABLE_TO_EXECUTE_COMMAND); } /* open connection to a valid flom lock manager... */ if (FLOM_RC_OK != (ret_cod = flom_client_connect(NULL, &cd, TRUE))) { g_print("flom_client_connect: ret_cod=%d (%s)\n", ret_cod, flom_strerror(ret_cod)); exit(FLOM_ES_GENERIC_ERROR); } /* sending lock command */ ret_cod = flom_client_lock(NULL, &cd, flom_config_get_resource_timeout(NULL), locked_element, sizeof(locked_element)); switch (ret_cod) { case FLOM_RC_OK: /* OK, go on */ if (flom_config_get_verbose(NULL) && '\0' != locked_element[0]) g_print("Locked element is '%s'\n", locked_element); break; case FLOM_RC_LOCK_BUSY: /* busy */ g_print("Resource already locked, the lock cannot be obtained\n"); /* gracefully disconnect from daemon */ if (FLOM_RC_OK != (ret_cod = flom_client_disconnect(&cd))) { g_print("flom_client_unlock: ret_cod=%d (%s)\n", ret_cod, flom_strerror(ret_cod)); } exit(FLOM_ES_RESOURCE_BUSY); break; case FLOM_RC_LOCK_CANT_WAIT: /* can't wait, leaving... */ g_print("The resource could be available in the future, but the " "requester can't wait\n"); /* gracefully disconnect from daemon */ if (FLOM_RC_OK != (ret_cod = flom_client_disconnect(&cd))) { g_print("flom_client_unlock: ret_cod=%d (%s)\n", ret_cod, flom_strerror(ret_cod)); } exit(FLOM_ES_REQUESTER_CANT_WAIT); break; case FLOM_RC_LOCK_IMPOSSIBLE: /* impossible */ g_print("Resource will never satisfy the request, the lock " "cannot be obtained\n"); /* gracefully disconnect from daemon */ if (FLOM_RC_OK != (ret_cod = flom_client_disconnect(&cd))) { g_print("flom_client_unlock: ret_cod=%d (%s)\n", ret_cod, flom_strerror(ret_cod)); } exit(FLOM_ES_GENERIC_ERROR); break; case FLOM_RC_NETWORK_TIMEOUT: /* timeout expired, busy resource */ g_print("The lock was not obtained because timeout " "(%d milliseconds) expired\n", flom_config_get_resource_timeout(NULL)); /* gracefully disconnect from daemon */ if (FLOM_RC_OK != (ret_cod = flom_client_disconnect(&cd))) { g_print("flom_client_unlock: ret_cod=%d (%s)\n", ret_cod, flom_strerror(ret_cod)); } exit(FLOM_ES_RESOURCE_BUSY); break; default: g_print("flom_client_lock: ret_cod=%d (%s)\n", ret_cod, flom_strerror(ret_cod)); exit(FLOM_ES_GENERIC_ERROR); } /* switch (ret_cod) */ /* execute the command */ if (FLOM_RC_OK != (ret_cod = flom_exec(command_argv, locked_element, &child_status))) { guint i, num; g_print("Unable to execute command: '"); num = g_strv_length(command_argv); for (i=0; i<num; ++i) g_print("%s", command_argv[i]); g_print("'\n"); exit(FLOM_ES_UNABLE_TO_EXECUTE_COMMAND); } /* sending unlock command */ if (FLOM_RC_OK != (ret_cod = flom_client_unlock(NULL, &cd))) { g_print("flom_client_unlock: ret_cod=%d (%s)\n", ret_cod, flom_strerror(ret_cod)); exit(FLOM_ES_GENERIC_ERROR); } /* gracefully disconnect from daemon */ if (FLOM_RC_OK != (ret_cod = flom_client_disconnect(&cd))) { g_print("flom_client_unlock: ret_cod=%d (%s)\n", ret_cod, flom_strerror(ret_cod)); } g_strfreev (command_argv); command_argv = NULL; /* release config data */ flom_config_free(NULL); /* release regular expression data */ global_res_name_preg_free(); return child_status; }