wpa_supplicant / hostapd 2.0
Functions

os_internal.c File Reference

wpa_supplicant/hostapd / Internal implementation of OS specific functions More...

#include "includes.h"
#include "os.h"

Functions

void os_sleep (os_time_t sec, os_time_t usec)
 Sleep (sec, usec)
int os_get_time (struct os_time *t)
 Get current time (sec, usec)
int os_mktime (int year, int month, int day, int hour, int min, int sec, os_time_t *t)
 Convert broken-down time into seconds since 1970-01-01.
int os_gmtime (os_time_t t, struct os_tm *tm)
int os_daemonize (const char *pid_file)
 Run in the background (detach from the controlling terminal)
void os_daemonize_terminate (const char *pid_file)
 Stop running in the background (remove pid file)
int os_get_random (unsigned char *buf, size_t len)
 Get cryptographically strong pseudo random data.
unsigned long os_random (void)
 Get pseudo random value (not necessarily very strong)
char * os_rel2abs_path (const char *rel_path)
 Get an absolute path for a file.
int os_program_init (void)
 Program initialization (called at start)
void os_program_deinit (void)
 Program deinitialization (called just before exit)
int os_setenv (const char *name, const char *value, int overwrite)
 Set environment variable.
int os_unsetenv (const char *name)
 Delete environent variable.
char * os_readfile (const char *name, size_t *len)
 Read a file to an allocated memory buffer.
void * os_zalloc (size_t size)
 Allocate and zero memory.
void * os_malloc (size_t size)
void * os_realloc (void *ptr, size_t size)
void os_free (void *ptr)
void * os_memcpy (void *dest, const void *src, size_t n)
void * os_memmove (void *dest, const void *src, size_t n)
void * os_memset (void *s, int c, size_t n)
int os_memcmp (const void *s1, const void *s2, size_t n)
char * os_strdup (const char *s)
size_t os_strlen (const char *s)
int os_strcasecmp (const char *s1, const char *s2)
int os_strncasecmp (const char *s1, const char *s2, size_t n)
char * os_strchr (const char *s, int c)
char * os_strrchr (const char *s, int c)
int os_strcmp (const char *s1, const char *s2)
int os_strncmp (const char *s1, const char *s2, size_t n)
char * os_strncpy (char *dest, const char *src, size_t n)
size_t os_strlcpy (char *dest, const char *src, size_t siz)
 Copy a string with size bound and NUL-termination.
char * os_strstr (const char *haystack, const char *needle)
int os_snprintf (char *str, size_t size, const char *format,...)

Detailed Description

wpa_supplicant/hostapd / Internal implementation of OS specific functions

Copyright
Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>

This software may be distributed under the terms of the BSD license. See README for more details.

This file is an example of operating system specific wrapper functions. This version implements many of the functions internally, so it can be used to fill in missing functions from the target system C libraries.

Some of the functions are using standard C library calls in order to keep this file in working condition to allow the functions to be tested on a Linux target. Please note that OS_NO_C_LIB_DEFINES needs to be defined for this file to work correctly. Note that these implementations are only examples and are not optimized for speed.


Function Documentation

int os_daemonize ( const char *  pid_file)

Run in the background (detach from the controlling terminal)

Parameters:
pid_fileFile name to write the process ID to or NULL to skip this
Returns:
0 on success, -1 on failure
void os_daemonize_terminate ( const char *  pid_file)

Stop running in the background (remove pid file)

Parameters:
pid_fileFile name to write the process ID to or NULL to skip this
int os_get_random ( unsigned char *  buf,
size_t  len 
)

Get cryptographically strong pseudo random data.

Parameters:
bufBuffer for pseudo random data
lenLength of the buffer
Returns:
0 on success, -1 on failure
int os_get_time ( struct os_time t)

Get current time (sec, usec)

Parameters:
tPointer to buffer for the time
Returns:
0 on success, -1 on failure
int os_mktime ( int  year,
int  month,
int  day,
int  hour,
int  min,
int  sec,
os_time_t *  t 
)

Convert broken-down time into seconds since 1970-01-01.

Parameters:
yearFour digit year
monthMonth (1 .. 12)
dayDay of month (1 .. 31)
hourHour (0 .. 23)
minMinute (0 .. 59)
secSecond (0 .. 60)
tBuffer for returning calendar time representation (seconds since 1970-01-01 00:00:00)
Returns:
0 on success, -1 on failure

Note: The result is in seconds from Epoch, i.e., in UTC, not in local time which is used by POSIX mktime().

void os_program_deinit ( void  )

Program deinitialization (called just before exit)

This function is called just before a program exists. If there are any OS specific processing, e.g., freeing resourced allocated in os_program_init(), it should be done here. It is also acceptable for this function to do nothing.

int os_program_init ( void  )

Program initialization (called at start)

Returns:
0 on success, -1 on failure

This function is called when a programs starts. If there are any OS specific processing that is needed, it can be placed here. It is also acceptable to just return 0 if not special processing is needed.

unsigned long os_random ( void  )

Get pseudo random value (not necessarily very strong)

Returns:
Pseudo random value
char* os_readfile ( const char *  name,
size_t *  len 
)

Read a file to an allocated memory buffer.

Parameters:
nameName of the file to read
lenFor returning the length of the allocated buffer
Returns:
Pointer to the allocated buffer or NULL on failure

This function allocates memory and reads the given file to this buffer. Both binary and text files can be read with this function. The caller is responsible for freeing the returned buffer with os_free().

char* os_rel2abs_path ( const char *  rel_path)

Get an absolute path for a file.

Parameters:
rel_pathRelative path to a file
Returns:
Absolute path for the file or NULL on failure

This function tries to convert a relative path of a file to an absolute path in order for the file to be found even if current working directory has changed. The returned value is allocated and caller is responsible for freeing it. It is acceptable to just return the same path in an allocated buffer, e.g., return strdup(rel_path). This function is only used to find configuration files when os_daemonize() may have changed the current working directory and relative path would be pointing to a different location.

int os_setenv ( const char *  name,
const char *  value,
int  overwrite 
)

Set environment variable.

Parameters:
nameName of the variable
valueValue to set to the variable
overwriteWhether existing variable should be overwritten
Returns:
0 on success, -1 on error

This function is only used for wpa_cli action scripts. OS wrapper does not need to implement this if such functionality is not needed.

void os_sleep ( os_time_t  sec,
os_time_t  usec 
)

Sleep (sec, usec)

Parameters:
secNumber of seconds to sleep
usecNumber of microseconds to sleep
size_t os_strlcpy ( char *  dest,
const char *  src,
size_t  siz 
)

Copy a string with size bound and NUL-termination.

Parameters:
destDestination
srcSource
sizSize of the target buffer
Returns:
Total length of the target string (length of src) (not including NUL-termination)

This function matches in behavior with the strlcpy(3) function in OpenBSD.

int os_unsetenv ( const char *  name)

Delete environent variable.

Parameters:
nameName of the variable
Returns:
0 on success, -1 on error

This function is only used for wpa_cli action scripts. OS wrapper does not need to implement this if such functionality is not needed.

void* os_zalloc ( size_t  size)

Allocate and zero memory.

Parameters:
sizeNumber of bytes to allocate
Returns:
Pointer to allocated and zeroed memory or NULL on failure

Caller is responsible for freeing the returned buffer with os_free().

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines