libmetal
Functions
utilities.c File Reference
#include <metal/utilities.h>
#include <metal/sys.h>

Functions

int metal_open (const char *path, int shm)
 Open (or create) a file. More...
 
int metal_open_unlinked (const char *path, int shm)
 Open (or create) and unlink a file. More...
 
void metal_randomize_string (char *template)
 Randomize a string. More...
 
void metal_mktemp_template (char template[PATH_MAX], const char *name)
 Create a file name template suitable for use with metal_mktemp. More...
 
int metal_mktemp (char *template, int fifo)
 Create a temporary file or fifo. More...
 
int metal_mktemp_unlinked (char *template)
 Open (or create) and unlink a temporary file. More...
 
int metal_map (int fd, off_t offset, size_t size, int expand, int flags, void **result)
 Map a segment of a file/device. More...
 
int metal_unmap (void *mem, size_t size)
 Unmap a segment of the process address space. More...
 
int metal_mlock (void *mem, size_t size)
 Lock in a region of the process address space. More...
 
int metal_virt2phys (void *addr, unsigned long *phys)
 

Function Documentation

◆ metal_map()

int metal_map ( int  fd,
off_t  offset,
size_t  size,
int  expand,
int  flags,
void **  result 
)

Map a segment of a file/device.

This function maps a segment of a file or device into the process address space, after optionally expanding the file if necessary. If required, the file is expanded to hold the requested map area. This is done under and advisory lock, and therefore the called must not have an advisory lock on the file being mmapped.

Parameters
[in]fdFile descriptor to map.
[in]offsetOffset in file to map.
[in]sizeSize of region to map.
[in]expandAllow file expansion via ftruncate if non-zero.
[in]flagsFlags for mmap(), MAP_SHARED included implicitly.
[out]resultReturned pointer to new memory map.
Returns
0 on success, or -errno on error.

◆ metal_mktemp()

int metal_mktemp ( char *  template,
int  fifo 
)

Create a temporary file or fifo.

This function creates a temporary file or fifo, and sets the O_CLOEXEC flag on it.

Parameters
[in]templateFile name template (the last 6 characters must be XXXXXX per mkstemp requirements).
[in]fifoNon-zero to create a FIFO instead of a regular file.
Returns
File descriptor.

◆ metal_mktemp_template()

void metal_mktemp_template ( char  template[PATH_MAX],
const char *  name 
)

Create a file name template suitable for use with metal_mktemp.

Parameters
[out]templateTemplate string to be filled in.
[in]nameModule/user identifier used as part of the generated template.

◆ metal_mktemp_unlinked()

int metal_mktemp_unlinked ( char *  template)

Open (or create) and unlink a temporary file.

This function creates a temporary file, and sets the O_CLOEXEC flag on it. O_CLOEXEC flag set. This file is then unlinked to ensure that it is removed when the last reference to the file is dropped. This ensures that libmetal shared maps are released appropriately once all users of the shared data exit.

Parameters
[in]templateFile name template (the last 6 characters must be XXXXXX per mkstemp requirements).
Returns
File descriptor.

◆ metal_mlock()

int metal_mlock ( void *  mem,
size_t  size 
)

Lock in a region of the process address space.

Parameters
[in]memPointer to start of region.
[in]sizeSize of region.
Returns
0 on success, or -errno on error.

◆ metal_open()

int metal_open ( const char *  path,
int  shm 
)

Open (or create) a file.

This function opens or creates a file with read/write permissions and the O_CLOEXEC flag set.

Parameters
[in]pathFile path to open.
[in]shmOpen shared memory (via shm_open) if non-zero.
Returns
File descriptor.

◆ metal_open_unlinked()

int metal_open_unlinked ( const char *  path,
int  shm 
)

Open (or create) and unlink a file.

This function opens or creates a file with read/write permissions and the O_CLOEXEC flag set. This file is then unlinked to ensure that it is removed when the last reference to the file is dropped. This ensures that libmetal shared maps are released appropriately once all users of the shared data exit.

Parameters
[in]pathFile path to open.
[in]shmOpen shared memory (via shm_open) if non-zero.
Returns
File descriptor.

◆ metal_randomize_string()

void metal_randomize_string ( char *  template)

Randomize a string.

This function randomizes the contents of a string.

Parameters
[in]templateString to be randomized.

◆ metal_unmap()

int metal_unmap ( void *  mem,
size_t  size 
)

Unmap a segment of the process address space.

This function unmaps a segment of the process address space.

Parameters
[in]memSegment to unmap.
[in]sizeSize of region to unmap.
Returns
0 on success, or -errno on error.

◆ metal_virt2phys()

int metal_virt2phys ( void *  addr,
unsigned long *  phys 
)