move control socket handling to separate file
[mizunara.git] / mizucp / main.c
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright 2021 Olaf Wintermann. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above copyright
13  *      notice, this list of conditions and the following disclaimer in the
14  *      documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "main.h"
30
31 #include "srvctrl.h"
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <signal.h>
38 #include <errno.h>
39 #include <time.h>
40 #include <sys/stat.h>
41 #include <sys/socket.h>
42 #include <sys/un.h>
43 #include <sys/fcntl.h>
44 #include <pthread.h>
45 #include <poll.h>
46
47 #include <libidav/utils.h>
48
49 #include <ucx/utils.h>
50
51
52
53 #define OPTSTR "hlpsuv"
54
55
56
57 static char *cfgdir;
58 static char *copydir;
59
60 int main(int argc, char** argv) { 
61     int ret = 1;
62     
63     extern char *optarg;
64     extern int optind, opterr, optopt;
65     
66     CPSettings settings;
67     memset(&settings, 0, sizeof(CPSettings));
68     
69     int help = 0;
70     int version = 0;
71     int list = 0;    // list copying processes
72     
73     int c;
74     while((c = getopt(argc, argv, OPTSTR)) != -1) {
75         switch(c) {
76             case 'l': list = 1; break;
77             case 'p': settings.pause = 1; break;
78             case 's': settings.printsocket = 1; break;
79             case 'u': settings.url = 1; break;
80             case 'v': version = 1; break;
81         }
82     }
83     
84     int ac = argc - optind;
85     
86     if(list) {
87         // list command
88     } else if(help) {
89         // print help
90     } else if(version) {
91         // print version
92     } else if(ac == 2) {
93         // copy
94         settings.from = argv[optind];
95         settings.to   = argv[optind+1];
96         ret = mzcp_copy(&settings);
97     } else {
98         
99         // print usage
100     }
101     
102     return ret;
103 }
104
105 static int check_dir(const char *path) {
106     struct stat s;
107     if(stat(path, &s)) {
108         if(errno == ENOENT) {
109             if(mkdir(path, S_IRWXU)) {
110                 fprintf(stderr, "Cannot create %s: %s", path, strerror(errno));
111                 return 1;
112             }
113         } else {
114             fprintf(stderr, "Cannot access %s: %s", path, strerror(errno));
115             return 1;
116         }
117     }
118     return 0;
119 }
120
121 static int check_configdir(void) {
122     char *home = getenv(MZCP_ENV_HOME);
123     
124     char *base = util_concat_path(home, MZCP_CFG_BASE);
125     if(check_dir(base)) return 1;
126     free(base);
127
128     cfgdir = util_concat_path(home, MZCP_CFG_DIR);
129     if(check_dir(cfgdir)) return 1;
130     
131     copydir = util_concat_path(mzcp_get_cfgdir(), MZCP_COPY_DIR);
132     if(check_dir(copydir)) return 1;
133     
134     return 0;
135 }
136
137 const char* mzcp_get_cfgdir(void) {
138     return cfgdir;
139 }
140
141 const char* mzcp_get_copydir(void) {
142     return copydir;
143 }
144
145 int mzcp_copy(CPSettings *settings) {
146     int ret  = 0;
147     
148     if(check_configdir()) {
149         return 2;
150     }
151     
152     
153     if(create_control_socket()) {
154         return 3;
155     }
156     
157     if(settings->printsocket) {
158         printf("%s\n", mzcp_get_socketpath());
159     } else {
160         printf("copy %s to %s\n", settings->from, settings->to);
161         if(settings->pause) {
162             printf("pause\n");
163         }
164     }
165     
166     //pid_t p = fork();
167     pid_t p = 0;
168     if(p == 0) {
169         //close(0);
170         //close(1);
171         //close(2);
172         
173         ret =  mzcp_srvctrl(settings);
174     }
175     
176     return ret;
177 }