Commit Graph

164 Commits

Author SHA1 Message Date
Fabian Ebner
ff79ee6596 allow workers to count warnings and finish tasks in a WARNINGS state
as is already supported by the UI (and PBS).

A nice bonus is that warn() can be used by both workers and non-workers. For
workers, the output is redirected/duplicated as set up by {fork,tee}_worker(),
and non-erroring workers that issued a warning will end in a WARNINGS state.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
2021-04-23 14:35:55 +02:00
Thomas Lamprecht
7a2e8ca127 tools: getaddrinfo: code/indentation cleanup
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2021-04-06 10:33:51 +02:00
Fabian Ebner
4c4bd1047c sendmail: use more complete email regex and shellquote
Shellquote is needed for '~', and while it doesn't help with '-',
there should be no problem, because options are separated from mailto
since commit 216a3f4f13.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
Reviewed-By: Dominik Csapak <d.csapak@proxmox.com>
2021-02-18 12:49:12 +01:00
Thomas Lamprecht
9d52694bca followup comment/code cleanups
actually explain why we set to undef and not use `delete`.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2020-12-03 16:51:33 +01:00
Dominik Csapak
40682a697d tools: add extract_sensitive_params
moved and generalized from pve-storage, since we'll need it
in more places

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
2020-12-03 16:46:16 +01:00
Thomas Lamprecht
a7886364a7 sendmail: code cleanup
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2020-10-29 15:20:21 +01:00
Thomas Lamprecht
09d47f9d47 tools: sync_mountpoint: throw actual error is syncfs fails
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2020-09-18 11:59:32 +02:00
Stoiko Ivanov
b260d4e320 sync_mountpoint: open path so that sync works
sync_mountpoint takes a path, gets an open filedescriptor and calls
syncfs(2) on it.
by opening with O_PATH the syncfs call fails with EBADF (see open(2)).

found by running:
```
pkill -f 'pvedaemon worker';
strace -yyttT -s 512 -o /tmp/trace -fp $(pgrep -f pvedaemon$)
```

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
2020-09-18 11:53:50 +02:00
Stoiko Ivanov
a24d91ea08 sendmail helper: allow empty display name in from
This patch enables the sendmail helper sub to send emails with a non-existant
display name in the from address. This is used to replace the direct call to
the sendmail binary in pve-manager/PVE/API2/APT.pm.

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
2020-09-07 09:40:25 +02:00
Stoiko Ivanov
b18826ce61 sendmail-helper: only send multipart if necessary
PVE::Tools::sendmail currently always sends a multipart/alternative message
irrespective of the actual content of the mail (e.g. a plain-text only mail
need not be sent as multipart message).

Additionally a few small refactorings based on the discussion
in https://lists.proxmox.com/pipermail/pbs-devel/2020-August/000423.html
and commited in 66004f22c6475ceb0146cf2df1f380f9f0274be4 in the
rust proxmox repository git://git.proxmox.com/git/proxmox.git
were carried over.

tested by creating a backup of a VM and setting an e-mail address, having
ha-manager send a mail after fencing as well as sending a few small mails via
'perl -e'

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
2020-09-07 09:40:25 +02:00
Thomas Lamprecht
ba104d6760 run command: fix matching \r\n and use non-capturing group for it
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2020-08-19 08:54:08 +02:00
Dominik Csapak
cb9db10c1a run_command: improve performance for logging and long lines
to call out/err/logfunc with each line, we search for a newline and call
outfunc/logfunc with everything before that

since we do a select/read (with 4096 size) in a loop, this means
that if we have very long lines, we search for a newline in an
ever growing buffer (for which we know does not contain a newline)

so instead, only search the new data for newlines

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
2020-08-19 07:39:39 +02:00
Fabian Grünbichler
8fb28ab914 safe_read_from: bump default size limit to 512k
this is (via file_get_contents and file_copy) used for reading from
pmxcfs, which has a file size limit of 512k. since quite a number of
call sites would need to explicitly override this (and then get updated
if we bump the limit on the pmxcfs side again in the future), making our
default file reader compatible by default seems the better solution.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
2020-08-19 07:38:44 +02:00
Oguz Bektas
216a3f4f13 sendmail: separate 'mailto' list from the rest of the parameters
Signed-off-by: Oguz Bektas <o.bektas@proxmox.com>
2020-07-07 17:07:16 +02:00
Fabian Ebner
890d25d963 print_text_table: handle undefined values in comparision
by introducing a safe_compare helper. Fixes warnings, e.g.
pvesh get /nodes/<NODE>/network
would print "use of uninitialized"-warnings if there are inactive
network interfaces, because for those, 'active' is undef.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
Tested-by: Oguz Bektas <o.bektas@proxmox.com>
2020-05-04 18:20:39 +02:00
Dominik Csapak
30aeac2ef2 fix #2618: increase maximum port for spice to 61999
and expose the range in its own sub to reuse it in http-server

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
2020-03-04 15:30:37 +01:00
Thomas Lamprecht
8bc99fda82 followup: avoid very long line through comment moval
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2019-11-12 14:16:01 +01:00
Dominik Csapak
c1d62a9d54 fix Tools::df for big storage usage values
if the size/avail of a mount is bigger than a certain amount,
json_encode writes the number in scientific format, which was not
matched by our \d+ regex.

This then resulted in 'undef' values for the result hash and
subsequently led to errors and warnings.

Extend the regex to also match scientific formatted numbers,
perl can then use them as is, no need for any conversion.

https://forum.proxmox.com/threads/bug-in-pve-tools-df-when-adding-petabyte-scale-storage.60090/#post-277050

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2019-11-12 14:12:02 +01:00
Wolfgang Bumiller
76c5fee814 Tools: add new mount api wrappers
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-11-11 17:09:56 +01:00
Wolfgang Bumiller
1b0bc6c0ab tools: add AT_FDCWD and extend exports
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-11-11 17:09:56 +01:00
Thomas Lamprecht
b8faece385 followup: drop get_host_arch prototype
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2019-10-29 07:16:38 +01:00
Stefan Reiter
ce007e99ff tools: get_host_arch: return raw uname machine entry
The current version had only one user in LXC, so move the LXC-specific
code there to reuse this in QemuServer.

Also cache, since the host's architecture can't change during runtime.

Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
2019-10-29 07:13:54 +01:00
Fabian Grünbichler
6cf6b40469 tools: add fchownat syscall
and constant AT_EMPTY_PATH for chowning a directory/file opened via
openat(2), for example when walking/creating a directory tree without
following symlinks.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
2019-08-14 15:33:19 +02:00
Thomas Lamprecht
3982313e8d array_intersect: allow passing list of references for convenience
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2019-07-23 09:14:34 +02:00
Thomas Lamprecht
26a68cf6d8 array_intersect: followup: early exit if first empty intersection found
no point in trying more

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2019-07-23 09:04:22 +02:00
Thomas Lamprecht
3858602867 followup: code comments and small "cleanups"
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2019-07-23 09:03:23 +02:00
Thomas Lamprecht
4c28a8bc69 follouwp: maker array_unique private for now
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2019-07-23 08:50:11 +02:00
Stefan Reiter
a59544e779 Add array_intersect and array_unique functions
Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
2019-07-23 08:46:49 +02:00
Stoiko Ivanov
0c078e6645 Tools: add setresuid syscall
See `man 2 setresuid`. The code was tested with small UIDs (109) and
one which does not fit into 16 bit (100000000), since I wasn't too
sure about the workings of setresuid vs. setresuid32 (see NOTES of
the manpage) - it worked with both.

Linux on amd64 has only a single setresuid, as it the 64 bit arch
came after the setresuid32 syscall, and thus it started with the
later one as single common one

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2019-04-17 10:00:24 +00:00
Wolfgang Bumiller
243c4e5892 tools: add dev_t_major/minor
Extract major/minor from `dev_t` values as found in stat()
calls, with support for the full 32 bit values.

The device value returned by stat() is 32 bits long and encoded as
high 12 bit of the minor value as in the 12 MSBs, 12 bit major value,
then the low 8 bit of the minor value in the low byte.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2019-02-04 11:36:35 +01:00
Thomas Lamprecht
cee0e23ac0 Tools: add fsync sycall
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2019-01-31 11:42:17 +01:00
Dominik Csapak
0a3de87e0f Tools.pm: do not ignore "0" in split_list
giving split_list the string "0" would actually return
an empty list instead of a list with the element "0"

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
2018-11-21 11:06:20 +01:00
Thomas Lamprecht
14410e5f20 tools: template_replace: esacpe braces
To avoid warnings (and in the future, errors) like:

> Unescaped left brace in regex is deprecated here (and will be fatal
> in Perl 5.32), passed through in regex; marked by <-- HERE in
> m/([^{]+)?({ <-- HERE ([^}]+)})?/ at /usr/share/perl5/PVE/Tools.pm
> line 673.

with future perl versions, like Debian Buster for example has.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2018-11-19 10:05:54 +01:00
Thomas Lamprecht
fb3a1b2985 fix some erro mesage and comment typos
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2018-09-20 09:31:19 +02:00
Dietmar Maurer
732b693f14 new helper get_host_arch 2018-09-20 06:21:55 +02:00
Dominik Csapak
2e353dfb5f run_fork_with_timeout: handle SIGTERM
when stopping a worker while doing a run_fork_with_timeout,
we want to handle that there and send the child the SIGTERM
so that it can clean up

for this we have to use readline_nointr,
because the read from the pipe gets interrupted by the signal

this partially fixes #1874
as we now correctly clean up the new disk, except if it is
on lvm/lvmthin (possibly other storages as well), and use the old disk
in the config

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
2018-08-22 15:32:56 +02:00
Dominik Csapak
e03a836591 add readline_nointr to Tools
this is a wrapper to have an uninterruptible readline
so that we can read e.g. from a pipe even if interrupted by
a signal

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
2018-08-22 15:32:56 +02:00
Wolfgang Bumiller
f024a872c4 add PVE::SystemD module
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2018-06-15 11:33:17 +02:00
Wolfgang Bumiller
893ec6f259 tools: unbless errors in run_fork_with_timeout
We cannot properly encode blessed objects as json, so
instead, we should stringify them. This happened for
instance if a VM's systemd scope wasn't cleaned up as we
got an error as a Net::DBus::Error object causing a
"malformed json string" error to appear instead of the
actual message.

Additionally, add a 'must_stringify' helper: The above error
object implements a '""' operator for stringification (as
all error should), but in theory that could die as well, in
which case we just return a generic error string we'll
hopefully never see...

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2018-06-04 12:55:57 +02:00
Dietmar Maurer
62635f9235 new helper PVE::Tools::du() - get disk usage
We simply call the external binary 'du', so that we can abort the command
when we run into a timeout.
2018-04-25 10:29:58 +02:00
Dominik Csapak
fd58bb2bff untaint df return values
since we sometimes use their length in a format string for printf

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
Co-authored-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2018-04-24 10:42:13 +02:00
Thomas Lamprecht
4fdc9659e9 Tools/df: reuse run_fork_with_timeout
Use the later added run_fork_with_timeout for encapsulating df in a
safely manner.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2017-12-21 09:28:47 +01:00
Thomas Lamprecht
ed61b9d675 run_command: add 'quiet' parameter for omitting STD* prints
Without this patch we printed to STDOUT and STDERR, respectively, if
no $outfunc or $errfunc was passed.

Sometimes it's useful if one, or even both, of those prints can
be suppressed, currently this can only be done by either using an array
of arrays or a whole string for the command and redirecting STDOUT
and STDERR.

Add a 'quiet' option which allows to do this in an easier way.
It allows to silent STDERR or STDOUT or both.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2017-12-19 13:09:07 +01:00
Dominik Csapak
95386dafb7 fix convert_size with decimal numbers and add tests
converting from 0.5 gb to mb resulted in 0 mb
with this patch it correctly returns 512

also add tests and catch more errors

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
2017-12-15 11:15:50 +01:00
Thomas Lamprecht
590b924e19 wait_for_vnc_port: allow to enforce IP family
Most times a port was requested for a specified IP family (v4, v6)
only. Thus also ensure that the port from the respective family got
ready, else we may return on a false positive.

As we had no user setting the $timeout param we can add the $family
param as second one, it'll get used more often, so no need to put it
at the back.

As we do nothing if not defined this does not changes the behavior of
our users yet.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2017-12-05 13:48:49 +01:00
Thomas Lamprecht
8fd0d63460 wait_for_vnc_port: die if port did not get ready
All of our users expected this behavior and did not check for undef

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2017-12-05 13:48:49 +01:00
Thomas Lamprecht
91bae4c070 lock_file_full: add missing trailing newline
When we do not instantly get the lock we print a respective message
to stderr. This shows also up in the task logs, and if it's the last
message before a 'Task OK' the UI gets confused an shows the task as
erroneous.

Keep the message as its a good feedback for the user to see why an op
seems to do nothing, so simply add a trailing newline.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2017-11-10 11:28:21 +01:00
Wolfgang Bumiller
a2d049afde tools: more general run_fork_with_timeout + run_fork 2017-10-13 13:21:44 +02:00
Wolfgang Bumiller
e8c13610c4 tools: df: handle a failing df
This function assumed df() will work or hang, but it can
also actually fail and return undef which results in
warnings - let's silence those.
2017-09-12 14:00:45 +02:00
Thomas Lamprecht
f2c72fc3ba run_fork_with_timeout: allow returning complex structures
Encode the result or the error in JSON. This way complex objects or
exceptions may be passed to the parent in a generic way.

This allows to remove the second pipe 'pipe_err'.

Allow also to return undef without any warnings to our caller.
This avoids a "use of uninitialized variable ..." warning

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2017-09-12 13:37:56 +02:00
Thomas Lamprecht
8e677e74e7 Tools: add convert_size for generic byte conversion
We often need to convert between file sizes, for formatting output,
but also code-internal. Some methods expect kilobytes, some gigabytes
and sometimes we need bytes.

While conversion from smaller to bigger units can be simply done with
a left-shift, the opposite conversion may need more attention -
depending on the used context.

If we allocate disks this is quite critical. For example, if we need
to allocate a disk with size 1023 bytes using the
PVE::Storage::vdisk_alloc method (which expects kilobytes) a
right shift by 10 (<=> division by 1024) would result in "0", which
obviously fails.

Thus we round up the converted value if a remainder was lost on the
transformation in this new method. This behaviour is opt-out, to be
on the safe side.

The method can be used in a clear way, as it gives information about
the source and target unit size, unlike "$var *= 1024", which doesn't
gives direct information at all, if not commented or derived
somewhere from its context.

For example:
 > my $size = convert_unit($value, 'gb' => 'kb');
is more clear than:
 > my $size = $value*1024*1024;

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2017-09-11 11:12:41 +02:00
Thomas Lamprecht
eead1ccaa5 run_fork_with_timeout: do not overwrite global signal handlers
perls 'local' must be either used in front of each $SIG{...}
assignments or they must be put in a list, else it affects only the
first variable and the rest are *not* in local context.

This may cause weird behaviour where daemons seemingly do not get
terminating signals delivered correctly and thus may not shutdown
gracefully anymore.

As we only send SIGINT to processes if a manual stop action gets
triggered just catch this one here.

As this is a general method which allows to pass an arbitrary code
payload we cannot sanely handle all signals here, so remove trapping
all other besides SIGINT, if those need to be trapped that should be
done by the caller on a case by case basis.

Fixes: #1495

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2017-09-07 10:27:29 +02:00
Thomas Lamprecht
2d38b8a1b9 tools: add pipe_socket_to_command
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2017-08-21 16:07:30 +02:00
Emmanuel Kasper
72fba9114b Add run_fork_with_timeout utility
This runs subroutine in a forked process
and kills it after a timeout
2017-06-29 15:16:03 +02:00
Thomas Lamprecht
9a41a7b7c9 use more reliable checks in wait_for_vnc_port
We run into problems where this method returned to early, even if the
port wasn't actually ready yet. The reason for this is that we
checked /proc/net/tcp which does not guarantees and always up to date
state of only those ports which are actuall available, i.e. a port
could linger around (time-wait state) or appear even if it wasn't
accepting connections yet (as stated in the kernel docs:
/proc/net/tcp is seen as obsolete by the kernel devs).

Use the `ss` tool from the iproute2 package, it uses netlink to get
the current state and has switches where we can direct it to really
only get the state of those sockets which interest us currently.
I.e., we tell it to get only listening TCP sockets from the requested
port.

The only drawback is that we loop on a run_command, which is slower
than just reading a file. A single loop needs about 1ms here vs the
60µs on the /proc/net/tcp read. But this isn't a api call which is
used highly frequently but rather once per noVNC console open.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2017-06-27 11:22:55 +02:00
Wolfgang Bumiller
a0ecb15991 tools: next_unused_port: use IPPROTO_TCP explicitly
Otherwise perl tries to bind+listen on a UDP socket if the
TCP socket fails - which is a waste since we're looking for
TCP ports.
Additionall since UDP doesn't support listen(), perl will
return EOPNOTSUPP instead of, say, EADDRINUSE. (We don't
care about the error in this code though.)
2017-05-31 07:34:14 +02:00
Wolfgang Bumiller
c14960cc1d tools: unused ports: optional address parameter
While it should be impossible to bind to a wildcard address
when the port is in use by any other address there's one
case where this is allowed, and that's when the port is in
use by an ipv6 address while trying to bind to an ipv4
wildcard.
This currently happens when qemu finds ::1 for the
'localhost' we pass to qemu's spice address while we're
resolving the local nodename via IPv4.
2017-05-31 07:32:17 +02:00
Thomas Lamprecht
c8e94d4bb5 swap raw syscall numbers with syscall.ph for easier porting
Raw syscall numbers were not platform independent, so replace them
with the helpers provided from the syscall.ph perl bits helper.

This makes reading the code easier as a nice side effect.

As syscall.ph is not an ordinary module and makes problems when it is
required by multiple modules we make a own module PVE::Syscall which
loads it and allows to export the necessary constants in a sane way.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2017-05-24 11:24:34 +02:00
Wolfgang Bumiller
d9f86d0d87 Tools: make file-locking aware of external exception sources
Previously an external exception (eg. caused by a SIGARLM in a code
which is already inside a run_with_timeout() call) could happen in
various places where we did not properly this situation.
For instance after calling $lock_func() but before reaching the cleanup
code. In this case a lock was leaked.
Additionally the code was broken in that it used perl's automatic hash
creation side effect ($a->{x}->{y} implicitly initializing $a->{x} with
an empty hash when it did not exist). The effect was that if our own
time out was triggered after the initial check for an existing file
handle inside $lock_func() happened (extremely rare since perl would have
to be running insanely slow), the cleanup did:

    if (my $fh = $lock_handles->{$$}->{$filename}->{fh}) {

This recreated $lock_handles->{$$}->{$filename} as an empty hash.
A subsequent call to lock_file_full() will think a file descriptor
already exists because the check simply used:

    if (!$lock_handles->{$$}->{$filename}) {

While this could have been a one-line fix for this one particular case,
we'd still not be taking external timeouts into account causing the
first issue described above.
2017-05-12 11:40:26 +02:00
Dietmar Maurer
6d46baf63a mark decode_utf8_parameters() as depreciated 2017-05-02 11:51:29 +02:00
Thomas Lamprecht
38d9aa1247 run_command: default exit code to -1
When the child process running the command got  an signal or failed
to execute exitcode was still undefined as we extract it just only
after the signal/failed to execute check.
This led to:
 > Use of uninitialized value in numeric ne (!=) at
 > /usr/share/perl5/PVE/API2/Qemu.pm line 1433.
errors if we used run_commands `noerr` param and checked for the
commands exit code.

So just default the exit code to -1 for such cases.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2017-04-10 14:50:13 +02:00
Dietmar Maurer
ccf60d3bb8 PVE::Tools::dump_journal: allow to filter a specific service 2017-04-04 06:35:24 +02:00
Emmanuel Kasper
d3c8f0c182 Add utility subroutine to get the fully qualified domain name of a host 2017-04-03 11:07:01 +02:00
Dietmar Maurer
9867ff7a83 PVE::Tools::encrypt_pw() - new helper copied from pve-access-control 2017-03-30 17:49:26 +02:00
Dominik Csapak
0f3f314ed7 add keeplocale parameter to run_command
since the "lang" param has not worked, introduce a "keeplocale"
parameter instead.

the default behaviour is the same (set LC_ALL to 'C'), but we can use
the parameter to keep the locale from the host (eg. for the vncshell)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
2017-03-09 09:24:52 +01:00
Dominik Csapak
3c476ed580 (maybe) fixes #1229: fix port reservation
when reserving ports, we use lock_file to lock the
reservation file, but then use file_set_content which
writes a new file and renames it, making the lock invalid
and different processes waiting for the lock get inconsistent
data

instead we use a designated lock file for the lock, so that we don't
lose the lock when writing the reservation file

this should fix the problem that sometimes multiple vms get the
same vnc/spice port

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
2017-02-16 15:02:55 +01:00
Dominik Csapak
813a5c0d26 fix trailing whitespaces
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
2017-02-16 09:15:47 +01:00
Dietmar Maurer
034a8181c6 safe_read_from: add filename parameter to improve error messages 2016-11-28 07:42:34 +01:00
Wolfgang Bumiller
77b2b96ffc tempfile: use /tmp for fallback-tempfiles
At this point we know it's not a tmpfs (as tmpfs definitely
supports O_TMPFILE), so /tmp makes more sense than /run as
default path.
2016-11-02 12:47:05 +01:00
Wolfgang Bumiller
7e1ee743f4 tempfile: unliked-file fallback
some file systems (eg. ZFS) don't support O_TMPFILE
2016-11-02 12:44:35 +01:00
Wolfgang Bumiller
f0cfc20e65 Fix #1188: tempfile: use /run by default
as /tmp is not a tmpfs by default and some file systems
(like ZFS) don't support O_TMPFILE
2016-11-02 12:43:16 +01:00
Wolfgang Bumiller
ce338f4fbc harden file_set_contents against symlink attacks 2016-08-16 17:12:55 +02:00
Wolfgang Bumiller
123921731a tools: optional prefix for random_ether_addr 2016-07-14 09:01:01 +02:00
Wolfgang Bumiller
d743b69c4b fix mac address generation limitation
Commit de9a267 introduced vec() to optimize the generation
by using binary operations instead of converting back and
forth between hex and strings, but forgot to switch over to
the binary sha1 method. This resulted in only the first 6
hex digits of the output string making up the address.
2016-07-05 13:42:30 +02:00
Wolfgang Bumiller
28705ff6d1 df: untaint the result 2016-07-04 14:13:41 +02:00
Fabian Grünbichler
c9c6d91073 catch malformed mailto/mailfrom in sendmail 2016-06-09 17:55:38 +02:00
Fabian Grünbichler
1a0c010327 remove duplicate 'set -o pipefail' 2016-06-07 10:37:25 +02:00
Wolfgang Bumiller
0b9cf991e5 added: enter_systemd_scope
This essentially performas the task of systemd-run while
also waiting for the job to finish.

With the systemd-run version in jessie we run into a race
condition where the executed process can start forking child
processes before the systemd daemon is done setting up the
scope's cgroups, causing the children to NOT be included in
the cgroups. This means the child processes (in our case
qemu) will not adhere to the limits we want to apply to it
via cgroups.

enter_systemd_scope() performs the setup task of systemd-run
and waits for the job to finish, after this we can spawn the
qemu process without systemd-run.
2016-06-03 11:26:13 +02:00
Wolfgang Bumiller
c064776576 Allow O_PATH and O_TMPFILE to be exported 2016-06-01 11:29:59 +02:00
Wolfgang Bumiller
21c56a963f added: openat, mkdirat 2016-06-01 11:29:49 +02:00
Dietmar Maurer
0a7de8204e sort keymap array 2016-04-30 11:54:20 +02:00
Fabian Grünbichler
48df47a466 Add validate_ssh_public_keys
validate format of SSH public keys using ssh-keygen -l and
temp files.
2016-04-05 12:24:38 +02:00
Dietmar Maurer
5a873e6d35 sendmail: minor cleanups 2016-04-05 06:27:29 +02:00
Thomas Lamprecht
b61a47dbeb Tools: add sendmail
can be used to send multipart (HTML, plain) mails to one or more
recipients at once.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2016-04-05 06:21:45 +02:00
Wolfgang Bumiller
26598a513b Tools: tempfile and tempfile_contents 2016-04-05 06:18:41 +02:00
Wolfgang Bumiller
d0229d1d82 fix O_PATH value 2016-04-05 06:14:12 +02:00
Thomas Lamprecht
19e95cd08f Add since and until parameter to dump_journal
journalctl can check their validness itself
2016-03-02 06:22:47 +01:00
Wolfgang Bumiller
44acb12c27 added syncfs syscall and sync_mountpoint helper 2016-02-11 11:24:11 +01:00
Fabian Grünbichler
f127adaba5 Implement refcounting for flocks
This was already implemented in PVE::LXC::lock_aquire() and
lock_release(). Enabling refcounting in the general
PVE::Tools::lock_file() and lock_file_full() methods allows
us to use one code base for flocking.

Furthermore, we could get rid of various xx_no_lock methods
that were required because the old non-refcounting version
did not support nested flocks (the inner most flock would
close the file handle and thus release the flock).
2016-02-10 11:12:12 +01:00
Wolfgang Bumiller
891b224a8f Tools: add setns system call 2016-01-23 08:59:07 +01:00
Thomas Lamprecht
b148e99f3f Output also lockname if it cannot be acquired
If we can't acquire the lock in lock_file_full and get interrupted
by a signal inqeual to EINTR (e.g. SIGTERM), output also it's name
in the error message to allow better debugging.

Also fix a typo.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2015-12-19 09:05:00 +01:00
Thomas Lamprecht
7e82692825 run_command: return exit code and add noerr
Allow to return the exit code of the executed command.
And as we do not reach the return of the exit code if it was not 0,
a noerr parameter is also needed so we can suppress the 'command
failed' die in case of an exit code unequal to 0.

This is required as some programs return another value than 0 when
they succeed, For example `systemctl list-jobs` returns  a value
>= 0 on a successful execution, normally 1.
Without this patch a run_command call to `systemctl list-jobs` gets
marked as failed although it was successful.

This does not break current behaviour in any way as setting the
noerr parameter is required to return something other than 0 or
undef, which are equal in a boolean comparison.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2015-11-30 12:59:45 +01:00
Wolfgang Link
23e0e0d786 add function file_copy
to have a save copy.
2015-11-26 08:08:31 +01:00
Wolfgang Bumiller
be8f0477bc fix CLONE constant declaration 2015-10-30 11:04:51 +01:00
Wolfgang Bumiller
817c6be02c Tools: make unshare behave like other perl syscalls
Most syscall wrappers in perl return 1 on success and our
current use of Tools::unshare isn't using the return value
(yet), so let's fix this while we can.

Also it seems to make sense to use prototyping on syscalls
to add some compile-time argument checking.
2015-10-30 11:03:55 +01:00
Wolfgang Bumiller
97c8c8577d Tools::df: fork and use Filesys::Df
Instead of depending on the 'df' commandline tool do a
fork() to create a killable process and run Filesys::Df,
returning the data over a pipe.
2015-10-30 07:00:37 +01:00
Dietmar Maurer
85d5625a15 random_ether_addr: code cleanups 2015-10-09 11:48:25 +02:00
Philipp Marek
de9a267fec Shorter implementation of random_ether_addr(). 2015-10-09 11:46:50 +02:00
Wolfgang Bumiller
cd9bd2526a Tools::IPV4OCTET: move longer alternatives forward
In an alternation /a|b|c/ the first match matches, so while
'1.1.1.121' matches /^$IPV4RE$/ (note the ^ and $ anchors),
parsing a line like /nameserver ($IPV4RE)/ would only
extract '1.1.1.12', ignoring the last '1' due to the /[1-9]/
alternative matching before the /1[0-9]/ one.
2015-10-05 12:31:24 +02:00
Wolfgang Bumiller
176b1186bd INotify::read_etc_resolv_conf: ipv6 support 2015-10-05 12:30:44 +02:00