fishd.c File Reference

The universal variable server. More...

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <pwd.h>
#include <fcntl.h>
#include <getopt.h>
#include <locale.h>
#include <signal.h>
#include "fallback.h"
#include "util.h"
#include "common.h"
#include "wutil.h"
#include "env_universal_common.h"
#include "halloc.h"
#include "halloc_util.h"
#include "path.h"
#include "print_help.h"

Defines

#define UNIX_PATH_MAX   100
 Maximum length of socket filename.
#define MSG_DONTWAIT   0
 Fallback if MSG_DONTWAIT isn't defined.
#define GREETING   "# Fish universal variable daemon\n"
 Small greeting to show that fishd is running.
#define SAVE_MSG   "# This file is automatically generated by the fishd universal variable daemon.\n# Do NOT edit it directly, your changes will be overwritten.\n"
 Small not about not editing ~/.fishd manually.
#define FILE   "fishd."
 The name of the save file.
#define HOSTNAME_LEN   32
 Maximum length of hostname.
#define LOCKPOSTFIX   ".lock"
 The string to append to the socket name to name the lockfile.
#define LOCKTIMEOUT   1
 The timeout in seconds on the lockfile for critical section.
#define GETOPT_STRING   "hv"
 Getopt short switches for fishd.

Functions

static char * get_socket_filename ()
 Constructs the fish socket filename.
static void handle_term (int signal)
 Signal handler for the term signal.
static char * acquire_socket_lock (const char *sock_name)
 Acquire the lock for the socket Returns the name of the lock file if successful or NULL if unable to obtain lock.
static int get_socket ()
 Connects to the fish socket and starts listening for connections.
static void broadcast (int type, const wchar_t *key, const wchar_t *val)
 Event handler.
static void daemonize ()
 Make program into a creature of the night.
static wchar_t * fishd_env_get (wchar_t *key)
 Get environment variable value.
static wchar_t * fishd_get_config ()
 Get the configuration directory.
static void load_or_save (int save)
 Load or save all variables.
static void load ()
 Load variables from disk.
static void save ()
 Save variables to disk.
static void init ()
 Do all sorts of boring initialization.
int main (int argc, char **argv)
 Main function for fishd.

Variables

static connection_tconn
 The list of connections to clients.
static int sock
 The socket to accept new clients on.
static int quit = 0
 Set to one when fishd should save and exit.


Detailed Description

The universal variable server.

fishd is automatically started by fish if a fishd server isn't already running. fishd reads any saved variables from ~/.fishd, and takes care of communication between fish instances. When no clients are running, fishd will automatically shut down and save.

Commands

Fishd works by sending and receiving commands. Each command is ended with a newline. These are the commands supported by fishd:

set KEY:VALUE
set_export KEY:VALUE

These commands update the value of a variable. The only difference between the two is that set_export-variables should be exported to children of the process using them. When sending messages, all values below 32 or above 127 must be escaped using C-style backslash escapes. This means that the over the wire protocol is ASCII. However, any conforming reader must also accept non-ascii characters and interpret them as UTF-8. Lines containing invalid UTF-8 escape sequences must be ignored entirely.

erase KEY

Erase the variable with the specified name.

barrier
barrier_reply

A barrier command will result in a barrier_reply being added to the end of the senders queue of unsent messages. These commands are used to synchronize clients, since once the reply for a barrier message returns, the sender can know that any updates available at the time the original barrier request was sent have been received.


Define Documentation

#define FILE   "fishd."

The name of the save file.

The hostname is appended to this.

Referenced by find_process(), get_interpreter(), history_save_mode(), launch_process(), load_or_save(), parser_destroy(), proc_get_jiffies(), read_ni(), search_ini(), wfopen(), and wfreopen().

#define HOSTNAME_LEN   32

Maximum length of hostname.

Longer hostnames are truncated

Referenced by load_or_save().

#define MSG_DONTWAIT   0

Fallback if MSG_DONTWAIT isn't defined.

That's actually prerry bad, and may lead to strange fishd behaviour, but at least it should work most of the time.

Referenced by main().

#define SAVE_MSG   "# This file is automatically generated by the fishd universal variable daemon.\n# Do NOT edit it directly, your changes will be overwritten.\n"

Small not about not editing ~/.fishd manually.

Inserted at the top of all .fishd files.

Referenced by load_or_save().


Function Documentation

static char* acquire_socket_lock ( const char *  sock_name  )  [static]

Acquire the lock for the socket Returns the name of the lock file if successful or NULL if unable to obtain lock.

The returned string must be free()d after unlink()ing the file to release the lock

References acquire_lock_file(), LOCKPOSTFIX, LOCKTIMEOUT, and wperror().

Referenced by get_socket().

static void broadcast ( int  type,
const wchar_t *  key,
const wchar_t *  val 
) [static]

Event handler.

Broadcasts updates to all clients.

References message_t::count, create_message(), connection::next, q_put(), try_send_all(), and connection::unsent.

Referenced by init().

static wchar_t* fishd_env_get ( wchar_t *  key  )  [static]

Get environment variable value.

The resulting string needs to be free'd.

References env_universal_common_get(), str2wcs(), and wcs2str().

Referenced by fishd_get_config().

static wchar_t* fishd_get_config (  )  [static]

Get the configuration directory.

The resulting string needs to be free'd. This is mostly the same code as path_get_config(), but had to be rewritten to avoid dragging in additional library dependencies.

References _, create_directory(), debug(), fishd_env_get(), and wcsdupcat.

Referenced by load_or_save().


Generated on Sun Mar 8 15:46:54 2009 for fish by  doxygen 1.5.6