71
83
return program_dir;
74
//TODO it is faking this, implement proper system
75
const char *get_data_dir()
77
std::string path = std::string(get_program_dir()) + "/../../share/sflphone/ringtones/";
83
std::string xdg_env(XDG_CACHE_HOME);
84
std::string path = (not xdg_env.empty()) ? xdg_env : std::string(HOMEDIR) + DIR_SEPARATOR_STR ".cache/";
86
if (!check_dir(path.c_str()))
91
if (!check_dir(path.c_str()))
94
std::string pidfile = path + "/" PIDFILE;
95
std::ifstream is(pidfile.c_str());
98
// PID file exists. Check if the former process is still alive or
99
// not. If alive, give user a hint.
103
if (kill(oldPid, 0) == 0) {
104
// Use cerr because logging has not been initialized
105
std::cerr << "There is already a sflphoned daemon running in " <<
106
"the system. Starting Failed." << std::endl;
112
std::ofstream os(pidfile.c_str());
115
perror(pidfile.c_str());
86
// FIXME: This should use our real DATADIR
90
return std::string(get_program_dir()) + "/../../share/sflphone/ringtones/";
95
/* Lock a file region */
97
lockReg(int fd, int cmd, int type, int whence, int start, off_t len)
102
fl.l_whence = whence;
106
return fcntl(fd, cmd, &fl);
109
int /* Lock a file region using nonblocking F_SETLK */
110
lockRegion(int fd, int type, int whence, int start, int len)
112
return lockReg(fd, F_SETLK, type, whence, start, len);
119
const std::string name(get_home_dir() + DIR_SEPARATOR_STR PIDFILE);
122
f.fd = open(f.name.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
124
ERROR("Could not open PID file %s", f.name.c_str());
128
if (lockRegion(f.fd, F_WRLCK, SEEK_SET, 0, 0) == -1) {
129
if (errno == EAGAIN or errno == EACCES)
130
ERROR("PID file '%s' is locked; probably "
131
"'%s' is already running", f.name.c_str(), PACKAGE_NAME);
133
ERROR("Unable to lock PID file '%s'", f.name.c_str());
139
if (ftruncate(f.fd, 0) == -1) {
140
ERROR("Could not truncate PID file '%s'", f.name.c_str());
146
snprintf(buf, sizeof(buf), "%ld\n", (long) getpid());
148
const int buf_strlen = strlen(buf);
149
if (write(f.fd, buf, buf_strlen) != buf_strlen) {
150
ERROR("Problem writing to PID file '%s'", f.name.c_str());
124
159
bool isDirectoryWritable(const std::string &directory)
126
161
return access(directory.c_str(), W_OK) == 0;
164
FileHandle::FileHandle(const std::string &n) : fd(-1), name(n)
167
FileHandle::~FileHandle()
169
// we will only delete the file if it was created by this process
172
if (unlink(name.c_str()) == -1)
173
ERROR("%s", strerror(errno));
180
// 1) try getting user's home directory from the environment
181
const std::string home(PROTECTED_GETENV("HOME"));
182
if (not home.empty())
185
// 2) try getting it from getpwuid_r (i.e. /etc/passwd)
186
const long max = sysconf(_SC_GETPW_R_SIZE_MAX);
189
struct passwd pwbuf, *pw;
190
if (getpwuid_r(getuid(), &pwbuf, buf, sizeof(buf), &pw) == 0 and pw != NULL)