main.cpp 4.53 KB
Newer Older
jpbl's avatar
jpbl committed
1
/*
2
 *  Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
3
 *  Author: Alexandre Bourget <alexandre.bourget@savoirfairelinux.com>
jpbl's avatar
jpbl committed
4
5
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
 *  Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
6
 *
jpbl's avatar
jpbl committed
7
8
9
10
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
11
 *
jpbl's avatar
jpbl committed
12
13
14
15
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
16
 *
jpbl's avatar
jpbl committed
17
18
19
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21
22
23
24
25
26
27
28
29
30
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
jpbl's avatar
jpbl committed
31
32
33
34
35
 */

#include <libintl.h>
#include <cstring>
#include <iostream>
36
#include <memory> // for auto_ptr
Yun Liu's avatar
Yun Liu committed
37
#include <string>
Yun Liu's avatar
Yun Liu committed
38
39
#include <dirent.h>
#include <sys/stat.h>
40
#include <cc++/common.h>
jpbl's avatar
jpbl committed
41
#include "global.h"
42
#include "fileutils.h"
jpbl's avatar
jpbl committed
43

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
44
#include "dbus/dbusmanager.h"
jpbl's avatar
jpbl committed
45
46
#include "manager.h"

47
48
#include "audio/audiolayer.h"

Tristan Matthews's avatar
Tristan Matthews committed
49
ost::CommandOptionNoArg	console (
50
    "console", "c", "Log in console (instead of syslog)"
51
52
);

Tristan Matthews's avatar
Tristan Matthews committed
53
ost::CommandOptionNoArg	debug (
54
    "debug", "d", "Debug mode (more verbose)"
55
56
);

Tristan Matthews's avatar
Tristan Matthews committed
57
ost::CommandOptionNoArg	help (
58
    "help", "h", "Print help"
59
60
);

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// returns true if directory exists
static bool check_dir(const char *path)
{
	DIR *dir = opendir (path);

	if (!dir) {	// doesn't exist
		if (mkdir (path, 0755) != 0) {  // couldn't create the dir
			perror(path);
			return false;
		}
	} else {
		closedir(dir);
	}

	return true;
}

jpbl's avatar
jpbl committed
78
int
Emmanuel Milou's avatar
[#2402]    
Emmanuel Milou committed
79
80
main (int argc, char **argv)
{
81
    set_program_dir(argv[0]);
82
83
84
    // makeCommandOptionParse allocates the object with operator new, so
    // auto_ptr is fine in this context.
    // TODO: This should eventually be replaced with std::unique_ptr for C++0x
Tristan Matthews's avatar
Tristan Matthews committed
85
    std::auto_ptr<ost::CommandOptionParse> args(ost::makeCommandOptionParse (argc, argv, ""));
86

87
88
    printf ("SFLphone Daemon "VERSION", by Savoir-Faire Linux 2004-2011\n" \
    		"http://www.sflphone.org/\n");
89

90
    if (help.numSet) {
91
        std::cerr << args->printUsage();
92
        return 0;
93
    } else if (args->argsHaveError()) {
94
95
        std::cerr << args->printErrors();
        std::cerr << args->printUsage();
96
        return 1;
97
    }
98

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    Logger::setConsoleLog (console.numSet);
    Logger::setDebugMode (debug.numSet);

    const char *xdg_env = XDG_CACHE_HOME;
	std::string path = xdg_env ? xdg_env : std::string(HOMEDIR) + DIR_SEPARATOR_STR ".cache/";

	if (!check_dir(path.c_str()))
		return 1;
    path = path + "sflphone";
	if (!check_dir(path.c_str()))
		return 1;

	std::string pidfile = path + "/" PIDFILE;
    FILE *fp = fopen (pidfile.c_str(),"r");
    if (fp) { // PID file exists. Check the former process still alive or not. If alive, give user a hint.
    	int oldPid;
    	if (fscanf(fp, "%d", &oldPid) != 1) {
116
            std::cerr << "Couldn't read pidfile " << pidfile << std::endl;
117
118
119
120
121
122
    		return 1;
    	}

		fclose (fp);

		if (kill (oldPid, 0) == 0) {
123
            std::cerr << "There is already a sflphoned daemon running in the system. Starting Failed." << std::endl;
124
125
126
127
128
129
130
131
132
			return 1;
		}
	}

    // write pid file
    fp = fopen (pidfile.c_str(),"w");
    if (!fp) {
    	perror(pidfile.c_str());
        return 1;
133
    } else {
134
135
136
137
138
        std::ostringstream pidstr;
        pidstr << getpid();

        fputs (pidstr.str().c_str() , fp);
        fclose (fp);
139
    }
140

141
    try {
142
    	Manager::instance().init();
143
    } catch (const std::exception &e) {
144
        std::cerr << e.what() << std::endl;
145
        return 1;
146
    } catch (...) {
147
        std::cerr << "An exception occured when initializing the system." << std::endl;
148
        return 1;
149
    }
jpbl's avatar
jpbl committed
150

151
152
153
    _debug ("Starting DBus event loop");
    Manager::instance().getDbusManager()->exec();

154
    return 0;
jpbl's avatar
jpbl committed
155
}