Merged with SYSCALL_SWITCH branch (sswitch_merge tag)
[util-vserver.git] / util-vserver / src / save_ctxinfo.c
1 // $Id$    --*- c++ -*--
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 // Saves current ctx + vserver-info into 'argv[1] + /run' which must be a dead
19 // symlink
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24 #include "compat.h"
25
26 #include "vserver.h"
27 #include "internal.h"
28 #include "util.h"
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <limits.h>
35
36 inline static void
37 checkParams(int argc, char *argv[])
38 {
39   if (argc<3) {
40     WRITE_MSG(2, "Usage:  save_ctxinfo <VSERVER_DIR> <cmd> <args>*\n");
41     exit(255);
42   }
43 }
44
45 int main(int argc, char *argv[])
46 {
47   char          runfile[(checkParams(argc,argv),strlen(argv[1])) + sizeof("/run")];
48   char          dstfile[PATH_MAX];
49   int           fd;
50   char          buf[32];
51   ctx_t         ctx;
52   size_t        len;
53   size_t        len1 = strlen(argv[1]);
54
55   strcpy(runfile,      argv[1]);
56   strcpy(runfile+len1, "/run");
57
58   ctx=vc_X_getcctx();
59   if (ctx==-1) {
60     perror("vc_X_getcctx()");
61     return -1;
62   }
63
64   if (readlink(runfile, dstfile, sizeof(dstfile))==-1) {
65     perror("readlink()");
66     return -1;
67   }
68
69   fd = open(dstfile, O_EXCL|O_CREAT|O_WRONLY, 0644);
70   if (fd==-1) {
71     perror("open()");
72     return -1;
73   }
74
75   len  = utilvserver_uint2str(buf, sizeof(buf), ctx, 10);
76
77   if (write(fd, buf,     len) !=len  ||
78       write(fd, "\n",    1)   !=1    ||
79       write(fd, argv[1], len1)!=len1 ||
80       write(fd, "\n",    1)   !=1) {
81     perror("write()");
82     return -1;
83   }
84
85   if (close(fd)==-1) {
86     perror("close()");
87     return -1;
88   }
89
90   execv(argv[2], argv+2);
91   perror("execv()");
92   return -1;
93 }