freezer.c revision 5e97c3fcce787a5bc0f8ceef43aa3e05195b480a
816bf6f8088b162b681101d93fd450127a0e586fJulian Kornberger/*
a847d9812b328c048773e705606b10875a929034Eugen Kuksa * lxc: linux Container library
5e2a6a8992b4e9e77be3a94fc87af4dc14b0c8c4henning mueller *
a4344d37747b6733bbd0d8df738b614cb385316cTim Reddehase * (C) Copyright IBM Corp. 2007, 2008
d1f0cb74e8bc61e9185488a431b86816cb1cc7edSascha Graef *
c273fbbb7863507673f695bc85709c0cb715011fTim Reddehase * Authors:
c273fbbb7863507673f695bc85709c0cb715011fTim Reddehase * Daniel Lezcano <dlezcano at fr.ibm.com>
5ec1c6ff5da427d945d7fb708567ce3526c4c741Tim Reddehase *
5ec1c6ff5da427d945d7fb708567ce3526c4c741Tim Reddehase * This library is free software; you can redistribute it and/or
98ba1c38b1cce99ecc61117259f2ae05ffe98469Tim Reddehase * modify it under the terms of the GNU Lesser General Public
5ec1c6ff5da427d945d7fb708567ce3526c4c741Tim Reddehase * License as published by the Free Software Foundation; either
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehase * version 2.1 of the License, or (at your option) any later version.
10be32b36246121321399ec3ab26d4d723d550e3Tim Reddehase *
10be32b36246121321399ec3ab26d4d723d550e3Tim Reddehase * This library is distributed in the hope that it will be useful,
10be32b36246121321399ec3ab26d4d723d550e3Tim Reddehase * but WITHOUT ANY WARRANTY; without even the implied warranty of
10be32b36246121321399ec3ab26d4d723d550e3Tim Reddehase * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10be32b36246121321399ec3ab26d4d723d550e3Tim Reddehase * Lesser General Public License for more details.
6f84d8dbc4edca35b7e69c8296a257ea17e3b74fTim Reddehase *
6f84d8dbc4edca35b7e69c8296a257ea17e3b74fTim Reddehase * You should have received a copy of the GNU Lesser General Public
10be32b36246121321399ec3ab26d4d723d550e3Tim Reddehase * License along with this library; if not, write to the Free Software
10be32b36246121321399ec3ab26d4d723d550e3Tim Reddehase * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
10be32b36246121321399ec3ab26d4d723d550e3Tim Reddehase */
10be32b36246121321399ec3ab26d4d723d550e3Tim Reddehase#define _GNU_SOURCE
10be32b36246121321399ec3ab26d4d723d550e3Tim Reddehase#include <stdio.h>
10be32b36246121321399ec3ab26d4d723d550e3Tim Reddehase#undef _GNU_SOURCE
10be32b36246121321399ec3ab26d4d723d550e3Tim Reddehase#include <stdlib.h>
10be32b36246121321399ec3ab26d4d723d550e3Tim Reddehase#include <errno.h>
10be32b36246121321399ec3ab26d4d723d550e3Tim Reddehase#include <unistd.h>
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehase#include <string.h>
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehase#include <fcntl.h>
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehase#include <sys/types.h>
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehase#include <sys/stat.h>
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase#include <sys/param.h>
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase#include <sys/inotify.h>
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase#include <netinet/in.h>
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase#include <net/if.h>
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase#include <lxc.h>
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase#include <state.h>
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase#include <log.h>
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase
81a6387a4ab56a24194ecbabd6609c6bcca568b7Tim Reddehasestatic int freeze_unfreeze(const char *name, int freeze)
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase{
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase char *freezer, *f = freeze?"FROZEN":"RUNNING";
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase int fd, ret = -1;
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase asprintf(&freezer, LXCPATH "/%s/nsgroup/freezer.state", name);
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase fd = open(freezer, O_WRONLY);
db8aaeeeb3b24f487a5d02c60d18e96e55f6ba85Tim Reddehase if (fd < 0) {
db8aaeeeb3b24f487a5d02c60d18e96e55f6ba85Tim Reddehase lxc_log_syserror("failed to open freezer for '%s'", name);
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase goto out;
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase }
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase ret = write(fd, f, strlen(f) + 1) < 0;
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase close(fd);
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase if (ret)
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase lxc_log_syserror("failed to write to '%s'", freezer);
4949048bda09e116ee3627383e831455954cbe41Tim Reddehaseout:
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase free(freezer);
db8aaeeeb3b24f487a5d02c60d18e96e55f6ba85Tim Reddehase return ret;
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase}
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehaseint lxc_freeze(const char *name)
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase{
db8aaeeeb3b24f487a5d02c60d18e96e55f6ba85Tim Reddehase return freeze_unfreeze(name, 1);
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase}
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehaseint lxc_unfreeze(const char *name)
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase{
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase return freeze_unfreeze(name, 0);
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase}
23f0ab4a423943eb59109df37074ef0c330d07c0Tim Reddehase
4949048bda09e116ee3627383e831455954cbe41Tim Reddehase