root / trunk / src / dot / proxy / main.c

Revision 7618, 4.5 kB (checked in by BradNeuberg, 22 months ago)

Local and remote SVN repositories somehow became out of sync and corrupted -- re-adding these in

Line 
1/*
2Copyright (c) 2003-2006 by Juliusz Chroboczek
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20THE SOFTWARE.
21*/
22
23#include "polipo.h"
24
25AtomPtr configFile = NULL;
26AtomPtr pidFile = NULL;
27int daemonise = 0;
28
29static void
30usage(char *argv0)
31{
32    fprintf(stderr,
33            "%s [ -h ] [ -v ] [ -x ] [ -c filename ] [ -- ] [ var=val... ]\n",
34            argv0);
35    fprintf(stderr, "  -h: display this message.\n");
36    fprintf(stderr, "  -v: display the list of configuration variables.\n");
37    fprintf(stderr, "  -x: perform expiry on the disk cache.\n");
38    fprintf(stderr, "  -c: specify the configuration file to use.\n");
39}
40
41int
42main(int argc, char **argv)
43{
44    FdEventHandlerPtr listener;
45    int i;
46    int rc;
47    int expire = 0, printConfig = 0;
48
49    initAtoms();
50    CONFIG_VARIABLE(daemonise, CONFIG_BOOLEAN, "Run as a daemon");
51    CONFIG_VARIABLE(pidFile, CONFIG_ATOM, "File with pid of running daemon.");
52
53    preinitChunks();
54    preinitLog();
55    preinitObject();
56    preinitIo();
57    preinitDns();
58    preinitServer();
59    preinitHttp();
60    preinitDiskcache();
61    preinitLocal();
62    preinitForbidden();
63    preinitSocks();
64        preinitOffline();
65
66    i = 1;
67    while(i < argc) {
68        if(argv[i][0] != '-')
69            break;
70        if(strcmp(argv[i], "--") == 0) {
71            i++;
72            break;
73        } else if(strcmp(argv[i], "-h") == 0) {
74            usage(argv[0]);
75            exit(0);
76        } else if(strcmp(argv[i], "-v") == 0) {
77            printConfig = 1;
78            i++;
79        } else if(strcmp(argv[i], "-x") == 0) {
80            expire = 1;
81            i++;
82        } else if(strcmp(argv[i], "-c") == 0) {
83            i++;
84            if(i >= argc) {
85                usage(argv[0]);
86                exit(1);
87            }
88            if(configFile)
89                releaseAtom(configFile);
90            configFile = internAtom(argv[i]);
91            i++;
92        } else {
93            usage(argv[0]);
94            exit(1);
95        }
96    }
97
98    if(configFile)
99        configFile = expandTilde(configFile);
100
101    if(configFile == NULL) {
102        configFile = expandTilde(internAtom("~/.polipo"));
103        if(configFile)
104            if(access(configFile->string, F_OK) < 0) {
105                releaseAtom(configFile);
106                configFile = NULL;
107            }
108    }
109
110    if(configFile == NULL) {
111        if(access("/etc/polipo/config", F_OK) >= 0)
112            configFile = internAtom("/etc/polipo/config");
113        if(configFile && access(configFile->string, F_OK) < 0) {
114            releaseAtom(configFile);
115            configFile = NULL;
116        }
117    }
118
119    rc = parseConfigFile(configFile);
120    if(rc < 0)
121        exit(1);
122
123    while(i < argc) {
124        rc = parseConfigLine(argv[i], "command line", 0, 0);
125        if(rc < 0)
126            exit(1);
127        i++;
128    }
129
130    initChunks();
131    initLog();
132    initObject();
133    if(!expire && !printConfig)
134        initEvents();
135    initIo();
136    initDns();
137    initHttp();
138    initServer();
139    initDiskcache();
140    initForbidden();
141    initSocks();
142        initOffline();
143
144    if(printConfig) {
145        printConfigVariables(stdout, 0);
146        exit(0);
147    }
148
149    if(expire) {
150        expireDiskObjects();
151        exit(0);
152    }
153
154    if(daemonise)
155        do_daemonise(logFile == NULL || logFile->length == 0);
156
157    if(pidFile)
158        writePid(pidFile->string);
159
160    listener = create_listener(proxyAddress->string,
161                               proxyPort, httpAccept, NULL);
162    if(!listener) {
163        if(pidFile) unlink(pidFile->string);
164        exit(1);
165    }
166
167    eventLoop();
168
169    if(pidFile) unlink(pidFile->string);
170    return 0;
171}
Note: See TracBrowser for help on using the browser.