Repository: moby/moby
File: libnetwork/iptables/firewalld.go (Go)

Code before:
	onReloaded        []*func() // callbacks when Firewalld has been reloaded
)

// UsingFirewalld returns true if iptables rules will be applied via firewalld's
// passthrough interface. The error return is non-nil if the status cannot be
// determined because the initialisation function has not been called.
func UsingFirewalld() (bool, error) {
	// If called before startup has completed, the firewall backend is unknown.
	// But, if running rootless, the init function is not called because
	// firewalld will be running in the host's netns, not in rootlesskit's.
	if !firewalldInitCalled && !rootless.RunningWithRootlessKit() {
		return false, fmt.Errorf("iptables.firewalld is not initialised")
	}
	return firewalldRunning, nil
}

// FirewalldReloadedAt returns the time at which the daemon last completed a
// firewalld reload, or a nil-valued time.Time if it has not been reloaded
// since the daemon started.
func FirewalldReloadedAt() time.Time {
	firewalldReloadedAtMu.Lock()
	defer firewalldReloadedAtMu.Unlock()
	return firewalldReloadedAt
}

// firewalldInit initializes firewalld management code.
func firewalldInit() error {
	var err error

	firewalldInitCalled = true
	if connection, err = newConnection(); err != nil {
		return fmt.Errorf("Failed to connect to D-Bus system bus: %v", err)
	}
	firewalldRunning = checkRunning()
	if !firewalldRunning {
		connection.sysconn.Close()
		connection = nil
	}
	if connection != nil {
		go signalHandler()
		zoneAdded, err := setupDockerZone()
		if err != nil {
			return err
		}
		policyAdded, policyAddErr := setupDockerForwardingPolicy()
		if policyAddErr != nil {
			// Log the error, but still reload firewalld if necessary.
			log.G(context.TODO()).WithError(policyAddErr).Warnf("Firewalld: failed to add policy %s", dockerFwdPolicy)
		}
		if zoneAdded || policyAdded {
			// Reload for changes to take effect.

Review comment (suggestion):
With that;

```suggestion
// FirewalldReloadedAt returns the time at which the daemon last completed a
// firewalld reload, or a nil-valued time.Time if it has not been reloaded
// since the daemon started.
func FirewalldReloadedAt() time.Time {
	val := firewalldReloadedAt.Load()
	if val == nil {
		return time.Time{}
	}
	return val.(time.Time)
}
```

Code after:
	onReloaded        []*func() // callbacks when Firewalld has been reloaded
)

// UsingFirewalld returns true if iptables rules will be applied via firewalld's
// passthrough interface. The error return is non-nil if the status cannot be
// determined because the initialisation function has not been called.
func UsingFirewalld() (bool, error) {
	// If called before startup has completed, the firewall backend is unknown.
	// But, if running rootless, the init function is not called because
	// firewalld will be running in the host's netns, not in rootlesskit's.
	if !firewalldInitCalled && !rootless.RunningWithRootlessKit() {
		return false, fmt.Errorf("iptables.firewalld is not initialised")
	}
	return firewalldRunning, nil
}

// FirewalldReloadedAt returns the time at which the daemon last completed a
// firewalld reload, or a zero-valued time.Time if it has not been reloaded
// since the daemon started.
func FirewalldReloadedAt() time.Time {
	val := firewalldReloadedAt.Load()
	if val == nil {
		return time.Time{}
	}
	return val.(time.Time)
}

// firewalldInit initializes firewalld management code.
func firewalldInit() error {
	var err error

	firewalldInitCalled = true
	if connection, err = newConnection(); err != nil {
		return fmt.Errorf("Failed to connect to D-Bus system bus: %v", err)
	}
	firewalldRunning = checkRunning()
	if !firewalldRunning {
		connection.sysconn.Close()
		connection = nil
	}
	if connection != nil {
		go signalHandler()
		zoneAdded, err := setupDockerZone()
		if err != nil {
			return err
		}
		policyAdded, policyAddErr := setupDockerForwardingPolicy()
		if policyAddErr != nil {
			// Log the error, but still reload firewalld if necessary.
			log.G(context.TODO()).WithError(policyAddErr).Warnf("Firewalld: failed to add policy %s", dockerFwdPolicy)
		}