coverity: don't bother getting template path if we're not going to measure it

This should also fix a memory leak, since we were freeing it under ifdef
but always allocating it.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
coverity: ftell returns long, not size_t (which is unsigned)

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
coverity: correctly handle tpath error case.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
coverity: don't leak open DIR

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
coverity: don't leak partial_fd

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
coverity: dont dereference before null check

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
coverity: make indent match nest level

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
avoid very unlikely race due to EEXIST

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Track snapshot dependencies (v2)

(Will push in a bit barring any objections)

lvm, btrfs, and zfs snapshots each do an ok job of handling deletions
for us - a btrfs snapshot does fine after the original is removed,
while zfs and lvm will both refuse to allow the original to be deleted
while the snapshot exists.

Overlayfs doesn't do this for us.  So, for overlayfs snapshots, track
the dependencies.

When c2 is created as an overlayfs snapshot of dir-backed c1, then

1. c2's lxc_rdepends file will contain

	c1_lxcpath
	c1_lxcname

2. c1's lxc_snapshots will contain "1"

c1 cannot be deleted so long as lxc_snapshots exists and contains
a non-zero number.

The contents of lxc_snapshots and lxc_rdepends are protected by
container_disk_lock() and at lxc_clone by the new container not yet
being accessible.

(Originally I was going to keep them in the container config, but the
problem with using $lxcpath/$name/config is that api users could end up
calling c->save_config() with a cached old value of snapshots/rdepends.)

Changelog:
	aug 21: check for fprintf and fclose failures

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Dwight Engen <dwight.engen@oracle.com>
lxc_cgroup: convert to using API

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
api: convert lxc_wait, lxc_freeze, and lxc_unfreeze

These are the last of the simpler conversions.  Start, execute,
kill, info and attach remain to be done.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Dwight Engen <dwight.engen@oracle.com>
hooks/ubuntu-cloud-prep: add hostname to meta-data

prior to my enabling of the clone hook, the setting of the hostname
was being done by writing to /etc/hostname.  Instead of relying on that
we're now writing 'local-hostname' into the metadata for the instance.

cloud-init then reads this and sets the hostname properly.

We are also writing /etc/hostname with the new hostname explicitly.  This is
useful/necessary because on network bringup of eth0, dhclient will submit its
hosname.  The updating done by cloud-init occurs to late, and thus
the dhcp request goes out with the un-configured hostname and dns doens't
work correctly.

Signed-off-by: Scott Moser <smoser@ubuntu.com>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
api: convert lxc_start

Normal lxc-start usage tends to be "lxc-start -n name [-P lxcpath]".
This causes $lxcpath/$name/config to be the configuration for the
container.  However, lxc-start is more flexible than that.  You can
specify a custom configuration file, in which case $lxcpath/$name/config
is not used.  You can also (in addition or in place of either of these)
specify configuration entries one-by-one using "-s lxc.utsname=xxx".

To support this using the API, if we are not using
$lxcpath/$name/config then we put ourselves into a custom lxcpath
called (configurable using LXCPATH) /var/lib/lxc_anon.  To stop a
container so created, then, you would use

	lxc-stop -P /var/lib/lxc_anon -n name

TODO: we should walk over the list of &defines by hand and set them
using c->set_config_item.  I haven't done that in this patch.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
config_ipv6: run inet_pton on the addr value without mask

otherwise a "$addr/$mask" results in failure.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
lxc-alpine: create /dev/zero

Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
start.c: handle potential signal flood

Signalfd does not guarantee that we'll get an event for every signal.
So if 3 tasks exit at the same time, we may get only one sigchld
event.  Therefore, in signal_handler(), always check whether init has
exited.  Do with with WNOWAIT so that we can still wait4 to cleanup
the init after lxc_poll() exists (rather than complicating the code).

Note - there is still a race in the kernel which can cause the
container init to become a defunct child of the host init (!).  This
doesn't solve that, but is a potential (if very unlikely) race which
apw pointed out while we were trying to create a reproducer for the
kernel bug.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
Fix some typos

Signed-off-by: Dmitry Shachnev <mitya57@ubuntu.com>
Reported-by: Vincent Ladeuil
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
fedora: Add missing double-quotes.

Reported-by: tlc
Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
avoid zgrep -q as it's failing on some distros

Reported-by: Filirom1
Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
fix wrong license text for parts of liblxc library

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>