Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
dfda0faa
Commit
dfda0faa
authored
Aug 01, 2018
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
unix/{Daemon,PidFile}: throw exception on error
Don't use the bad `FatalError` library.
parent
95acc4be
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
25 additions
and
26 deletions
+25
-26
Daemon.cxx
src/unix/Daemon.cxx
+20
-21
Daemon.hxx
src/unix/Daemon.hxx
+2
-2
PidFile.hxx
src/unix/PidFile.hxx
+3
-3
No files found.
src/unix/Daemon.cxx
View file @
dfda0faa
...
...
@@ -19,9 +19,10 @@
#include "config.h"
#include "Daemon.hxx"
#include "system/
Fatal
Error.hxx"
#include "system/Error.hxx"
#include "fs/AllocatedPath.hxx"
#include "fs/FileSystem.hxx"
#include "util/RuntimeError.hxx"
#ifndef _WIN32
#include "PidFile.hxx"
...
...
@@ -69,18 +70,17 @@ void
daemonize_kill
(
void
)
{
if
(
pidfile
.
IsNull
())
FatalE
rror
(
"no pid_file specified in the config file"
);
throw
std
::
runtime_e
rror
(
"no pid_file specified in the config file"
);
const
pid_t
pid
=
ReadPidFile
(
pidfile
);
if
(
pid
<
0
)
{
const
std
::
string
utf8
=
pidfile
.
ToUTF8
();
FormatFatalError
(
"unable to read the pid from file
\"
%s
\"
"
,
utf8
.
c_str
());
throw
FormatErrno
(
"unable to read the pid from file
\"
%s
\"
"
,
utf8
.
c_str
());
}
if
(
kill
(
pid
,
SIGTERM
)
<
0
)
FormatFatalSystemError
(
"unable to kill process %i"
,
int
(
pid
));
throw
FormatErrno
(
"unable to kill process %i"
,
int
(
pid
));
exit
(
EXIT_SUCCESS
);
}
...
...
@@ -101,8 +101,7 @@ daemonize_set_user(void)
/* set gid */
if
(
user_gid
!=
(
gid_t
)
-
1
&&
user_gid
!=
getgid
()
&&
setgid
(
user_gid
)
==
-
1
)
{
FormatFatalSystemError
(
"Failed to set group %d"
,
(
int
)
user_gid
);
throw
FormatErrno
(
"Failed to set group %d"
,
(
int
)
user_gid
);
}
#ifdef HAVE_INITGROUPS
...
...
@@ -114,17 +113,16 @@ daemonize_set_user(void)
we are already this user */
user_uid
!=
getuid
()
&&
initgroups
(
user_name
,
user_gid
)
==
-
1
)
{
FormatFatalSystemError
(
"Failed to set supplementary groups "
"of user
\"
%s
\"
"
,
user_name
);
throw
FormatErrno
(
"Failed to set supplementary groups "
"of user
\"
%s
\"
"
,
user_name
);
}
#endif
/* set uid */
if
(
user_uid
!=
(
uid_t
)
-
1
&&
user_uid
!=
getuid
()
&&
setuid
(
user_uid
)
==
-
1
)
{
FormatFatalSystemError
(
"Failed to set user
\"
%s
\"
"
,
user_name
);
throw
FormatErrno
(
"Failed to set user
\"
%s
\"
"
,
user_name
);
}
}
...
...
@@ -133,7 +131,7 @@ daemonize_begin(bool detach)
{
/* release the current working directory */
if
(
chdir
(
"/"
)
<
0
)
FatalError
(
"problems changing to root directory"
);
throw
MakeErrno
(
"problems changing to root directory"
);
if
(
!
detach
)
/* the rest of this function deals with detaching the
...
...
@@ -152,13 +150,13 @@ daemonize_begin(bool detach)
int
fds
[
2
];
if
(
pipe
(
fds
)
<
0
)
FatalSystemError
(
"pipe() failed"
);
throw
MakeErrno
(
"pipe() failed"
);
/* move to a child process */
pid_t
pid
=
fork
();
if
(
pid
<
0
)
FatalSystemError
(
"fork() failed"
);
throw
MakeErrno
(
"fork() failed"
);
if
(
pid
==
0
)
{
/* in the child process */
...
...
@@ -193,11 +191,11 @@ daemonize_begin(bool detach)
int
status
;
pid_t
pid2
=
waitpid
(
pid
,
&
status
,
0
);
if
(
pid2
<
0
)
FatalSystemError
(
"waitpid() failed"
);
throw
MakeErrno
(
"waitpid() failed"
);
if
(
WIFSIGNALED
(
status
))
FormatFatalError
(
"MPD died from signal %d%s"
,
WTERMSIG
(
status
),
WCOREDUMP
(
status
)
?
" (core dumped)"
:
""
);
throw
FormatErrno
(
"MPD died from signal %d%s"
,
WTERMSIG
(
status
),
WCOREDUMP
(
status
)
?
" (core dumped)"
:
""
);
exit
(
WEXITSTATUS
(
status
));
}
...
...
@@ -223,7 +221,7 @@ daemonize_init(const char *user, const char *group, AllocatedPath &&_pidfile)
if
(
user
)
{
struct
passwd
*
pwd
=
getpwnam
(
user
);
if
(
pwd
==
nullptr
)
FormatFatal
Error
(
"no such user
\"
%s
\"
"
,
user
);
throw
FormatRuntime
Error
(
"no such user
\"
%s
\"
"
,
user
);
user_uid
=
pwd
->
pw_uid
;
user_gid
=
pwd
->
pw_gid
;
...
...
@@ -237,7 +235,8 @@ daemonize_init(const char *user, const char *group, AllocatedPath &&_pidfile)
if
(
group
)
{
struct
group
*
grp
=
getgrnam
(
group
);
if
(
grp
==
nullptr
)
FormatFatalError
(
"no such group
\"
%s
\"
"
,
group
);
throw
FormatRuntimeError
(
"no such group
\"
%s
\"
"
,
group
);
user_gid
=
grp
->
gr_gid
;
had_group
=
true
;
}
...
...
src/unix/Daemon.hxx
View file @
dfda0faa
...
...
@@ -48,11 +48,11 @@ daemonize_finish()
void
daemonize_kill
();
#else
#include
"system/FatalError.hxx"
#include
<stdexcept>
static
inline
void
daemonize_kill
()
{
FatalE
rror
(
"--kill is not available on WIN32"
);
throw
std
::
runtime_e
rror
(
"--kill is not available on WIN32"
);
}
#endif
...
...
src/unix/PidFile.hxx
View file @
dfda0faa
...
...
@@ -22,7 +22,7 @@
#include "fs/FileSystem.hxx"
#include "fs/AllocatedPath.hxx"
#include "
Log
.hxx"
#include "
system/Error
.hxx"
#include <assert.h>
#include <string.h>
...
...
@@ -41,8 +41,8 @@ public:
fd
=
OpenFile
(
path
,
O_WRONLY
|
O_CREAT
|
O_TRUNC
,
0666
).
Steal
();
if
(
fd
<
0
)
{
const
std
::
string
utf8
=
path
.
ToUTF8
();
FormatFatalSystemError
(
"Failed to create pid file
\"
%s
\"
"
,
utf8
.
c_str
());
throw
FormatErrno
(
"Failed to create pid file
\"
%s
\"
"
,
utf8
.
c_str
());
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment