Utility Functions

A collection of useful functions. More...

## Macros

#define | VMTOOLS_GUEST_SERVICE "vmsvc"
#define | VMTOOLS_USER_SERVICE "vmusr"
#define | VMTOOLS_EXTERN_C
#define | ABS(x) (((x) >= 0) ? (x) : -(x))
#define | VMTOOLS_GET_FILENAME_LOCAL(path, err)
#define | VMTOOLS_RELEASE_FILENAME_LOCAL(path) g_free(path)
#define | VMTOOLS_WRAP_ARRAY(a) VMTools_WrapArray((a), sizeof *(a), G_N_ELEMENTS(a))

## Typedefs

typedef gboolean(* | SignalSourceCb) (const siginfo_t *, gpointer)

## Functions

G_BEGIN_DECLS void | vm_free (void *ptr)
gboolean | VMTools_LoadConfig (const gchar *path, GKeyFileFlags flags, GKeyFile **config, time_t *mtime)
gboolean | VMTools_WriteConfig (const gchar *path, GKeyFile *config, GError **err)
gboolean | VMTools_ChangeLogFilePath (const gchar *delimiter, const gchar *appendString, const gchar *domain, GKeyFile *conf)
gboolean | VMTools_ConfigGetBoolean (GKeyFile *config, const gchar *section, const gchar *key, const gboolean defValue)
gint | VMTools_ConfigGetInteger (GKeyFile *config, const gchar *section, const gchar *key, const gint defValue)
gchar * | VMTools_ConfigGetString (GKeyFile *config, const gchar *section, const gchar *key, const gchar *defValue)
GSource * | VMTools_NewSignalSource (int signum)
gchar * | VMTools_GetLibdir (void)
GSource * | VMTools_CreateTimer (gint timeout)
| Create a timer based on a monotonic clock source. More...
void | VMTools_SetGuestSDKMode (void)
void | VMTools_AcquireLogStateLock (void)
void | VMTools_ReleaseLogStateLock (void)
void | VMTools_StopLogging (void)
void | VMTools_RestartLogging (void)
void | VMTools_SuspendLogIO (void)
void | VMTools_ResumeLogIO (void)
GArray * | VMTools_WrapArray (gconstpointer data, guint elemSize, guint count)

## Detailed Description

A collection of useful functions.

This module contains functions for loading configuration data and extensions to the glib API that are useful when writing applications.

## Macro Definition Documentation

## VMTOOLS_GET_FILENAME_LOCAL

#define VMTOOLS_GET_FILENAME_LOCAL | ( |  | path,
|  |  | err
| ) |  |

Value:
g_filename_from_utf8((path), \
-1, \
NULL, \
NULL, \
(err))

Converts an UTF-8 path to the local (i.e., glib) file name encoding. This is a no-op on Windows, since the local encoding is always UTF-8 in glib. The returned value should not be freed directly; instead, use VMTOOLS_RELEASE_FILENAME_LOCAL.

Parameters

  [in] | path | Path in UTF-8 (should not be NULL).
  [out] | err | Where to store errors (type: GError **; may be NULL).

Returns
  The path in glib's filename encoding, or NULL on error.

## VMTOOLS_RELEASE_FILENAME_LOCAL

#define VMTOOLS_RELEASE_FILENAME_LOCAL | ( |  | path | ) | g_free(path)

Frees a path allocated with VMTOOLS_GET_FILENAME_LOCAL. No-op on Windows.

Parameters

  [in] | path | Path in UTF-8.

## VMTOOLS_WRAP_ARRAY

#define VMTOOLS_WRAP_ARRAY | ( |  | a | ) | VMTools_WrapArray((a), sizeof *(a), G_N_ELEMENTS(a))

Convenience macro around VMTools_WrapArray.

## Typedef Documentation

## SignalSourceCb

typedef gboolean(* SignalSourceCb) (const siginfo_t *, gpointer)

Type of callback used by the signal event source.

## Function Documentation

## vm_free()

G_BEGIN_DECLS void vm_free | ( | void * | ptr | ) |

Frees a pointer allocated by the vmtools library.

Parameters

  [in] | ptr | Pointer to memory to be freed.

## VMTools_AcquireLogStateLock()

void VMTools_AcquireLogStateLock | ( | void |  | ) |

Acquire the log state lock.

## VMTools_ConfigGetBoolean()

gboolean VMTools_ConfigGetBoolean | ( | GKeyFile * | config,
|  | const gchar * | section,
|  | const gchar * | key,
|  | const gboolean | defValue
| ) |  |

Loads boolean value for a key from the specified config section.

Parameters

  [in] | config | Config file to read the key from.
  [in] | section | Section to look for in the config file.
  [in] | key | Key to look for in the section.
  [in] | defValue | Default value if the key is not found or error.

Returns
  value of the key if value was read successfully, else defValue.

## VMTools_ConfigGetInteger()

gint VMTools_ConfigGetInteger | ( | GKeyFile * | config,
|  | const gchar * | section,
|  | const gchar * | key,
|  | const gint | defValue
| ) |  |

Loads integer value for a key from the specified config section.

Parameters

  [in] | config | Config file to read the key from.
  [in] | section | Section to look for in the config file.
  [in] | key | Key to look for in the section.
  [in] | defValue | Default value if the key is not found or error.

Returns
  value of the key if value was read successfully, else defValue.

## VMTools_ConfigGetString()

gchar* VMTools_ConfigGetString | ( | GKeyFile * | config,
|  | const gchar * | section,
|  | const gchar * | key,
|  | const gchar * | defValue
| ) |  |

Loads string value for a key from the specified config section.

Parameters

  [in] | config | Config file to read the key from.
  [in] | section | Section to look for in the config file.
  [in] | key | Key to look for in the section.
  [in] | defValue | Default value if the key is not found or error.

Returns
  value of the key if value was read successfully, else a copy of defValue unless defValue is NULL, in which case it's NULL. The returned string should be freed with g_free() when no longer needed.

## VMTools_CreateTimer()

GSource * VMTools_CreateTimer | ( | gint | timeout | ) |

Create a timer based on a monotonic clock source.

This timer differs from the glib timeout source, which uses the system time. It is recommended for code that needs more reliable time tracking, using a clock that is not affected by changes in the system time (which can happen when using NTP or the Tools time synchronization feature).

Parameters

  [in] | timeout | The timeout for the timer, must be >= 0.

Returns
  The new source.

## VMTools_LoadConfig()

gboolean VMTools_LoadConfig | ( | const gchar * | path,
|  | GKeyFileFlags | flags,
|  | GKeyFile ** | config,
|  | time_t * | mtime
| ) |  |

Loads the configuration file at the given path.

If an old configuration file is detected and the current process has write permission to the file, the configuration data will automatically upgraded to the new configuration format (the old configuration file is saved with a ".old" extension).

Parameters

  [in] | path | Path to the configuration file, or NULL for default Tools config file.
  [in] | flags | Flags for opening the file.
  [in,out] | config | Where to store the config dictionary; when reloading the file, the old config object will be destroyed.
  [in,out] | mtime | Last known modification time of the config file. When the function succeeds, will contain the new modification time read from the file. If NULL (or 0), the config dictionary is always loaded.

Returns
  Whether a new config dictionary was loaded.

## VMTools_NewSignalSource()

GSource * VMTools_NewSignalSource | ( | int | signum | ) |

Creates a new source for the given signal.

Rather than processing the events in the signal handling context, the main loop is woken up and callbacks are processed in the main loop's thread.

The same "wakeup" file descriptors are used for all sources, so if sources are added to different main loop instances, all of them will be woken up if any signal for which handlers are registered occurs.

This code assumes that the rest of the app is not setting signal handlers directly, at least for signals for which glib sources have been set up.

Also note that on older Linux systems (pre-NPTL), some real-time signals are used by the pthread library and shouldn't be used by applications.

Example of setting a handler for a signal:

GSource *src = VMTools_NewSignalSource(signum);
g_source_set_callback(src, MyCallback, myData, NULL);
g_source_attach(src, myContext);

Note
  This API is not available on Win32.

Parameters

  [in] | signum | Signal to watch.

Returns
  Pointer to the new source, NULL if failed to set signal handler.

## VMTools_ReleaseLogStateLock()

void VMTools_ReleaseLogStateLock | ( | void |  | ) |

Release the log state lock.

## VMTools_RestartLogging()

void VMTools_RestartLogging | ( | void |  | ) |

This is called to reset logging in vmxLogger. NOTE: This must be called after acquiring LogState lock.

## VMTools_ResumeLogIO()

void VMTools_ResumeLogIO | ( | void |  | ) |

Resume IO caused by logging activity.

## VMTools_SetGuestSDKMode()

void VMTools_SetGuestSDKMode | ( | void |  | ) |

Called if vmtools lib is used along with Guestlib SDK.

## VMTools_StopLogging()

void VMTools_StopLogging | ( | void |  | ) |

This is called to avoid nested logging in vmxLogger. NOTE: This must be called after acquiring LogState lock.

## VMTools_SuspendLogIO()

void VMTools_SuspendLogIO | ( | void |  | ) |

Suspend IO caused by logging activity.

## VMTools_WrapArray()

GArray* VMTools_WrapArray | ( | gconstpointer | data,
|  | guint | elemSize,
|  | guint | count
| ) |  |

A convenience function for wrapping an array with a GArray instance.

Parameters

  [in] | data | The array data. The original data is copied into the new array.
  [in] | elemSize | The size of each element in the array.
  [in] | count | The number of elements in the array.

Returns
  A new GArray.

## VMTools_WriteConfig()

gboolean VMTools_WriteConfig | ( | const gchar * | path,
|  | GKeyFile * | config,
|  | GError ** | err
| ) |  |

Saves the given config data to the given path.

Parameters

  [in] | path | Where to save the data.
  [in] | config | Config data.
  [out] | err | Where to store error information (may be NULL).

Returns
  Whether saving was successful.