0384eb697fa7c5f2922898c430e0a228e93af778
[util-vserver.git] / src / vreboot.c
1 // $Id: vreboot.c,v 1.2 2003/09/30 20:16:53 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 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <errno.h>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #include <sys/socket.h>
36 #include <sys/un.h>
37 #include <stdbool.h>
38
39 /*
40         Connect to a unix domain socket
41 */
42 static int vreboot_connect (const char *sockpath, bool showerror)
43 {
44         int ret = -1;
45         int fd =  socket (AF_UNIX,SOCK_STREAM,0);
46         if (fd == -1){
47                 if (showerror) perror("socket client");
48         }else{
49                 struct sockaddr_un un;
50                 int s;
51                 
52                 un.sun_family = AF_UNIX;
53                 strcpy (un.sun_path,sockpath);
54                 s = connect(fd,(struct sockaddr*)&un,sizeof(un));
55                 if (s == -1){
56                         if (showerror) fprintf (stderr,"connect %s (%s)\n"
57                                 ,sockpath,strerror(errno));
58                 }else{
59                         ret = fd;
60                 }
61         }
62         return ret;
63 }
64
65 static void usage()
66 {
67         fprintf (stderr,"vreboot version %s\n",VERSION);
68         fprintf (stderr,"\n");
69         fprintf (stderr,"vreboot [ --socket path ]\n");
70         fprintf (stderr,"vhalt   [ --socket path ]\n");
71         fprintf (stderr,"vreboot request a reboot or a halt of a virtual server\n");
72 }
73
74 int main (int argc, char *argv[])
75 {
76         int ret = -1;
77         int i;
78         const char *sockpath = "/dev/reboot";
79         for (i=1; i<argc; i++){
80                 const char *arg = argv[i];
81                 const char *opt = argv[i+1];
82                 if (strcmp(arg,"--socket")==0){
83                         sockpath = opt;
84                         i++;
85                 }else if (strcmp(arg,"--help")==0){
86                         break;
87
88                 }else{
89                         fprintf (stderr,"Invalid option %s\n",arg);
90                         break;
91                 }
92         }
93         if (argc != i){
94                 usage();
95         }else{
96                 int fd = vreboot_connect (sockpath,true);
97                 if (fd != -1){
98                         if (strstr(argv[0],"halt")!=NULL){
99                                 write (fd,"halt\n",5);
100                         }else{
101                                 write (fd,"reboot\n",7);
102                         }
103                         close (fd);
104                         ret = 0;
105                 }
106         }
107         return ret;
108 }
109