# rc.d scripts: "netif" and "routing"



## Niatross (Oct 8, 2013)

I know I can start networking "manually" by using the following command in single user mode:


```
/etc/rc.d/netif start && /etc/rc.d/routing start
```

My question is:

What configuration file in FreeBSD "automatically" starts networking and routing when the system boots into multi user mode?

I've check the /etc/rc.conf and I don't find anything in that file that runs the netif and routing scripts that reside in the /etc/rc.d directory.


----------



## SirDice (Oct 8, 2013)

All startup scripts are started from /etc/rc. See rc(8). The file /etc/rc.conf only contains variables that are sourced by the various rc scripts.


----------



## Niatross (Oct 8, 2013)

Does rc(8) run every startup script that resides in the /etc/rc.d directory when the system boots into multi user mode?

Does rc(8) ignore every startup script that resides in the /etc/rc.d directory when the system boots into single user mode?

If I didn't want a particular script to run, how would I tell rc(8) not to run it? Would I just rename it or move it out of the /etc/rc.d directory?

How is single user mode so intelligent that it knows not to run the netif and routing rc scripts?


----------



## SirDice (Oct 8, 2013)

Niatross said:
			
		

> Does rc(8) run every startup script that resides in the /etc/rc.d directory in multi-user mode?


Yes, see step 7 in rc(8).



> Does rc(8) ignore every startup script that resides in the /etc/rc.d directory in single user mode?


In single user mode rc(8) isn't run at all.



> If I didn't want a particular script to run, how would I tell rc(8) not to run it? Would I just rename it or move it out of the /etc/rc.d directory?


rc.conf(5)


----------



## Niatross (Oct 8, 2013)

So in essence, single user mode runs no services whatsoever because rc(8) doesn't run. Correct?

Also, the /etc/defaults/rc.conf configuration file needs to disable each service that it does not want to run, since rc(8) is going to attempt to run every script in the /etc/rc.d directory. Correct?

Do I have this right?


----------



## SirDice (Oct 8, 2013)

Niatross said:
			
		

> So in essence, single user mode runs no services whatsoever because rc(8) doesn't run. Correct?


Yes, only the root filesystem is mounted read-only and a question is asked which shell to run. Exiting that shell will start rc(8).



> Also, the /etc/defaults/rc.conf configuration file needs to disable each service that it does not want to run since rc(8) is going to attempt to run every script in the /etc/rc.d directory. Correct?


Correct and it does. Although the base OS doesn't have a lot of services. Ports _must_ have their services disabled by default and are therefor not in /etc/default/rc.conf.


----------



## SirDice (Oct 8, 2013)

Niatross said:
			
		

> How is single user mode so intelligent that it knows not to run the netif and routing rc scripts?


It's the kernel that does it by way of loader(8).


----------



## SirDice (Oct 8, 2013)

For example, www/apache22 installs a service. The port installs an rc script in /usr/local/etc/rc.d/. This script must set it's *_enable to NO by default. 

For the base system the scripts in /etc/rc.d/ don't set the default. For them almost everything is disabled by default in /etc/defaults/rc.conf.

In single user mode rc(8) isn't run, so nothing from /etc/rc.d/ or /usr/local/etc/rc.d/ isn't either.


----------



## Niatross (Oct 8, 2013)

So how does a port service (ex: www/apache22) become enabled if its disabled by default?

Do you use the "/etc/rc.conf" configuration file to enable a port service?

It seems that the behavior of the base system rc(8) scripts and the port rc(8) scripts are polar opposites.

Base system rc(8) scripts are enabled by default and disabled via "/etc/defaults/rc.conf" and port rc(8) scripts are disabled by default and need to be enabled in "/etc/rc.conf"

Is this correct?


----------



## SirDice (Oct 9, 2013)

Niatross said:
			
		

> So how does a port service (ex: www/apache22) become enabled if its disabled by default?
> 
> Do you use the /etc/rc.conf configuration file to enable a port service?


Yes, in case of Apache you add apache22_enable="YES" to /etc/rc.conf.



> It seems that the behavior of the base system rc(8) scripts and the port [mn=8]rc[/man]) scripts are polar opposites.
> 
> Base system rc(8) scripts are enabled by default and disabled via /etc/defaults/rc.conf and port rc(8) scripts are disabled by default and need to be enabled in /etc/rc.conf


No, all scripts are run, just the place that defines the default is a little different.


----------



## ShelLuser (Oct 9, 2013)

@SirDice already covered most of it, but a few extra details which could be interesting to know.



			
				Niatross said:
			
		

> So how does a port service (ex: www/apache22) become enabled if its disabled by default?
> 
> Do you use the "/etc/rc.conf" configuration file to enable a port service?


Apart from adding it to /etc/rc.conf it's also good to know that running the rc script with the rcvar command will show you what you need to add in order to start the service:


```
$ ./mysql-server rcvar
eval: cannot open /var/db/mysql/smtp2.losoco.com.pid: Permission denied
# mysql
#
mysql_enable="YES"
#   (default: "")
$ ./dovecot rcvar
# dovecot
#
dovecot_enable="YES"
#   (default: "")
```
So now I know that in order to activate the MySQL or Dovecot server I'd need to use mysql_enable and/or dovecot_enable. Although it's usually best to view the rc script itself, because it is possible that there are some extra (usually optional) variables which can also be set.



			
				Niatross said:
			
		

> Base system rc(8) scripts are enabled by default and disabled via "/etc/defaults/rc.conf" and port rc(8) scripts are disabled by default and need to be enabled in "/etc/rc.conf"


Yes and no.

Basically the behaviour isn't that much different. The main difference is that an rc script provided by a port needs to specify a default behaviour by itself. This is a strict requirement. If I use the same scripts which I showed above you'll soon come across this:


```
([FILE]dovecot[/FILE])

. /etc/rc.subr

name=dovecot
rcvar=dovecot_enable

# read configuration and set defaults
load_rc_config ${name}
[b]: ${dovecot_enable:="NO"}[/b]
[B]: ${dovecot_config:="/usr/local/etc/${name}.conf"}[/B]

([file]mysql-server[/file])

. /etc/rc.subr

name="mysql"
rcvar=mysql_enable

load_rc_config $name

[B]: ${mysql_enable="NO"}[/B]
[B]: ${mysql_limits="NO"}[/B]
: ${mysql_dbdir="/var/db/mysql"}
```
The lines in bold are the ones which set the scripts default behaviour.

So in the end both script types behave completely the same. The main difference is that rc scripts and their options in the base system all have their default values defined in /etc/defaults/rc.conf whereas in the ports it's up to the port maintainer to do this.

And if you don't meet this requirement then this can happen:


```
root@smtp2:/usr/local/etc/rc.d # ./samba status
./samba: WARNING: $samba_enable is not set properly - see rc.conf(5).
Cannot 'status' samba. Set samba_enable to YES in /etc/rc.conf or use 'onestatus' instead of 'status'.
```
This may look like a weird error message, but the fact of the matter is that Samba hasn't been enabled at all on my server (I'm only after smbclient). The problem is that the default value isn't set properly which results in this error message.

Hope this can give you some ideas too. I also want to stress out that a lot of this information can be found in both the manual pages as well as the excellent FreeBSD handbook.


----------



## Niatross (Oct 10, 2013)

SirDice said:
			
		

> No, *all* scripts are run, just the place that defines the default is a little different.



SirDice,

Are you saying that *every* script is run in the /etc/rc.d, no matter if most of them are disabled in the /etc/defaults/rc.conf file? And why would rc(8) want to run *every* script in /usr/local/etc/rc.d when they are disabled by default.

I thought rc8 worked this way:

The /etc/rc script reads /etc/defaults/rc.conf and /etc/rc.conf to determine which services are enabled. The /etc/rc script then runs the corresponding script (in /etc/rc.d or /usr/local/etc/rc.d) to start that service.

Please see the first paragraph:

http://www.freebsd.org/doc/en/articles/linux-users/startup.html

The article (above) doesn't say that rc8 runs *all* scripts in the /etc/rc.d and /etc/defaults/rc.conf . It says it reads the /etc/defaults/rc.conf and /etc/rc.conf files to find out which services are enabled and only runs the script that pertains to that service

Is the (above) correct?

If not, could you give me one big, long, sentence that goes into detail of the steps that rc8 takes to start a service?


----------



## ShelLuser (Oct 10, 2013)

Niatross said:
			
		

> Are you saying that *every* script is run in the /etc/rc.d, no matter if most of them are disabled in the /etc/defaults/rc.conf file? And why would rc(8) want to run *every* script in /usr/local/etc/rc.d when they are disabled by default.


Yes, all scripts are run. Because the rc.d script itself will determine if it's allowed to perform it's action or not. You can see this behaviour for yourself by simply running a script in the /usr/local/etc/rc.d structure. The script will check if it has been enabled in rc.conf and act accordingly.

Also; you addressed the rc(8) manual page several times yourself, keep in mind that this behaviour is also explained there:


```
6.   Invoke rcorder(8) to order the files in /etc/rc.d/ that do not have
          a ``nostart'' KEYWORD (refer to rcorder(8)'s -s flag).

     7.   Call each script in turn using run_rc_script() (from rc.subr(8)),
          which sets $1 to ``start'', and sources the script in a subshell.
          If the script has a .sh suffix then it is sourced directly into the
          current shell.  Stop processing when the script that is the value of
          the $early_late_divider has been run.

     8.   Re-run rcorder(8), this time including the scripts in the
          $local_startup directories.  Ignore everything up to the
          $early_late_divider, then start executing the scripts as described
          above.
```
Another aspect of this behaviour is explained when looking at the demands put on a rc.d script, this is also explained in the same manual page:


```
Each script is expected to support at least the following arguments,
     which are automatically supported if it uses the run_rc_command() func-
     tion:

           start    Start the service.  This should check that the service is
                    to be started as specified by rc.conf(5).  Also checks if
                    the service is already running and refuses to start if it
                    is.  This latter check is not performed by standard
                    FreeBSD scripts if the system is starting directly to
                    multi-user mode, to speed up the boot process.  If
                    forcestart is given, ignore the rc.conf(5) check and start
                    anyway.
```



			
				Niatross said:
			
		

> Please see the first paragraph:
> 
> http://www.freebsd.org/doc/en/articles/linux-users/startup.html
> 
> The article (above) doesn't say that rc8 runs *all* scripts in the /etc/rc.d and /etc/defaults/rc.conf.


In my opinion the article isn't to be taken literally here since it doesn't explain the deep inner workings of rc but merely explains the overall functionality in comparison to that of Linux.

This could be a good read, it explains the process in more detail.

Hope this can help.


----------



## Niatross (Oct 11, 2013)

Do I have this correct now...?

The /etc/rc script is responsible for running every script (in /etc/rc.d and /usr/local/etc/rc.d).

These scripts (in /etc/rc.d and /usr/local/etc/rc.d) are then responsible for checking whether they are enabled (via the *_enable="YES" variable) in /etc/defaults/rc.conf and /etc/rc.conf . If the script is enabled in one of these rc(8) configuration files (AKA: /etc/defaults/rc.conf or /etc/rc.conf, then the service is started.


----------



## SirDice (Oct 11, 2013)

Yep, that sounds about right.


----------

