Commit Graph

235 Commits

Author SHA1 Message Date
Zbigniew Jędrzejewski-Szmek 60bec8e403 shared/install: move root skipping into create_symlink()
No functional change intended.
2016-08-19 09:55:54 -04:00
Zbigniew Jędrzejewski-Szmek 11e11fd57a shared/install: ignore unit symlinks when doing preset-all
Before, when interating over unit files during preset-all, behaviour was the
following:

- if we hit the real unit name first, presets were queried for that name, and
  that unit was enabled or disabled accordingly,

- if we hit an alias first (one of the symlinks chaining to the real unit), we
  checked the presets using the symlink name, and then proceeded to enable or
  disable the real unit.

E.g. for systemd-networkd.service we have the alias dbus-org.freedesktop.network1.service
(/usr/lib/systemd/system/dbus-org.freedesktop.network1.service), but the preset
is only for the systemd-networkd.service name. The service would be enabled or
disabled pseudorandomly depending on the order of iteration.

For "preset", behaviour was analogous: preset on the alias name disabled the
service (following the default disable policy), preset on the "real" name
applied the presets.

With the patch, for "preset" and "preset-all" we silently skip symlinks. This
gives mostly the right behaviour, with the limitation that presets on aliases
are ignored.  I think that presets on aliases are not that common (at least my
preset files on Fedora don't exhibit any such usage), and should not be
necessary, since whoever installs the preset can just refer to the real unit
file. It would be possible to overcome this limitation by gathering a list of
names of a unit first, and then checking whether *any* of the names matches the
presets list. That would require a significant redesign of the code, and be
a lot slower (since we would have to fully read all unit directories to preset
one unit) to so I'm not doing that for now.

With this patch, two properties are satisfied:
- preset-all and preset are idempotent, and the second and subsequent invocations
  do not produce any changes,
- preset-all and preset for a specific name produce the same state for that unit.

Fixes #3616.
2016-08-19 09:55:54 -04:00
Zbigniew Jędrzejewski-Szmek ff56349d5a shared/install: remove unused paramater and add more comments 2016-08-19 09:55:53 -04:00
Zbigniew Jędrzejewski-Szmek 32d9493e59 systemctl: fix preset-all with missing /etc/systemd/system
If the directory is missing, we can assume that those pesky symlinks are gone too.
2016-08-19 09:55:53 -04:00
Rhys 1cd1dab98c install: follow config_path symlink (#3362)
Under NixOS, the config_path /etc/systemd/system is a symlink to
/etc/static/systemd/system. Commands such as `systemctl list-unit-files`
and `systemctl is-enabled` did not work as the symlink was not followed.

This does not affect how symlinks are treated within the config_path
directory.
2016-08-09 09:33:46 -04:00
Zbigniew Jędrzejewski-Szmek f777b4345e shared/install: allow "enable" on linked unit files (#3790)
User expectations are broken when "systemctl enable /some/path/service.service"
behaves differently to "systemctl link ..." followed by "systemctl enable".
From user's POV, "enable" with the full path just combines the two steps into
one.

Fixes #3010.
2016-07-25 16:20:16 +02:00
Zbigniew Jędrzejewski-Szmek a1feacf77f load-fragment: ignore ENOTDIR/EACCES errors (#3510)
If for whatever reason the file system is "corrupted", we want
to be resilient and ignore the error, as long as we can load the units
from a different place.

Arch bug https://bugs.archlinux.org/task/49547.

A user had an ntfs symlink (essentially a file) instead of a directory after
restoring from backup. We should just ignore that like we would treat a missing
directory, for general resiliency.

We should treat permission errors similarly. For example an unreadable
/usr/local/lib directory would prevent (user) instances of systemd from
loading any units. It seems better to continue.
2016-06-15 23:02:27 +02:00
Zbigniew Jędrzejewski-Szmek 323b7dc903 tree-wide: rename draw_special_char to special_glyph
That function doesn't draw anything on it's own, just returns a string, which
sometimes is more than one character. Also remove "DRAW_" prefix from character
names, TREE_* and ARROW and BLACK_CIRCLE are unambigous on their own, don't
draw anything, and are always used as an argument to special_glyph().

Rename "DASH" to "MDASH", as there's more than one type of dash.
2016-05-09 15:17:57 -04:00
Zbigniew Jędrzejewski-Szmek a760db24bb shared/install: use "→" instead of "pointing to" for a symlink
It's quite a bit shorter and just as readable.

(The full sentence with "pointing to" was added to replace a text that used
"ln -s %s %s". Using the "ln" syntax is indeed unclear, because it's not
obvious which is the source and which is the target, and because symlink(2)
uses the opposite order to ln(1). But with the unicode arrow there should
be no ambiguity.)
2016-05-09 15:17:57 -04:00
Zbigniew Jędrzejewski-Szmek 8515830341 shared/install: do not print warning when a unit is already enabled
Executing 'systemctl enable' on the same unit twice would cause
a warning about a missing [Install] section to be printed. To avoid
this, count all symlinks that "would" be created, and return 1
no matter if we actually created a symlink or skipped creation because
it already exists.
2016-05-09 15:17:57 -04:00
Zbigniew Jędrzejewski-Szmek 893275df36 shared/install: handle dangling aliases as an explicit case, report nicely
This fixes 'preset-all' with a unit that is a dangling symlink.

$ systemctl --root=/ preset-all
Unit syslog.service is an alias to a unit that is not present, ignoring.
Unit auditd.service is masked, ignoring.
Unit NetworkManager.service is masked, ignoring.
2016-05-09 15:17:56 -04:00
Zbigniew Jędrzejewski-Szmek 64f9280ef0 shared/install: add some more debug messages and comments
$ systemctl --root=/ preset foobar.service
Cannot find unit foobar.service.
Failed to preset: No such file or directory.
$ systemctl --root=/ preset foobar@.service
Cannot find unit foobar@.service.
Failed to preset: No such file or directory.
$ systemctl --root=/ preset foobar@blah.service
Cannot find unit foobar@blah.service or foobar@.service.
Failed to preset: No such file or directory.
2016-05-09 15:17:55 -04:00
Zbigniew Jędrzejewski-Szmek 0155928c3c shared/install: simplify error handling conditionals in a few places 2016-05-07 16:19:53 -04:00
Zbigniew Jędrzejewski-Szmek 348dc14569 Merge pull request #3183 from crawford/preset-array
install: cache the presets before evaluating
2016-05-03 16:24:09 -04:00
Alex Crawford 8965d9f8b9 install: cache the presets before evaluating
The previous implementation traversed the various config directories,
walking the preset files and parsing each line to determine if a service
should be enabled or disabled. It did this for every service which
resulted in many more file operations than neccessary.

This approach parses each of the preset entries into an array which is
then used to check if each service should be enabled or disabled.
2016-05-03 11:46:14 -07:00
Zbigniew Jędrzejewski-Szmek 133e5b362f shared/install: refuse template files for non-templateable units
$ systemctl --root=/ enable templated@bar.mount
Unit type mount cannot be templated.
Failed to enable: Invalid argument.
2016-05-01 19:58:59 -04:00
Zbigniew Jędrzejewski-Szmek 6597fa6117 shared/install: warn about DefaultInstance in non-template units
[/etc/systemd/system/mnt-test.mount:6] DefaultInstance only makes sense for template units, ignoring.
2016-05-01 19:58:59 -04:00
Zbigniew Jędrzejewski-Szmek ce99c68a33 Move no_instances information to shared/
This way it can be used in install.c in subsequent commit.
2016-05-01 19:58:59 -04:00
Zbigniew Jędrzejewski-Szmek a772458901 shared/install: ignore Alias in [Install] of units which don't allow aliases
A downside is that a warning about missing [Install] is printed:
$ systemctl --root=/ enable mnt-test.mount
[/etc/systemd/system/mnt-test.mount:5] Aliases are not allowed for mount units, ignoring.
The unit files have no installation config (WantedBy, RequiredBy, Also, Alias
settings in the [Install] section, and DefaultInstance for template units).
This means they are not meant to be enabled using systemctl.
Possible reasons for having this kind of units are:
1) A unit may be statically enabled by being symlinked from another unit's
   .wants/ or .requires/ directory.
2) A unit's purpose may be to act as a helper for some other unit which has
   a requirement dependency on it.
3) A unit may be started when needed via activation (socket, path, timer,
   D-Bus, udev, scripted systemctl call, ...).
4) In case of template units, the unit is meant to be enabled with some
   instance name specified.

That's a bit misleading, but I don't see an easy way to fix this. But
the situation is similar for many other parsing errors, so maybe that's
OK.
2016-05-01 19:58:51 -04:00
Zbigniew Jędrzejewski-Szmek 8a993b61d1 Move no_alias information to shared/
This way it can be used in install.c in subsequent commit.
2016-05-01 19:40:51 -04:00
kayrus 313fe66fbd core: Filter by unit name behind the D-Bus, instead on the client side (#3142)
This commit improves systemd performance on the systems which have
thousands of units.
2016-04-29 15:59:51 +02:00
Alex Crawford d544d1a4d5 install: upgrade message to a warning 2016-04-28 16:01:04 -07:00
Torstein Husebø 4f25723c14 treewide: fix typos (#3092) 2016-04-22 14:18:05 +02:00
Zbigniew Jędrzejewski-Szmek 29380daff5 shared/install: always overwrite symlinks in .wants and .requires
Before:
$ systemctl preset getty@.service
Failed to preset unit, file /etc/systemd/system/getty.target.wants/getty@tty1.service
already exists and is a symlink to ../../../../usr/lib/systemd/system/getty@.service.

After:
$ systemctl preset getty@.service
Created symlink /etc/systemd/system/getty.target.wants/getty@tty1.service,
pointing to /usr/lib/systemd/system/getty@.service.

We don't really care where the symlink points to. For example, it might point
to /usr/lib or /etc, and systemd will always load the unit from /etc in
preference to /usr/lib. In fact, if we make a symlink like
/etc/systemd/system/multi-user.target.wants/b.service -> ../a.service, pid1
will still start b.service. The name of the symlink is the only thing that
matters, as far as systemd is concerned. For humans it's confusing when the
symlinks points to anything else than the actual unit file. At the very least,
the symlink is supposed to point to a file with the same name in some other
directory. Since we don't care where the symlink points, we can always replace
an existing symlink.

Another option I considered would be to simply leave an existing symlink in
place. That would work too, but replacing the symlink with the expected value
seems more intuitive.

Of course those considerations only apply to .wants and .requires. Symlinks
created with "link" and "alias" are a separate matter.

Fixes #3056.
2016-04-21 22:12:07 -04:00
Zbigniew Jędrzejewski-Szmek 12bf0ae4c6 shared/install: rewrite unit_file_changes_add()
path_kill_slashes was applied to the wrong arg...
2016-04-21 13:55:40 -04:00
Zbigniew Jędrzejewski-Szmek 7d782f265d shared/install: nicer error message is symlinking chokes on an existing file
Fixes #1892.

Previously:
Failed to enable unit: Invalid argument

Now:
Failed to enable unit, file /etc/systemd/system/ssh.service already exists.

It would be nice to include the unit name in the message too. I looked into
this, but it would require major surgery on the whole installation logic,
because we first create a list of things to change, and then try to apply them
in a loop. To transfer the knowledge which unit was the source of each change,
the data structures would have to be extended to carry the unit name over into
the second loop. So I'm skipping this for now.
2016-04-21 13:41:59 -04:00
Zbigniew Jędrzejewski-Szmek fe4aede922 systemctl: warning about missing install info for template units
The advice string didn't talk about template units at all. Extend
it and print when trying to enable a template unit without install info.

Fixes #2345.
2016-04-19 09:03:47 -04:00
Zbigniew Jędrzejewski-Szmek af3d811352 shared/install,systemctl,core: report offending file on installation error
Fixes #2191:

$ systemctl --root=/ enable sddm
Created symlink /etc/systemd/system/display-manager.service, pointing to /usr/lib/systemd/system/sddm.service.
$ sudo build/systemctl --root=/ enable gdm
Failed to enable unit, file /etc/systemd/system/display-manager.service already exists and is a symlink to /usr/lib/systemd/system/sddm.service.
$ sudo build/systemctl --root= enable sddm
$ sudo build/systemctl --root= enable gdm
Failed to enable unit: File /etc/systemd/system/display-manager.service already exists and is a symlink to /usr/lib/systemd/system/sddm.service.

(I tried a few different approaches to pass the error information back to the
caller. Adding a new parameter to hold the error results in a gigantic patch
and a lot of hassle to pass the args arounds. Adding this information to the
changes array is straightforward and can be more easily extended in the
future.)

In case local installation is performed, the full set of errors can be reported
and we do that. When running over dbus, only the first error is reported.
2016-04-19 08:58:00 -04:00
Zbigniew Jędrzejewski-Szmek 596fc2636a Various formatting and style fixes 2016-04-18 23:35:51 -04:00
Zbigniew Jędrzejewski-Szmek 24737c2917 install: allow paths like LookupPath.generator to be NULL
Fixes #3047.
2016-04-16 23:08:23 -04:00
Zbigniew Jędrzejewski-Szmek 9a0a413a19 systemctl/core: ignore masked units in preset-all
With any masked unit that would that would be enabled by presets, we'd get:

test@rawhide $ sudo systemctl preset-all
Failed to execute operation: Unit file is masked.

test@rawhide $ sudo systemctl --root=/ preset-all
Operation failed: Cannot send after transport endpoint shutdown

Simply ignore those units:

test@rawhide $ sudo systemctl preset-all
Unit xxx.service is masked, ignoring.
2016-04-16 21:51:09 -04:00
Zbigniew Jędrzejewski-Szmek 76ec966f0e tree-wide: use ERFKILL instead of ESHUTDOWN for "unit masked"
If the error code ever leaks (we print the strerror error instead of providing
our own), the message for ESHUTDOWN is "Cannot send after transport endpoint
shutdown", which can be misleading. In particular it suggest that some
mishandling of the dbus connection occured. Let's change that to ERFKILL which
has the advantage that a) it sounds implausible as actual error, b) has the
connotation of disabling something manually.
2016-04-16 18:41:34 -04:00
Lennart Poettering 344ca7556b core,systemctl: add new "systemctl revert" command
This allows dropping all user configuration and reverting back to the vendor
default of a unit file. It basically undoes what "systemctl edit", "systemctl
set-property" and "systemctl mask" do.
2016-04-12 13:43:32 +02:00
Lennart Poettering d096025b81 install: fix errno handling 2016-04-12 13:43:32 +02:00
Lennart Poettering 8f9364f98b install: simplify skip_root() a bit
Exit early, so that we can get rid of the large if block.
2016-04-12 13:43:32 +02:00
Lennart Poettering 2c52204c3f nstall: no need to export unit_file_lookup_state() anymore
We only use it inside of install.c, hence let's make it static.
2016-04-12 13:43:32 +02:00
Lennart Poettering 4943d14306 systemctl: don't confuse sysv code with generated units
The SysV compat code checks whether there's a native unit file before looking
for a SysV init script. Since the newest rework generated units will show up in
the unit path, and hence the checks ended up assuming that there always was a
native unit file for each init script: the generated one.

With this change the generated unit file directory is suppressed from the
search path when this check is done, to avoid the confusion.
2016-04-12 13:43:32 +02:00
Lennart Poettering e735decc38 systemctl: move check whether a service exists as native unit file to install.c
Move the search path check from the SysV service compat support into install.c
so that we can reuse the usual algorithm instead of rolling a private loop for
this.
2016-04-12 13:43:32 +02:00
Lennart Poettering 76adb5b8b5 install: unify checking whether operations may be applied to a unit file in a new function
Let's replace repeated code by a single implementation in a single function.
2016-04-12 13:43:31 +02:00
Lennart Poettering e4fca67ff0 install: introduce a new unit file state "transient"
Now, that the search path logic knows the unit path for transient units we also
can introduce an explicit unit file state "transient" that clarifies to the
user what kind of unit file he is encountering.
2016-04-12 13:43:31 +02:00
Lennart Poettering 401017e04d install: fix root prefix handling
Previously, we'd execute some operations with the root prefix applied, while
others without (which was a bug). Clean this up: all paths are now prefixed
properly with the root path, and we strip it off when necessary.

(Of course, an alternative option would be to strictly pass around paths
without the prefix prepended and only prepend it right before hitting the disk,
however, I am came to the conclusion this would result in more code.)
2016-04-12 13:43:31 +02:00
Lennart Poettering e4bb56c7a9 install: add root directory to LookupPaths structure
We use the root directory parameter while putting together the LookupPaths
structure, hence let's also store it in the structure as-is. That way we can
drop a parameter from half of the functions in install.c

Also, let's move the validation of the root paths into lookup_paths_init() so
that we can drop even more code from install.c
2016-04-12 13:43:30 +02:00
Lennart Poettering 32c0ed7bbb install: change in_search_path() to take a LookupPaths structure
Similar to the other calls that operate on the collected path data.
2016-04-12 13:43:30 +02:00
Lennart Poettering f4dc1e65e3 install: rename unit_file_is_generated() → path_is_generator()
This way the funciton name matches nicely our other calls path_is_config() and
path_is_runtime().
2016-04-12 13:43:30 +02:00
Lennart Poettering 385eb99634 install: be more accurate when checking whether something is runtime configuration
Let's actually check the runtime config dir, instead of just /run.
2016-04-12 13:43:30 +02:00
Lennart Poettering 463d0d1569 core: remove ManagerRunningAs enum
Previously, we had two enums ManagerRunningAs and UnitFileScope, that were
mostly identical and converted from one to the other all the time. The latter
had one more value UNIT_FILE_GLOBAL however.

Let's simplify things, and remove ManagerRunningAs and replace it by
UnitFileScope everywhere, thus making the translation unnecessary. Introduce
two new macros MANAGER_IS_SYSTEM() and MANAGER_IS_USER() to simplify checking
if we are running in one or the user context.
2016-04-12 13:43:30 +02:00
Lennart Poettering e1c5c2b0d2 install: make use of configuration directory paths in LookupPaths
Now that the LookupPaths structure contains the directory paths, let's make use
of that everywhere instead of duplicating the logic.
2016-04-12 13:43:30 +02:00
Lennart Poettering 7bfe3d44d0 core: when enabling a generated unit file, return a clean error
Let's be precise when the user tries to invoke an "enable" operation on a
generated unit file.
2016-04-12 13:43:29 +02:00
Lennart Poettering f413930863 core: add a new unit file state "generated"
Now that we store the generator directories in LookupPaths we can use this to
intrdouce a new unit file state called "generated", for units in these
directories.

Fixes: #2348
2016-04-12 13:43:29 +02:00
Lennart Poettering a3c4eb0710 core: rework generator dir logic, move the dirs into LookupPaths structure
A long time ago – when generators where first introduced – the directories for
them were randomly created via mkdtemp(). This was changed later so that they
use fixed name directories now. Let's make use of this, and add the genrator
dirs to the LookupPaths structure and into the unit file search path maintained
in it. This has the benefit that the generator dirs are now normal part of the
search path for all tools, and thus are shown in "systemctl list-unit-files"
too.
2016-04-12 13:43:29 +02:00
Martin Pitt 3de1521427 Install: correctly report symlink creations
All callers of create_symlink(), such as install_info_symlink_wants(), expect
that to return > 0 if it actually did something, and then return that number.
unit_file_enable() uses that to determine if any action was done
(carries_install_info != 0) and if not, show a "The unit files have no
[Install] section" warning.

Return 1 instead of 0 in the two code paths of create_symlink() when the link
was created or replaced with a new value.

This fixes getting a bogus "No [Install] section" warning when enabling a unit
with full path, like "systemctl enable /some/path/myunit.service".
2016-04-11 21:03:29 +02:00
Daniel Mack b26fa1a2fb tree-wide: remove Emacs lines from all files
This should be handled fine now by .dir-locals.el, so need to carry that
stuff in every file.
2016-02-10 13:41:57 +01:00
Thomas Hindoe Paaboel Andersen a8fbdf5424 shared: include what we use
The next step of a general cleanup of our includes. This one mostly
adds missing includes but there are a few removals as well.
2015-12-06 13:49:33 +01:00
Thomas Hindoe Paaboel Andersen d986e364c4 install: don't mix state and type enums
from 0ec0deaa
2015-11-15 14:45:24 +01:00
Lennart Poettering 79413b673b core: simplify handling of %u, %U, %s and %h unit file specifiers
Previously, the %u, %U, %s and %h specifiers would resolve to the user
name, numeric user ID, shell and home directory of the user configured
in the User= setting of a unit file, or the user of the manager instance
if no User= setting was configured. That at least was the theory. In
real-life this was not ever actually useful:

- For the systemd --user instance it made no sense to ever set User=,
  since the instance runs in user context after all, and hence the
  privileges to change user IDs don't even exist. The four specifiers
  were actually not useful at all in this case.

- For the systemd --system instance we did not allow any resolving that
  would require NSS. Hence, %s and %h were not supported, unless
  User=root was set, in which case they would be hardcoded to /bin/sh
  and /root, to avoid NSS. Then, %u would actually resolve to whatever
  was set with User=, but %U would only resolve to the numeric UID of
  that setting if the User= was specified in numeric form, or happened
  to be root (in which case 0 was hardcoded as mapping). Two of the
  specifiers are entirely useless in this case, one is realistically
  also useless, and one is pretty pointless.

- Resolving of these settings would only happen if User= was actually
  set *before* the specifiers where resolved. This behaviour was
  undocumented and is really ugly, as specifiers should actually be
  considered something that applies to the whole file equally,
  independently of order...

With this change, %u, %U, %s and %h are drastically simplified: they now
always refer to the user that is running the service instance, and the
user configured in the unit file is irrelevant. For the system instance
of systemd this means they always resolve to "root", "0", "/bin/sh" and
"/root", thus avoiding NSS. For the user instance, to the data for the
specific user.

The new behaviour is identical to the old behaviour in all --user cases
and for all units that have no User= set (or set to "0" or "root").
2015-11-12 17:57:04 +01:00
Lennart Poettering 0ec0deaa30 install: follow unit file symlinks in /usr, but not /etc when looking for [Install] data
Some distributions use alias unit files via symlinks in /usr to cover
for legacy service names. With this change we'll allow "systemctl
enable" on such aliases.

Previously, our rule was that symlinks are user configuration that
"systemctl enable" + "systemctl disable" creates and removes, while unit
files is where the instructions to do so are store. As a result of the
rule we'd never read install information through symlinks, since that
would mix enablement state with installation instructions.

Now, the new rule is that only symlinks inside of /etc are
configuration. Unit files, and symlinks in /usr are now valid for
installation instructions.

This patch is quite a rework of the whole install logic, and makes the
following addional changes:

- Adds a complete test "test-instal-root" that tests the install logic
  pretty comprehensively.

- Never uses canonicalize_file_name(), because that's incompatible with
  operation relative to a specific root directory.

- unit_file_get_state() is reworked to return a proper error, and
  returns the state in a call-by-ref parameter. This cleans up confusion
  between the enum type and errno-like errors.

- The new logic puts a limit on how long to follow unit file symlinks:
  it will do so only for 64 steps at max.

- The InstallContext object's fields are renamed to will_process and
  has_processed (will_install and has_installed) since they are also
  used for deinstallation and all kinds of other operations.

- The root directory is always verified before use.

- install.c is reordered to place the exported functions together.

- Stricter rules are followed when traversing symlinks: the unit suffix
  must say identical, and it's not allowed to link between regular units
  and templated units.

- Various modernizations

- The "invalid" unit file state has been renamed to "bad", in order to
  avoid confusion between UNIT_FILE_INVALID and
  _UNIT_FILE_STATE_INVALID. Given that the state should normally not be
  seen and is not documented this should not be a problematic change.
  The new name is now documented however.

Fixes #1375, #1718, #1706
2015-11-12 17:57:04 +01:00
Lennart Poettering d073dea0a8 install: never log from install functions
Instead, let the caller do that. Fix this by moving masked unit messages
into the caller, by returning a clear error code (ESHUTDOWN) by which
this may be detected.
2015-11-12 17:56:49 +01:00
Lennart Poettering d25e100bd6 install: various simplifications 2015-11-12 17:56:49 +01:00
Susant Sahani 03da651313 install: port to extract_first_word 2015-10-28 22:58:24 +05:30
Lennart Poettering b5efdb8af4 util-lib: split out allocation calls into alloc-util.[ch] 2015-10-27 13:45:53 +01:00
Lennart Poettering 8b43440b7e util-lib: move string table stuff into its own string-table.[ch] 2015-10-27 13:25:56 +01:00
Lennart Poettering 8fcde01280 util-lib: split stat()/statfs()/stavfs() related calls into stat-util.[ch] 2015-10-27 13:25:56 +01:00
Lennart Poettering f4f15635ec util-lib: move a number of fs operations into fs-util.[ch] 2015-10-27 13:25:56 +01:00
Lennart Poettering a09561746f util-lib: introduce dirent-util.[ch] for directory entry calls
Also, move a couple of more path-related functions to path-util.c.
2015-10-27 13:25:56 +01:00
Lennart Poettering 5f311f8c0e util: remove path_get_parent(), in favour of dirname_malloc()
We don't need two functions that do essentialy the same, hence drop
path_get_parent(), and stick to dirname_malloc(), but move it to
path-util.[ch].
2015-10-27 13:25:55 +01:00
Lennart Poettering 3ffd4af220 util-lib: split out fd-related operations into fd-util.[ch]
There are more than enough to deserve their own .c file, hence move them
over.
2015-10-25 13:19:18 +01:00
Lennart Poettering 07630cea1f util-lib: split our string related calls from util.[ch] into its own file string-util.[ch]
There are more than enough calls doing string manipulations to deserve
its own files, hence do something about it.

This patch also sorts the #include blocks of all files that needed to be
updated, according to the sorting suggestions from CODING_STYLE. Since
pretty much every file needs our string manipulation functions this
effectively means that most files have sorted #include blocks now.

Also touches a few unrelated include files.
2015-10-24 23:05:02 +02:00
Lennart Poettering 12ca818ffd tree-wide: clean up log_syntax() usage
- Rely everywhere that we use abs() on the error code passed in anyway,
  thus don't need to explicitly negate what we pass in

- Never attach synthetic error number information to log messages. Only
  log about errors we *receive* with the error number we got there,
  don't log any synthetic error, that don#t even propagate, but just eat
  up.

- Be more careful with attaching exactly the error we get, instead of
  errno or unrelated errors randomly.

- Fix one occasion where the error number and line number got swapped.

- Make sure we never tape over OOM issues, or inability to resolve
  specifiers
2015-09-30 22:26:16 +02:00
Michal Sekletar 8508ea9d05 install: make unit_file_get_list aware of UNIT_FILE_INDIRECT
Commit aedd401 introduced new unit file state, UNIT_FILE_INDIRECT. Unit file is
said to have indirect state if it contains [Install] section which has only
Also= directive. Thus, if enable of such unit file is requested then some other
unit file gets enabled.

Whether or not unit file is in indirect state can be determined by calling
unit_file_can_install. Function unit_file_get_list populates list of unit files
present in given lookup location. So far it did call unit_file_can_install in a
way that would prevent finding out about unit files in indirect state. Such unit
file would be incorrectly marked as static.

Fixes following assertion in test-install,

Assertion 'p->state == s' failed at src/test/test-install.c:59, function main(). Aborting.
[1]    26868 abort (core dumped)  ./test-install
2015-06-25 16:56:53 +02:00
Michal Sekletar 77cd2c87a4 install: explicitly return 0 on success
Maybe there is some left-over value stored in r from previous function
call. Let's make sure we always return consistent error code when we reach end of
the function body.

Fixes following crash of test-install,

Assertion 'r == 0' failed at src/test/test-install.c:52, function main(). Aborting.
[1]    11703 abort (core dumped)  ./test-install
2015-06-25 16:15:36 +02:00
Lennart Poettering 25d40bf57c install: fix bad memory access 2015-06-23 19:16:18 -04:00
Jan Synacek 57ab2eabb8 systemctl: introduce --now for enable, disable and mask
https://bugs.freedesktop.org/show_bug.cgi?id=42940
2015-05-15 11:59:36 +02:00
Lennart Poettering cab6235f74 install: when exporting prefix InstallInfo to become UnitFileInstallInfo
All other types exported from install.h should be namespaces like this,
hence namespace InstallInfo the same way.

Also, remove external forward definition of UnitFileScope type.
2015-05-11 22:53:54 +02:00
Lennart Poettering 7410616cd9 core: rework unit name validation and manipulation logic
A variety of changes:

- Make sure all our calls distuingish OOM from other errors if OOM is
  not the only error possible.

- Be much stricter when parsing escaped paths, do not accept trailing or
  leading escaped slashes.

- Change unit validation to take a bit mask for allowing plain names,
  instance names or template names or an combination thereof.

- Refuse manipulating invalid unit name
2015-05-05 15:06:42 -07:00
Ronny Chevalier 0d67448869 shared: fix memleak
path was used for 2 purposes but it was not freed before being reused.
2015-04-10 15:44:02 +02:00
Harald Hoyer a7f7d1bde4 fix gcc warnings about uninitialized variables
like:

src/shared/install.c: In function ‘unit_file_lookup_state’:
src/shared/install.c:1861:16: warning: ‘r’ may be used uninitialized in
this function [-Wmaybe-uninitialized]
         return r < 0 ? r : state;
                ^
src/shared/install.c:1796:13: note: ‘r’ was declared here
         int r;
             ^
2015-03-27 14:57:38 +01:00
Zbigniew Jędrzejewski-Szmek a8ffe6fbcb sysv-generator: initialize LookupPaths just once
With debugging on, sysv-generator would print the full set of
lookup paths for *every* sysv script.

While at it, pass LookupPaths as a pointer in sysv-generator,
and constify it everywhere.
2015-03-14 23:03:21 -04:00
David Herrmann 15411c0cb1 tree-wide: there is no ENOTSUP on linux
Replace ENOTSUP by EOPNOTSUPP as this is what linux actually uses.
2015-03-13 14:10:39 +01:00
Thomas Hindoe Paaboel Andersen 2eec67acbb remove unused includes
This patch removes includes that are not used. The removals were found with
include-what-you-use which checks if any of the symbols from a header is
in use.
2015-02-23 23:53:42 +01:00
Lennart Poettering 63c372cb9d util: rework strappenda(), and rename it strjoina()
After all it is now much more like strjoin() than strappend(). At the
same time, add support for NULL sentinels, even if they are normally not
necessary.
2015-02-03 02:05:59 +01:00
Ivan Shapovalov 60d27f1916 path-lookup, systemctl: export lookup_paths_init_from_scope() from shared/install.c and use it 2015-01-05 11:13:30 -05:00
Lennart Poettering a34bf9db5d util: rename ignore_file() to hidden_file()
hidden_file() is a bit more precise, since dot files usually shouldn't
be ignored, but certainly be considered hidden.
2014-12-19 20:03:36 +01:00
Lennart Poettering 764458325e core: properly pass unit file state to clients via the bus 2014-12-10 19:58:50 +01:00
Tom Gundersen 56e73b34ce shared: install - report error if mask symlink can not be marked for removal 2014-11-29 11:14:48 +01:00
Michal Schmidt f647962d64 treewide: yet more log_*_errno + return simplifications
Using:
find . -name '*.[ch]' | while read f; do perl -i.mmm -e \
 'local $/;
  local $_=<>;
  s/(if\s*\([^\n]+\))\s*{\n(\s*)(log_[a-z_]*_errno\(\s*([->a-zA-Z_]+)\s*,[^;]+);\s*return\s+\g4;\s+}/\1\n\2return \3;/msg;
  print;'
 $f
done

And a couple of manual whitespace fixups.
2014-11-28 18:56:16 +01:00
Michal Schmidt da927ba997 treewide: no need to negate errno for log_*_errno()
It corrrectly handles both positive and negative errno values.
2014-11-28 13:29:21 +01:00
Michal Schmidt 0a1beeb642 treewide: auto-convert the simple cases to log_*_errno()
As a followup to 086891e5c1 "log: add an "error" parameter to all
low-level logging calls and intrdouce log_error_errno() as log calls
that take error numbers", use sed to convert the simple cases to use
the new macros:

find . -name '*.[ch]' | xargs sed -r -i -e \
's/log_(debug|info|notice|warning|error|emergency)\("(.*)%s"(.*), strerror\(-([a-zA-Z_]+)\)\);/log_\1_errno(-\4, "\2%m"\3);/'

Multi-line log_*() invocations are not covered.
And we also should add log_unit_*_errno().
2014-11-28 12:04:41 +01:00
Jan Synacek aedd4012f4 shared/install: when unit contains only Also=, report 'indirect'
If a unit contains only Also=, with no Alias= or WantedBy=, it shouldn't
be reported as static. New 'indirect' status shall be introduced.

https://bugzilla.redhat.com/show_bug.cgi?id=864298
2014-11-10 19:58:21 +01:00
Dave Reisner 0ffce503cd shared/install: avoid prematurely rejecting "missing" units
f7101b7368 copied some logic to prevent enabling masked units, but
also added a check which causes attempts to enable templated units to
fail. Since we know the logic beyond this check will properly handle
units which truly do not exist, we can rely on the unit file state
comparison to suffice for expressing the intent of f7101b7368.

ref: https://bugs.archlinux.org/task/42616
2014-10-31 08:19:26 -04:00
Zbigniew Jędrzejewski-Szmek e95c98378a systemctl: do not ignore errors in symlink removal
On an ro fs, systemctl disable ... would fail silently.
2014-10-25 15:34:48 -04:00
Michal Schmidt 2d5c93c7af install, cgtop: adjust hashmap_move_one() callers for -ENOMEM possibility
That hashmap_move_one() currently cannot fail with -ENOMEM is an
implementation detail, which is not possible to guarantee in general.
Hashmap implementations based on anything else than chaining of
individual entries may have to allocate.

hashmap_move_one will not fail with -ENOMEM if a proper reservation has
been made beforehand. Use reservations in install.c.

In cgtop.c simply propagate the error instead of asserting.
2014-10-23 17:38:02 +02:00
Michal Schmidt 9f03ee51a2 install: make InstallContext::{will_install,have_installed} OrderedHashmaps
It appears order may matter here. Use OrderedHashmaps to be safe.
2014-10-23 17:38:02 +02:00
Lukas Nykryn e94937df95 systemctl: add add-wants and add-requires verbs 2014-10-08 12:44:00 +02:00
Jan Synacek f7101b7368 core: don't allow enabling if unit is masked 2014-10-07 17:08:18 -04:00
Zbigniew Jędrzejewski-Szmek 4d5dec2389 Rename user_runtime to user_runtime_dir
This makes this function name similar to user_config_home() and makes
it match the name of the environment variable.
2014-10-02 11:10:35 -04:00
Steven Allen 718880ba0d add a transient user unit directory
This patch adds a transient user unit directory under
`$XDG_RUNTIME_DIR/systemd/user/` and stores transient user-instance
units (such as those created by `systemd-run --user`) under there
instead of putting them in $XDG_CONFIG_HOME/systemd/user/.

Fixes https://bugs.freedesktop.org/show_bug.cgi?id=67331
2014-10-02 10:37:30 -04:00
Andreas Henriksson d9ab174bd7 shared: fix resource leak in config_parse_default_instance
The recently allocated "printed" is not freed on error path.

Found by coverity. Fixes: CID#1237745
2014-09-16 21:49:56 +02:00
Michal Schmidt d5099efc47 hashmap: introduce hash_ops to make struct Hashmap smaller
It is redundant to store 'hash' and 'compare' function pointers in
struct Hashmap separately. The functions always comprise a pair.
Store a single pointer to struct hash_ops instead.

systemd keeps hundreds of hashmaps, so this saves a little bit of
memory.
2014-09-15 16:08:50 +02:00
Lukas Nykryn 81fc054dc7 systemctl: fix broken list-unit-files with --root 2014-08-27 11:48:48 +02:00
Lennart Poettering 4fc13f521a Revert "systemctl: fix broken list-unit-files with --root"
This reverts commit 41a451cc29.

This breaks checks for masking of units file, since we invoke
null_or_empty_path() on the resulting path.
2014-08-26 04:09:22 +02:00