### Driver Initialization Example Source: https://www.smartos.org/man/9f/ddi_soft_state_init Example of a driver's _init function, demonstrating soft state initialization and module installation. ```c int _init(void) { int error; error = ddi_soft_state_init(&statep, sizeof (devstate_t), 0); if (error != 0) return (error); if ((error = mod_install(&modlinkage)) != 0) ddi_soft_state_fini(&statep); return (error); } ``` -------------------------------- ### Example Usage of Resolver Start Callback Source: https://www.smartos.org/man/3/CURLOPT_RESOLVER_START_FUNCTION Provides a complete example demonstrating the implementation and usage of a resolver start callback function. The callback logs the received resolver state and user data, returning 0 for success. ```c static int start_cb(void *resolver_state, void *reserved, void *userdata) { (void)reserved; printf("Received resolver_state=%p userdata=%p\n", resolver_state, userdata); return 0; } int main(void) { CURL *curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_RESOLVER_START_FUNCTION, start_cb); curl_easy_setopt(curl, CURLOPT_RESOLVER_START_DATA, curl); curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); curl_easy_perform(curl); curl_easy_cleanup(curl); } } ``` -------------------------------- ### Install Floating-Point Disabled Trap Handler Source: https://www.smartos.org/man/2/__sparc_utrap_install This example demonstrates how to install a custom trap handler for the UT_FP_DISABLED exception using sparc_utrap_install. It shows the setup for the handler and the call to the installation function. ```c extern void *fpdis_trap_handler(); utrap_handler_t new_precise = (utrap_handler_t)fpdis_trap_handler; double d; int err; err = __sparc_utrap_install(UT_FP_DISABLED, new_precise, UTH_NOCHANGE, NULL, NULL); if (err == EINVAL) { /* unexpected error, do something */ exit (1); } ``` -------------------------------- ### Simple Boot Menu Example Source: https://www.smartos.org/man/7/menu.4th This example demonstrates how to initialize and display a basic boot menu with two options: 'Boot' and a toggleable 'Option'. It also configures a reboot option and a default boot command. ```forth include /boot/forth/menu.4th menu-init set menu_caption[1]="Boot" set menu_command[1]="boot" set menu_options=2 set menu_caption[2]="Option: NO" set toggled_text[2]="Option: YES" set menu_command[2]="toggle_menuitem" set menu_timeout_command="boot" set menu_reboot menu-display ``` -------------------------------- ### Basic installf Usage Example Source: https://www.smartos.org/man/8/installf Demonstrates the basic usage of the installf command for creating directories, devices, and links during package installation. This is typically used within pre-install or post-install scripts. ```shell # create /dev/xt directory # (needs to be done before drvinstall) installf $PKGINST /dev/xt d 755 root sys || exit 2 majno=`/usr/sbin/drvinstall **-m** /etc/master.d/xt **-d** $BASEDIR/data/xt.o **-v** 1.0` || exit 2 i=00 while [ $i -lt $limit ] do for j in 0 1 2 3 4 5 6 7 do echo /dev/xt$i$j c $majno `expr $i ? 8 + $j` 644 root sys | echo /dev/xt$i$j=/dev/xt/$i$j done i=`expr $i + 1` [ $i -le 9 ] && i="0$i" #add leading zero done | installf $PKGINST - || exit 2 # finalized installation, create links installf **-f** $PKGINST || exit 2 ``` -------------------------------- ### Example read() routine using physio() Source: https://www.smartos.org/man/9E/read This example demonstrates how to implement a read() routine for a non-seekable device using the physio() function. It includes necessary setup like getting the device instance and checking for soft state. ```c static int xxread(dev_t dev, struct uio *uiop, cred_t *credp) { int rval; offset_t off; int instance; xx_t xx; instance = getminor(dev); xx = ddi_get_soft_state(xxstate, instance); if (xx == NULL) return (ENXIO); off = uiop->uio_loffset; rval = physio(xxstrategy, NULL, dev, B_READ, xxmin, uiop); uiop->uio_loffset = off; return (rval); } ``` -------------------------------- ### Connect and Traverse Services and Instances Source: https://www.smartos.org/man/3scf/scf_iter_service_pgs This example shows how to connect to the repository, iterate over all services and their instances, and print their FMRIs. Proper initialization and cleanup of SCF handles and iterators are crucial. ```c scf_handle_t *handle = scf_handle_create(SCF_VERSION); scf_scope_t *scope = scf_scope_create(handle); scf_service_t *svc = scf_service_create(handle); scf_instance_t *inst = scf_instance_create(handle); scf_iter_t *svc_iter = scf_iter_create(handle); scf_iter_t *inst_iter = scf_iter_create(handle); size_t sz = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH) + 1; char *fmri = malloc(sz + 1); int r; if (handle == NULL || scope == NULL || svc == NULL || inst == NULL || svc_iter == NULL || inst_iter == NULL || fMRI == NULL) { /* failure */ } if (scf_handle_bind(handle) == -1 || scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) == -1 || scf_iter_scope_services(svc_iter, scope) == -1) { /* failure */ } while ((r = scf_iter_next_service(svc_iter, svc)) > 0) { if (scf_service_to_fmri(svc, fmri, sz) < 0) { /* failure */ } puts(fmri); if (scf_iter_service_instances(inst_iter, svc) < 0) { /* failure */ } while ((r = scf_iter_next_instance(inst_iter, inst)) > 0) { if (scf_instance_to_fmri(inst, fmri, sz) < 0) { /* failure */ } puts(fmri); } if (r < 0) break; } if (r < 0) { /* failure */ } scf_handle_destroy(handle); scf_scope_destroy(scope); scf_service_destroy(svc); scf_instance_destroy(inst); scf_iter_destroy(svc_iter); scf_iter_destroy(inst_iter); ``` -------------------------------- ### Get URL Scheme with CURLINFO_PROTOCOL Source: https://www.smartos.org/man/3/CURLINFO_PROTOCOL Demonstrates how to use curl_easy_getinfo with CURLINFO_PROTOCOL to retrieve the scheme of a completed URL transfer. This example shows basic setup, performing a request, and then querying for the protocol. ```c #include int main(void) { CURL *curl = curl_easy_init(); if(curl) { CURLcode result; curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); result = curl_easy_perform(curl); if(result == CURLE_OK) { long scheme; curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &scheme); } curl_easy_cleanup(curl); } } ``` -------------------------------- ### Get Installed libcurl Version Source: https://www.smartos.org/man/1/curl-config Displays the version information of the installed libcurl library. ```bash curl-config --version ``` -------------------------------- ### Read Custom Configuration and Boot Source: https://www.smartos.org/man/7/loader.4th This example demonstrates how to unload existing configurations, read a specific configuration file, and then boot the system using the newly loaded configuration. This is useful for applying custom settings before booting. ```forth unload read-conf /boot/special.conf boot-conf ``` -------------------------------- ### Driver Entry Point Example Source: https://www.smartos.org/man/9f/ddi_soft_state_fini Example demonstrating the initialization and finalization of driver soft state within the _init and _fini entry points. ```c typedef struct { volatile caddr_t *csr; /* device registers */ kmutex_t csr_mutex; /* protects 'csr' field */ unsigned int state; dev_info_t *dip; /* back pointer to devinfo */ } devstate_t; static void *statep; int _init(void) { int error; error = ddi_soft_state_init(&statep, sizeof (devstate_t), 0); if (error != 0) return (error); if ((error = mod_install(&modlinkage)) != 0) ddi_soft_state_fini(&statep); return (error); } int _fini(void) { int error; if ((error = mod_remove(&modlinkage)) != 0) return (error); ddi_soft_state_fini(&statep); return (0); } ``` -------------------------------- ### Resolver Start Callback and Data Example Source: https://www.smartos.org/man/3/CURLOPT_RESOLVER_START_DATA This example demonstrates how to set a custom resolver start callback function and pass user data to it using CURLOPT_RESOLVER_START_FUNCTION and CURLOPT_RESOLVER_START_DATA. The callback receives the user data as its third argument. ```c #include #include static int resolver_start_cb(void *resolver_state, void *reserved, void *userdata) { (void)reserved; printf("Received resolver_state=%p userdata=%p\n", resolver_state, userdata); return 0; } int main(void) { CURL *curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_RESOLVER_START_FUNCTION, resolver_start_cb); curl_easy_setopt(curl, CURLOPT_RESOLVER_START_DATA, curl); curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); curl_easy_perform(curl); curl_easy_cleanup(curl); } } ``` -------------------------------- ### Connect and Iterate Services and Instances Source: https://www.smartos.org/man/3SCF/scf_iter_snaplevel_pgs This example demonstrates connecting to the repository, iterating through all services, and then iterating through all instances for each service, printing their FMRIs. It includes resource allocation and cleanup. ```c scf_handle_t *handle = scf_handle_create(SCF_VERSION); scf_scope_t *scope = scf_scope_create(handle); scf_service_t *svc = scf_service_create(handle); scf_instance_t *inst = scf_instance_create(handle); scf_iter_t *svc_iter = scf_iter_create(handle); scf_iter_t *inst_iter = scf_iter_create(handle); size_t sz = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH) + 1; char *fmri = malloc(sz + 1); int r; if (handle == NULL || scope == NULL || svc == NULL || inst == NULL || svc_iter == NULL || inst_iter == NULL || fmi == NULL) { /* failure */ } if (scf_handle_bind(handle) == -1 || scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) == -1 || scf_iter_scope_services(svc_iter, scope) == -1) { /* failure */ } while ((r = scf_iter_next_service(svc_iter, svc)) > 0) { if (scf_service_to_fmri(svc, fmri, sz) < 0) { /* failure */ } puts(fmri); if (scf_iter_service_instances(inst_iter, svc) < 0) { /* failure */ } while ((r = scf_iter_next_instance(inst_iter, inst)) > 0) { if (scf_instance_to_fmri(inst, fmri, sz) < 0) { /* failure */ } puts(fmri); } if (r < 0) break; } if (r < 0) { /* failure */ } scf_handle_destroy(handle); scf_scope_destroy(scope); scf_service_destroy(svc); scf_instance_destroy(inst); scf_iter_destroy(svc_iter); scf_iter_destroy(inst_iter); ``` -------------------------------- ### Sample bootparams Entry for JumpStart Installation Source: https://www.smartos.org/man/5/bootparams This entry is designed for JumpStart installations of diskless clients without displays. It includes parameters for the installation source, boot type, and network configuration. ```shell mozart root=haydn:/export/install/sparc/os/latest/Solaris_9/boot \ install=haydn:/export/install/sparc/os/8.1/latest boottype=:in \ install_config=haydn:/usr/local/share/lib/jump-net \ ns=otis:nisplus(255.255.255.0) term=:xterms domain=eu.cte.example.com ``` -------------------------------- ### Create Door and Attach to Filesystem Source: https://www.smartos.org/man/3C/door_create This example demonstrates creating a door using door_create() and then advertising it in the filesystem namespace using fattach(). It includes setup for the filesystem path. ```c int main(int argc, char *argv[]) { int did; struct stat buf; if ((did = door_create(server, 0, 0)) < 0) { perror("door_create"); exit(1); } /* make sure file system location exists */ if (stat("/tmp/door", &buf) < 0) { int newfd; if ((newfd = creat("/tmp/door", 0444)) < 0) { perror("creat"); exit(1); } (void) close(newfd); } /* make sure nothing else is attached */ (void) fdetach("/tmp/door"); /* attach to file system */ if (fattach(did, "/tmp/door") < 0) { perror("fattach"); exit(2); } [...] } ``` -------------------------------- ### Register Map Setup in Driver Attach Source: https://www.smartos.org/man/9F/ddi_check_dma_handle This example shows the setup of a single register-access handle during the driver attachment process. It maps registers and returns failure if the setup is unsuccessful. ```c status = ddi_regs_map_setup(dip, REGSET_ZERO, ®addr, 0, 0, , &acc_attrs, &acc_hdl); if (status != DDI_SUCCESS) return (DDI_FAILURE); ``` -------------------------------- ### SASL Server Start Compilation Example Source: https://www.smartos.org/man/3SASL/sasl_server_start Example of how to compile a C program that uses the SASL library, linking against the sasl library. ```bash cc[ _flag_ ... ] _file_ ... -lsasl[ _library_ ... ] ``` -------------------------------- ### Create a Door and Attach to File System Source: https://www.smartos.org/man/3c/door_create This example demonstrates creating a door and then using fattach() to advertise it in the file system namespace. It includes setup for the file system location and ensures no other process is attached. ```c void server(void *cookie, char *argp, size_t arg_size, door_desc_t *dp, uint_t n_desc) { door_return(NULL, 0, NULL, 0); /* NOTREACHED */ } int main(int argc, char *argv[]) { int did; struct stat buf; if ((did = door_create(server, 0, 0)) < 0) { perror("door_create"); exit(1); } /* make sure file system location exists */ if (stat("/tmp/door", &buf) < 0) { int newfd; if ((newfd = creat("/tmp/door", 0444)) < 0) { perror("creat"); exit(1); } (void) close(newfd); } /* make sure nothing else is attached */ (void) fdetach("/tmp/door"); /* attach to file system */ if (fattach(did, "/tmp/door") < 0) { perror("fattach"); exit(2); } [...] } ``` -------------------------------- ### Get Home Lgroup for a Specific Thread Source: https://www.smartos.org/man/3lgrp/lgrp_home Example of calling lgrp_home to get the home lgroup for a specific LWP ID (thread). ```c lwpid_t target_lwpid = 67890; lgrp_id_t home_lgrp; home_lgrp = lgrp_home(P_LWPID, target_lwpid); if (home_lgrp == -1) { /* Handle error */ } ``` -------------------------------- ### Example Static Routes Configuration Source: https://www.smartos.org/man/8/vmadm Demonstrates how to configure static routes for a VM using IP addresses and link-local routes via NICs or MAC addresses. ```json { "10.2.2.0/24": "10.2.1.1", "10.3.0.1": "nics[1]", "10.4.0.1": "macs[aa:bb:cc:12:34:56]" } ``` -------------------------------- ### Get Home Lgroup for a Specific Process Source: https://www.smartos.org/man/3lgrp/lgrp_home Example of calling lgrp_home to get the home lgroup for a specific process ID (PID). ```c pid_t target_pid = 12345; lgrp_id_t home_lgrp; home_lgrp = lgrp_home(P_PID, target_pid); if (home_lgrp == -1) { /* Handle error */ } ``` -------------------------------- ### Connect and Traverse Services and Instances Source: https://www.smartos.org/man/3SCF/scf_iter_next_scope This example demonstrates connecting to the SCF repository, iterating over all services, and then iterating over the instances of each service. It prints the FMRIs of both services and instances. Proper initialization and cleanup of SCF objects are crucial. ```c scf_handle_t *handle = scf_handle_create(SCF_VERSION); scf_scope_t *scope = scf_scope_create(handle); scf_service_t *svc = scf_service_create(handle); scf_instance_t *inst = scf_instance_create(handle); scf_iter_t *svc_iter = scf_iter_create(handle); scf_iter_t *inst_iter = scf_iter_create(handle); size_t sz = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH) + 1; char *fmri = malloc(sz + 1); int r; if (handle == NULL || scope == NULL || svc == NULL || inst == NULL || svc_iter == NULL || inst_iter == NULL || fmri == NULL) { /* failure */ } if (scf_handle_bind(handle) == -1 || scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) == -1 || scf_iter_scope_services(svc_iter, scope) == -1) { /* failure */ } while ((r = scf_iter_next_service(svc_iter, svc)) > 0) { if (scf_service_to_fmri(svc, fmri, sz) < 0) { /* failure */ } puts(fmri); if (scf_iter_service_instances(inst_iter, svc) < 0) { /* failure */ } while ((r = scf_iter_next_instance(inst_iter, inst)) > 0) { if (scf_instance_to_fmri(inst, fmri, sz) < 0) { /* failure */ } puts(fmri); } if (r < 0) break; } if (r < 0) { /* failure */ } scf_handle_destroy(handle); scf_scope_destroy(scope); scf_service_destroy(svc); scf_instance_destroy(inst); scf_iter_destroy(svc_iter); scf_iter_destroy(inst_iter); ``` -------------------------------- ### Get Home Lgroup for Current Process/Thread Source: https://www.smartos.org/man/3lgrp/lgrp_home Example of calling lgrp_home to get the home lgroup for the current process or thread using P_MYID. ```c lgrp_id_t home_lgrp; home_lgrp = lgrp_home(P_MYID, P_MYID); if (home_lgrp == -1) { /* Handle error */ } ``` -------------------------------- ### Example: Get Number of Processors Configured Source: https://www.smartos.org/man/3c/sysconf This snippet demonstrates how to use sysconf() to get the number of processors configured on the system using the _SC_NPROCESSORS_CONF constant. ```c #include #include int main() { long nprocs = sysconf(_SC_NPROCESSORS_CONF); if (nprocs == -1) { perror("sysconf _SC_NPROCESSORS_CONF"); } else { printf("Number of processors configured: %ld\n", nprocs); } return 0; } ``` -------------------------------- ### Loadable Module Entry Points and Mutex Handling Source: https://www.smartos.org/man/9e/_info This example demonstrates the _init, _info, and _fini entry points for a loadable module. It includes initializing and destroying a global mutex, ensuring proper resource management during module installation and uninstallation. ```c #include #include #include static struct dev_ops drv_ops; /* * Module linkage information for the kernel. */ static struct modldrv modldrv = { &mod_driverops, /* Type of module. This one is a driver */ "Sample Driver", &drv_ops /* driver ops */ }; static struct modlinkage modlinkage = { MODREV_1, &modldrv, NULL }; /* * Global driver mutex */ static kmutex_t xx_global_mutex; int _init(void) { int i; /* * Initialize global mutex before mod_install'ing driver. * If mod_install() fails, must clean up mutex initialization */ mutex_init(&xx_global_global_mutex, NULL, MUTEX_DRIVER, (void *)NULL); if ((i = mod_install(&modlinkage)) != 0) { mutex_destroy(&xx_global_mutex); } return (i); } int _info(struct modinfo *modinfop) { return (mod_info(&modlinkage, modinfop)); } int _fini(void) { int i; /* * If mod_remove() is successful, we destroy our global mutex */ if ((i = mod_remove(&modlinkage)) == 0) { mutex_destroy(&xx_global_mutex); } return (i); } ``` -------------------------------- ### pppoed Configuration File Example Source: https://www.smartos.org/man/8/pppoed This example shows how to configure specific services like 'internet' and 'debugging' in the /etc/ppp/pppoe file, associating them with devices and pppd options. ```shell service internet device $DEV pppd "proxyarp 192.168.1.1:" service debugging device hme0,$DEV pppd "debug proxyarp 192.168.1.1:" ``` -------------------------------- ### Get Next Client Path Node Source: https://www.smartos.org/man/3DEVINFO/di_path_client_next_path Iterate through all path nodes associated with a client device node. Start with DI_PATH_NIL to get the first path node. ```c #include di_path_t next_path = DI_PATH_NIL; while ((next_path = di_path_client_next_path(node, next_path)) != DI_PATH_NIL) { // Process the path node } ``` -------------------------------- ### Get Pretransfer Time Source: https://www.smartos.org/man/3/CURLINFO_PRETRANSFER_TIME_T Demonstrates how to use curl_easy_getinfo with CURLINFO_PRETRANSFER_TIME_T to get the time in microseconds from the start until the file transfer is about to begin. This includes all pre-transfer commands and negotiations. ```c #include int main(void) { CURL *curl = curl_easy_init(); if(curl) { CURLcode result; curl_off_t pretransfer; curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/"); result = curl_easy_perform(curl); if(CURLE_OK == result) { result = curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME_T, &pretransfer); if(CURLE_OK == result) { printf("Time: %" CURL_FORMAT_CURL_OFF_T ".%06ld\n", pretransfer / 1000000, (long)(pretransfer % 1000000)); } } /* always cleanup */ curl_easy_cleanup(curl); } } ``` -------------------------------- ### Setupterm Initialization Logic Source: https://www.smartos.org/man/3xcurses/set_curterm Demonstrates how setupterm initializes terminal variables like lines and columns, respecting environment variables or database values based on use_env settings. ```c /* The setupterm() function loads terminal-dependent variables for the terminfo layer of X/Open Curses. The setupterm() function initializes the terminfo variables lines and columns such that if use_env(FALSE) has been called, the terminfo values assigned in the database are used regardless of the environmental variables LINES and COLUMNS or the program's window dimensions; when use_env(TRUE) has been called, which is the default, the environment variables LINES and COLUMNS are used, if they exist. If the environment variables do not exist and the program is running in a window, the current window size is used. */ ``` -------------------------------- ### Install ON Build from Directory Source: https://www.smartos.org/man/1onbld/onu This command installs an ON build from a specified local directory into a new target Boot Environment. Ensure the directory path is correct and contains the 'repo.redist' subdirectory. ```bash # onu -d /path/to/my/ws/packages/`uname -p`/nightly -t new-nightly-be ``` -------------------------------- ### Get Installed Image Information Source: https://www.smartos.org/man/8/imgadm Retrieves detailed information (in JSON format) for a specific locally installed image. Can recursively gather child snapshots and clones. ```bash imgadm get [-P ] ``` ```bash imgadm get -r ``` ```bash imgadm get -P zones ``` -------------------------------- ### Example Signal Handler Setup Source: https://www.smartos.org/man/3C/sigfpe Demonstrates how to set up a user-specified signal handler for SIGFPE codes and restore the old handlers. ```APIDOC ## Example: User-Specified Signal Handler ### Description This example shows how to define a custom signal handler function (`sample_handler`) and then use `sigfpe()` to associate it with specific floating-point exceptions (overflow and invalid operand), and also demonstrates how to restore the original handlers. ### Code Example #### Signal Handler Function (`sample_handler`) ```c #include #include #include #include #include void sample_handler(int sig, siginfo_t *sip, ucontext_t *uap) { char *label; switch (sip->si_code) { case FPE_FLTINV: label = "invalid operand"; break; case FPE_FLTRES: label = "inexact"; break; case FPE_FLTDIV: label = "division-by-zero"; break; case FPE_FLTUND: label = "underflow"; break; case FPE_FLTOVF: label = "overflow"; break; default: label = "???"; break; } fprintf(stderr, "FP exception %s (0x%x) occurred at address %p.\n", label, sip->si_code, (void *) sip->si_addr); abort(); } ``` #### Setting Up and Restoring Handlers (`main` function) ```c #include #include #include #include #include // Assume sample_handler is defined elsewhere or above extern void sample_handler(int, siginfo_t *, ucontext_t *); int main(void) { sigfpe_handler_type hdl, old_handler1, old_handler2; // Set the new fp_overflow handler to sample_handler() hdl = (sigfpe_handler_type) sample_handler; old_handler1 = sigfpe(FPE_FLTOVF, hdl); // Set the new fp_invalid handler to SIGFPE_ABORT (abort on invalid) old_handler2 = sigfpe(FPE_FLTINV, SIGFPE_ABORT); // ... perform floating-point operations that might cause exceptions ... // Restore old fp_overflow and fp_invalid handlers sigfpe(FPE_FLTOVF, old_handler1); sigfpe(FPE_FLTINV, old_handler2); return 0; } ``` ``` -------------------------------- ### Get Help for Dyn Module and Dyn:Runpath Command Source: https://www.smartos.org/man/1/elfedit Examples of using the help command to get information on the 'dyn' module and the 'dyn:runpath' command. ```shell > help dyn > help dyn:runpath ``` -------------------------------- ### Get SO_PROTOCOL Source: https://www.smartos.org/man/3socket/getsockopt Example of retrieving the protocol used by the socket with getsockopt() and SO_PROTOCOL. ```c int protocol; int optlen = sizeof(protocol); getsockopt(s, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen); ``` -------------------------------- ### Connect and Iterate Through Services and Instances Source: https://www.smartos.org/man/3scf/scf_iter_next_scope This example demonstrates connecting to the SCF repository, iterating through all services, and then iterating through the instances of each service. It prints the FMRIs of both services and instances. Ensure all created SCF objects are destroyed. ```c scf_handle_t *handle = scf_handle_create(SCF_VERSION); scf_scope_t *scope = scf_scope_create(handle); scf_service_t *svc = scf_service_create(handle); scf_instance_t *inst = scf_instance_create(handle); scf_iter_t *svc_iter = scf_iter_create(handle); scf_iter_t *inst_iter = scf_iter_create(handle); size_t sz = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH) + 1; char *fmri = malloc(sz + 1); int r; if (handle == NULL || scope == NULL || svc == NULL || inst == NULL || svc_iter == NULL || inst_iter == NULL || fmri == NULL) { /* failure */ } if (scf_handle_bind(handle) == -1 || scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) == -1 || scf_iter_scope_services(svc_iter, scope) == -1) { /* failure */ } while ((r = scf_iter_next_service(svc_iter, svc)) > 0) { if (scf_service_to_fmri(svc, fmri, sz) < 0) { /* failure */ } puts(fmri); if (scf_iter_service_instances(inst_iter, svc) < 0) { /* failure */ } while ((r = scf_iter_next_instance(inst_iter, inst)) > 0) { if (scf_instance_to_fmri(inst, fmri, sz) < 0) { /* failure */ } puts(fmri); } if (r < 0) break; } if (r < 0) { /* failure */ } scf_handle_destroy(handle); scf_scope_destroy(scope); scf_service_destroy(svc); scf_instance_destroy(inst); scf_iter_destroy(svc_iter); scf_iter_destroy(inst_iter); ``` -------------------------------- ### Example mpapi.conf File Source: https://www.smartos.org/man/5/mpapi.conf This example shows the format for entries in the /etc/mpapi.conf file, mapping library IDs to their file paths. Do not manually edit this file. ```text # This file contains names and references to MP API plugin libraries # # Do NOT manually edit this file # # Format: # # # com.sun.mpapi32 /lib/libmpscsi_vhci.so com.sun.mpapi64 /lib/64/libmpscsi_vhci.so ``` -------------------------------- ### Get SO_DOMAIN Source: https://www.smartos.org/man/3socket/getsockopt Example of retrieving the domain used by the socket with getsockopt() and SO_DOMAIN. ```c int domain; int optlen = sizeof(domain); getsockopt(s, SOL_SOCKET, SO_DOMAIN, &domain, &optlen); ``` -------------------------------- ### Get SO_TYPE Source: https://www.smartos.org/man/3socket/getsockopt Example of retrieving the socket type using getsockopt() with SO_TYPE. ```c int sock_type; int optlen = sizeof(sock_type); getsockopt(s, SOL_SOCKET, SO_TYPE, &sock_type, &optlen); ``` -------------------------------- ### Example pam.conf Configuration Source: https://www.smartos.org/man/5/pam.conf This example demonstrates a pam.conf file supporting authentication, account, session, and password management modules. It shows how to configure services like 'login' and uses the 'other' keyword for default configurations. ```shell login auth requisite pam_authtok_get.so.1 login auth required pam_dhkeys.so.1 login auth required pam_unix_auth.so.1 login auth required pam_dial_auth.so.1 other account requisite pam_roles.so.1 other account required pam_unix_account.so.1 other session required pam_unix_session.so.1 other password required pam_dhkeys.so.1 other password requisite pam_authtok_get.so.1 other password requisite pam_authtok_check.so.1 other password required pam_authtok_store.so.1 ``` -------------------------------- ### Full Example: Setting up and Performing Transfer with Trailers Source: https://www.smartos.org/man/3/CURLOPT_TRAILERFUNCTION A complete example demonstrating how to initialize a CURL handle, set URL, upload options, a read function, HTTP headers, and the trailer callback function before performing the transfer. ```c extern size_t read_cb(char *ptr, size_t size, size_t nmemb, void *userdata); static int trailer_cb(struct curl_slist **tr, void *data) { /* libcurl frees the list */ *tr = curl_slist_append(*tr, "My-super-awesome-trailer: trailer-stuff"); return CURL_TRAILERFUNC_OK; } int main(void) { CURL *curl = curl_easy_init(); if(curl) { CURLcode result; /* Set the URL of the request */ curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/"); /* Now set it as a put */ curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); /* Assuming we have a function that returns the data to be pushed Let that function be read_cb */ curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_cb); struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Trailer: My-super-awesome-trailer"); result = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); /* Set the trailers filling callback */ curl_easy_setopt(curl, CURLOPT_TRAILERFUNCTION, trailer_cb); /* Perform the transfer */ result = curl_easy_perform(curl); curl_easy_cleanup(curl); curl_slist_free_all(headers); } } ``` -------------------------------- ### Get Next pHCI Path Node Source: https://www.smartos.org/man/3DEVINFO/di_path_phci_next_path Use di_path_phci_next_path to iterate through all path nodes associated with a pHCI device. Start with DI_PATH_NIL to get the first path node. ```c #include di_path_t next_path; // Assuming 'node' is a valid di_node_t for a pHCI device next_path = di_path_phci_next_path(node, DI_PATH_NIL); while (next_path != DI_PATH_NIL) { // Process the path node // ... next_path = di_path_phci_next_path(node, next_path); } ``` -------------------------------- ### Get Next Client Path Node Source: https://www.smartos.org/man/3DEVINFO/di_path_phci_next_path Use di_path_client_next_path to iterate through all path nodes associated with a client device. Start with DI_PATH_NIL to get the first path node. ```c #include di_path_t next_path; // Assuming 'node' is a valid di_node_t for a client device next_path = di_path_client_next_path(node, DI_PATH_NIL); while (next_path != DI_PATH_NIL) { // Process the path node // ... next_path = di_path_client_next_path(node, next_path); } ``` -------------------------------- ### TAILQ_FOREACH_FROM Macro Example Source: https://www.smartos.org/man/3head/TAILQ_FOREACH_FROM This macro iterates through a tail queue starting from a specified element. It requires the queue head, the type of the elements, and the starting element's name. ```c #define TAILQ_FOREACH_FROM(var, head, field) \ for ((var) = TAILQ_FIRST((head)); \ (var); \ (var) = TAILQ_NEXT((var), field)) ``` -------------------------------- ### Driver Open Example Source: https://www.smartos.org/man/9f/ddi_soft_state_fini Example showing how to retrieve soft state when a device is opened. ```c static int xxopen(dev_t *devp, int flag, int otyp, cred_t *cred_p) { devstate_t *softc; int instance; instance = getminor(*devp); if ((softc = ddi_get_soft_state(statep, instance)) == NULL) return (ENXIO); ... softc->state |= XX_IN_USE; ... return (0); } ``` -------------------------------- ### Setup Terminal with Defaults Source: https://www.smartos.org/man/3curses/mvcur This is the simplest call to setupterm, using all defaults and sending output to stdout. It initializes the terminfo database and terminal-dependent variables. ```c setupterm((char *)0, 1, (int *)0); ``` -------------------------------- ### C: Get string from specified window Source: https://www.smartos.org/man/3curses/curs_inwstr Use this function to get a string of wchar_t characters from a specified curses window starting at the cursor position. Attributes are stripped from the characters. ```c #include int winwstr(WINDOW *_win_, wchar_t *_wstr_); ``` -------------------------------- ### Create a New Zone with Filesystems and Network Interfaces Source: https://www.smartos.org/man/8/zonecfg This snippet demonstrates creating a new zone, setting its path, autoboot property, configuring loopback and UFS file systems, adding multiple network interfaces, setting CPU shares, and capped memory limits. ```shell example# **zonecfg -z myzone3** my-zone3: No such zone configured Use 'create' to begin configuring a new zone. zonecfg:myzone3> **create** zonecfg:myzone3> **set zonepath=/export/home/my-zone3** zonecfg:myzone3> **set autoboot=true** zonecfg:myzone3> **add fs** zonecfg:myzone3:fs> **set dir=/usr/local** zonecfg:myzone3:fs> **set special=/opt/local** zonecfg:myzone3:fs> **set type=lofs** zonecfg:myzone3:fs> **add options [ro,nodevices]** zonecfg:myzone3:fs> **end** zonecfg:myzone3> **add fs** zonecfg:myzone3:fs> **set dir=/mnt** zonecfg:myzone3:fs> **set special=/dev/dsk/c0t0d0s7** zonecfg:myzone3:fs> **set raw=/dev/rdsk/c0t0d0s7** zonecfg:myzone3:fs> **set type=ufs** zonecfg:myzone3:fs> **end** zonecfg:myzone3> **add net** zonecfg:myzone3:net> **set address=192.168.0.1/24** zonecfg:myzone3:net> **set physical=eri0** zonecfg:myzone3:net> **end** zonecfg:myzone3> **add net** zonecfg:myzone3:net> **set address=192.168.1.2/24** zonecfg:myzone3:net> **set physical=eri0** zonecfg:myzone3:net> **end** zonecfg:myzone3> **add net** zonecfg:myzone3:net> **set address=192.168.2.3/24** zonecfg:myzone3:net> **set physical=eri0** zonecfg:myzone3:net> **end** zonecfg:my-zone3> **set cpu-shares=5** zonecfg:myzone3> **add capped-memory** zonecfg:myzone3:capped-memory> **set physical=50m** zonecfg:myzone3:capped-memory> **set swap=100m** zonecfg:myzone3:capped-memory> **end** zonecfg:myzone3> **exit** ``` -------------------------------- ### C: Get string from current window Source: https://www.smartos.org/man/3curses/curs_inwstr Use this function to get a string of wchar_t characters from the current curses window starting at the cursor position. Attributes are stripped from the characters. ```c #include int inwstr(wchar_t *_wstr_); ``` -------------------------------- ### Devmap() Entry Point Implementation Source: https://www.smartos.org/man/9E/devmap Example implementation of the devmap() entry point for mapping device or kernel memory. It handles setup for both scenarios and requires appropriate callbacks for device memory. ```c #define MAPPING_SIZE 0x2000 /* size of the mapping */ #define MAPPING_START 0x70000000 /* logical offset at beginning of the mapping */ static struct devmap_callback_ctl xxmap_ops = { DEVMAP_OPS_REV, /* devmap_ops version number */ xxmap_map, /* devmap_ops map routine */ xxmap_access, /* devmap_ops access routine */ xxmap_dup, /* devmap_ops dup routine */ xxmap_unmap, /* devmap_ops unmap routine */ }; static int xxdevmap(dev_t dev, devmap_cookie_t dhp, offset_t off, size_t len, size_t *maplen, uint_t model) { int instance; struct xxstate *xsp; struct ddi_device_acc_attr *endian_attr; struct devmap_callback_ctl *callbackops = NULL; ddi_umem_cookie_t cookie; dev_info_t *dip; offset_t roff; offset_t koff; uint_t rnumber; uint_t maxprot; uint_t flags = 0; size_t length; int err; /* get device soft state */ instance = getminor(dev); xsp = ddi_get_soft_state(statep, instance); if (xsp == NULL) return (-1); dip = xsp->dip; /* check for a valid offset */ if ( _off_ _is_ _invalid_ ) return (-1); /* check if len is within the range of contiguous memory */ if ( **(**_off_ **+**_len_**)**_is_ _contiguous_**.)** length = len; else length = MAPPING_START + MAPPING_SIZE - off; /* device access attributes */ endian_attr = xsp->endian_attr; if ( _off_ _is_ _referring_ _to_ _a_ _device_ _memory._ ) { /* assign register related parameters */ rnumber = XXX; /* index to register set at off */ roff = XXX; /* offset of rnumber at local bus */ callbackops = &xxmap_ops; /* do all callbacks for this mapping */ maxprot = PROT_ALL; /* allowing all access */ if ((err = devmap_devmem_setup(dhp, dip, callbackops, rnumber, roff, length, maxprot, flags, endian_attr)) < 0) return (err); } else if ( _off_ _is_ _referring_ _to_ _a_ _kernel_ _memory._) { cookie = xsp->cookie; /* cookie is obtained from ddi_umem_alloc(9F) */ koff = XXX; /* offset within the kernel memory. */ callbackops = NULL; /* don't do callback for this mapping */ maxprot = PROT_ALL; /* allowing all access */ if ((err = devmap_umem_setup(dhp, dip, callbackops, cookie, koff, length, maxprot, flags, endian_attr)) < 0) return (err); } *maplen = length; return (0); } ``` -------------------------------- ### Get NDP Mapping Example Source: https://www.smartos.org/man/4p/NDP Retrieves an NDP mapping for a given host name. Compile with `gcc -Wall -lsocket -o get get.c`. Requires the sockets library. ```c #include #include #include #include #include #include #include #include int get(char *host) { struct lifreq lifr; struct addrinfo hints, *serverinfo, *p; int err, s; bzero(&hints, sizeof (struct addrinfo)); hints.ai_family = PF_INET6; hints.ai_protocol = IPPROTO_IPV6; if ((err = getaddrinfo(host, NULL, &hints, &serverinfo)) != 0) { (void) fprintf(stderr, "Unable to lookup %s: %s\n", host, gai_strerror(err)); return (1); } s = socket(AF_INET6, SOCK_DGRAM, 0); if (s < 0) { perror("Failed to open IPv6 socket"); return (1); } for (p = serverinfo; p != NULL; p = p->ai_next) { /* Zero out structure */ bzero(&lifr, sizeof (struct lifreq)); (void) strlcpy(lifr.lifr_name, "net0", sizeof (lifr.lifr_name)); (void) memcpy(&lifr.lifr_nd.lnr_addr, p->ai_addr, sizeof (struct sockaddr_storage)); /* Get mapping */ if (ioctl(s, SIOCLIFGETND, &lifr) < 0) { perror("Unable to get NDP mapping"); continue; } /* * lifr.lifr_nd.lnr_hdw_addr now contains the MAC address, * and can be used as desired. */ } /* * Clean up linked list. */ freeaddrinfo(serverinfo); return (0); } int main(int argc, char *argv[]) { if (argc < 2) exit(1); return (get(argv[1])); } ```