# [doc]script init, liste des fonctions disponibles

## bibi.skuk

Après quelques recherches, j'ai constaté que les fonctions utilisables dans les scripts d'init n'etait pas toutes documentées... je vais essayer de reparer cet oubli.

Ce document est dédié a tout ceux qui veulent faire des scripts d'init ( /etc/init.d/* ). Il n'est pas complet, mais je vais essayer d'y ajouter tout ce que je peux au fur et a mesure.

Voici une liste de ce que j'ai pu trouver pour le moment.

/sbin/functions.sh

Ici se trouvent une bonne partie des fonctions pratiques... en voici une liste, avec le commentaire qui les accompagne (en anglais dans un premier temps, je ferai les traductions/test/exemple par la suite).

```
void import_addon(char *addon)
```

  Import code from the specified addon if it exists

```
void splash(...)
```

  Notify bootsplash/splashutils/gensplash/whatever about

  important events.

 This will override the splash() function...

 

```
void profiling(...)
```

  Notify bootsplash/whatever about important events.

```
void bootlog(...)
```

  Notify bootlogger about important events.

```
void get_bootconfig()
```

    Get the BOOTLEVEL and SOFTLEVEL by setting

    'bootlevel' and 'softlevel' via kernel

    parameters.

```
void get_libdir(void)
```

    prints the current libdir {lib,lib32,lib64}

```
void esyslog(char* priority, char* tag, char* message)
```

    use the system logger to log a message

```
void eindent(int num)
```

    increase the indent used for e-commands.

```
void eoutdent(int num)
```

    decrease the indent used for e-commands.

```
void esetdent(int num)
```

    hard set the indent used for e-commands.

    num defaults to 0

```
void einfo(char* message)
```

    show an informative message (with a newline)

 

```
void einfon(char* message)
```

    show an informative message (without a newline)

```
void ewarn(char* message)
```

    show a warning message + log it

 Log warnings to system log

```
void eerror(char* message)
```

    show an error message + log it

 Log errors to system log

```
void ebegin(char* message)
```

    show a message indicating the start of a process

```
void _eend(int error, char *efunc, char* errstr)
```

    indicate the completion of process, called from eend/ewend

    if error, show errstr via efunc

    This function is private to functions.sh.  Do not call it from a

    script.

```
void eend(int error, char* errstr)
```

    indicate the completion of process

    if error, show errstr via eerror

```
void ewend(int error, char* errstr)
```

    indicate the completion of process

    if error, show errstr via ewarn

 v-e-commands honor RC_VERBOSE which defaults to no.

 The condition is negated so the return value will be zero.

```
char *KV_major(string)
```

    Return the Major (X of X.Y.Z) kernel version

```
char *KV_minor(string)
```

    Return the Minor (Y of X.Y.Z) kernel version

```
char *KV_micro(string)
```

    Return the Micro (Z of X.Y.Z) kernel version.

```
int KV_to_int(string)
```

    Convert a string type kernel version (2.4.0) to an int (132096)

    for easy compairing or versions ...

 We make version 2.2.0 the minimum version we will handle as

 a sanity check ... if its less, we fail ...

```
int get_KV()
```

    Return the kernel version (major, minor and micro concated) as an integer.

    Assumes X and Y of X.Y.Z are numbers.  Also assumes that some leading

    portion of Z is a number.

    e.g. 2.4.25, 2.6.10, 2.6.4-rc3, 2.2.40-poop, 2.0.15+foo

 

```
bool get_bootparam(param)
```

   return 0 if gentoo=param was passed to the kernel

   EXAMPLE:  if get_bootparam "nodevfs" ; then ....

 Parse gentoo option

                    echo "YES"

 Safer way to list the contents of a directory,

 as it do not have the "empty dir bug".

```
char *dolisting(param)
```

    print a list of the directory contents

    NOTE: quote the params if they contain globs.

          also, error checking is not that extensive ...

```
void save_options(char *option, char *optstring)
```

    save the settings ("optstring") for "option"

```
char *get_options(char *option)
```

    get the "optstring" for "option" that was saved

    by calling the save_options function

 

```
char *add_suffix(char * configfile)
```

    Returns a config file name with the softlevel suffix

    appended to it.  For use with multi-config services.

```
char *get_base_ver()
```

    get the version of baselayout that this system is running

 Network filesystems list for common use in rc-scripts.

 This variable is used in is_net_fs and other places such as

 localmount.

```
bool is_net_fs(path)
```

   return 0 if path is the mountpoint of a networked filesystem

   EXAMPLE:  if is_net_fs / ; then ...

 /proc/mounts is always accurate but may not always be available

 

```
bool is_uml_sys()
```

   return 0 if the currently running system is User Mode Linux

   EXAMPLE:  if is_uml_sys ; then ...

```
bool is_vserver_sys()
```

   return 0 if the currently running system is a Linux VServer

   EXAMPLE:  if is_vserver_sys ; then ...

 

```
bool is_xenU_sys()
```

   return 0 if the currently running system is an unprivileged Xen domain

   EXAMPLE:  if is_xenU_sys ; then ...

```
bool get_mount_fstab(path)
```

   return the parameters to pass to the mount command generated from fstab

   EXAMPLE: cmd=$( get_mount_fstab /proc )

            cmd=${cmd:--t proc none /proc}

            mount -n ${cmd}

 

```
char *reverse_list(list)
```

   Returns the reversed order of list

```
void start_addon(addon)
```

   Starts addon.

 

```
void start_volumes()
```

   Starts all volumes in RC_VOLUME_ORDER.

```
 void stop_addon(addon)
```

   Stops addon.

```
void stop_volumes()
```

   Stops all volumes in RC_VOLUME_ORDER (reverse order).

```
bool is_older_than(reference, files/dirs to check)
```

   return 0 if any of the files/dirs are newer than

   the reference file

   EXAMPLE: if is_older_than a.out *.o ; then ...

```
char* bash_variable(char *variable)
```

   Turns the given variable into something that bash can use

   Basically replaces anything not a-z,A-Z into a _

```
void requote()
```

   Requotes params so they're suitable to be eval'd, just like this would:

   set -- 1 2 "3 4"

   /usr/bin/getopt -- '' "$@" | sed 's/^ -- //'

/sbin/runscript.sh

Les fonctions interessantes de /sbin/runscript.sh sont les suivantes :

svc_start : execute la fonction start definie dans le fichier /etc/init.d/*

svc_restart : execute la fonction restart definie dans ce même fichier. Le comportement par defaut de cette fonction est le suivant:

```

svc_restart() {         

    if ! service_stopped "${myservice}" ; then

        svc_stop || return "$?"

    fi              

    svc_start || return "$?"

}

```

en gros, a moins de vouloir avoir quelque chose de special dans la focntion restart, il ne sert a rien de la preciser.

svc_status : renvoie le statut du service :

        inactive|starting|started|stopping|stopped

avec des couleurs delon le statut...

svc_homegrown : permet d'executer une fonction definie par l'utilisateur. par exemple :

```

foo() {

    einfo "toto"

}

start() {

    svc_homegrown( foo ) #execute la fonction foo()

}

```

Voila, c'est tout pour le moment...

A venir : les fonctions de runscript.sh et des rc-*.sh...

Pour plus d'information sur les init script :

http://www.gentoo.org/doc/fr/handbook/handbook-x86.xml?part=2&chap=4

Suggestion, commentaire et lettres d'insultes appréciées...

----------

## Enlight

Heu... chez moi /sbin/functions.sh c'est du shellscript.

----------

## bibi.skuk

 *Enlight wrote:*   

> Heu... chez moi /sbin/functions.sh c'est du shellscript.

 

Oui, c'est du shell script, mais c'est documenté comme ca... de maniere a savoir a peu pres comment on s'en sert...

----------

