#!/bin/sh
#  Copyright (C) 2013, Parallels, Inc. All rights reserved.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# This script is called by CRIU (http://criu.org) after creating namespaces.
#
# Parameters are passed in environment variables.
# Required parameters:
#   VZCTL_PID	  - pid of vzctl
#   STATUSFD	  - file descriptor for sending signal to vzctl
#   WAITFD	  - file descriptor for receiving signal from vzctl
#   VE_STATE_FILE - file with PID of container init process
#   VE_NETNS_FILE - file, which used by ip netns
#   CRTOOLS_SCRIPT_ACTION - current action (set by criu)

exec 1>&2
. @SCRIPTDIR@/vps-functions

vzcheckvar CRTOOLS_SCRIPT_ACTION
[ "$CRTOOLS_SCRIPT_ACTION" = "setup-namespaces" ] || exit 0

vzcheckvar VZCTL_PID
vzcheckvar STATUSFD
vzcheckvar WAITFD
vzcheckvar VE_NETNS_FILE
vzcheckvar VE_STATE_FILE

set -e

pid=$(cat $VE_STATE_FILE)
ln -s /proc/$pid/ns/net $VE_NETNS_FILE

printf '\0\0\0\0' > /proc/$VZCTL_PID/fd/$STATUSFD
ret="`hexdump -e '\"%d\"' -n 4 < /proc/$VZCTL_PID/fd/$WAITFD`"

[ "$ret" -eq "0" ]
