This commit was generated by cvs2svn to compensate for changes in r120,
[util-vserver.git] / src / vreboot.c
1 // $Id: vreboot.c,v 1.1 2003/09/29 22:01:57 ensc Exp $
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on vreboot.cc by Jacques Gelinas
5 //  
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10 //  
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //  
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 /*
21         Used to send a reboot message to the reboot manager. It opens /dev/reboot
22         and write "reboot\n".
23 */
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 #include <sys/socket.h>
32 #include <sys/un.h>
33 #include <stdbool.h>
34
35 /*
36         Connect to a unix domain socket
37 */
38 static int vreboot_connect (const char *sockpath, bool showerror)
39 {
40         int ret = -1;
41         int fd =  socket (AF_UNIX,SOCK_STREAM,0);
42         if (fd == -1){
43                 if (showerror) perror("socket client");
44         }else{
45                 struct sockaddr_un un;
46                 int s;
47                 
48                 un.sun_family = AF_UNIX;
49                 strcpy (un.sun_path,sockpath);
50                 s = connect(fd,(struct sockaddr*)&un,sizeof(un));
51                 if (s == -1){
52                         if (showerror) fprintf (stderr,"connect %s (%s)\n"
53                                 ,sockpath,strerror(errno));
54                 }else{
55                         ret = fd;
56                 }
57         }
58         return ret;
59 }
60
61 static void usage()
62 {
63         fprintf (stderr,"vreboot version %s\n",VERSION);
64         fprintf (stderr,"\n");
65         fprintf (stderr,"vreboot [ --socket path ]\n");
66         fprintf (stderr,"vhalt   [ --socket path ]\n");
67         fprintf (stderr,"vreboot request a reboot or a halt of a virtual server\n");
68 }
69
70 int main (int argc, char *argv[])
71 {
72         int ret = -1;
73         int i;
74         const char *sockpath = "/dev/reboot";
75         for (i=1; i<argc; i++){
76                 const char *arg = argv[i];
77                 const char *opt = argv[i+1];
78                 if (strcmp(arg,"--socket")==0){
79                         sockpath = opt;
80                         i++;
81                 }else if (strcmp(arg,"--help")==0){
82                         break;
83
84                 }else{
85                         fprintf (stderr,"Invalid option %s\n",arg);
86                         break;
87                 }
88         }
89         if (argc != i){
90                 usage();
91         }else{
92                 int fd = vreboot_connect (sockpath,true);
93                 if (fd != -1){
94                         if (strstr(argv[0],"halt")!=NULL){
95                                 write (fd,"halt\n",5);
96                         }else{
97                                 write (fd,"reboot\n",7);
98                         }
99                         close (fd);
100                         ret = 0;
101                 }
102         }
103         return ret;
104 }
105