Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
batteryd
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SystemCshee
batteryd
Commits
08f20cc3
Commit
08f20cc3
authored
Feb 9, 2023
by
Zámolyi Csaba Ferenc
Browse files
Options
Downloads
Patches
Plain Diff
Added basic daemonize
parent
ea50784b
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+2
-0
2 additions, 0 deletions
.gitignore
CMakeLists.txt
+6
-0
6 additions, 0 deletions
CMakeLists.txt
src/daemonutils.cpp
+51
-0
51 additions, 0 deletions
src/daemonutils.cpp
src/main.cpp
+6
-0
6 additions, 0 deletions
src/main.cpp
with
65 additions
and
0 deletions
.gitignore
+
2
−
0
View file @
08f20cc3
cmake-build-debug/
.idea/
\ No newline at end of file
This diff is collapsed.
Click to expand it.
CMakeLists.txt
0 → 100644
+
6
−
0
View file @
08f20cc3
cmake_minimum_required
(
VERSION 3.25
)
project
(
batteryd
)
set
(
CMAKE_CXX_STANDARD 20
)
add_executable
(
batteryd src/main.cpp src/daemonutils.cpp
)
This diff is collapsed.
Click to expand it.
src/daemonutils.cpp
0 → 100644
+
51
−
0
View file @
08f20cc3
/*
* Created by: Cshee, 2023-02-08
* Project: SystemCshee / batteryd
* License: GNU GPL v3.0
*/
#include
<cstdlib>
#include
<unistd.h>
#include
<csignal>
#include
<sys/types.h>
#include
<sys/stat.h>
#include
<syslog.h>
#include
<cstdio>
static
void
daemonize
()
{
// First fork to lose parent to PID1, then create own session
pid_t
pid
;
pid
=
fork
();
if
(
pid
<
0
)
exit
(
EXIT_FAILURE
);
if
(
pid
>
0
)
exit
(
EXIT_SUCCESS
);
if
(
setsid
()
<
0
)
exit
(
EXIT_FAILURE
);
// Second fork to lose session control and prevent controlling tty
pid
=
fork
();
if
(
pid
<
0
)
exit
(
EXIT_FAILURE
);
if
(
pid
>
0
)
exit
(
EXIT_SUCCESS
);
// Set new file permissions and work directory
umask
(
0
);
chdir
(
"/"
);
// Close all open file handles, then assign /dev/null to stdin, -out, -err
for
(
int
i
=
0
;
i
<
sysconf
(
_SC_OPEN_MAX
);
++
i
)
close
(
i
);
stdin
=
fopen
(
"/dev/null"
,
"r"
);
stdout
=
fopen
(
"/dev/null"
,
"w+"
);
stderr
=
fopen
(
"/dev/null"
,
"w+"
);
}
static
void
setup
()
{
signal
(
SIGCHLD
,
SIG_IGN
);
signal
(
SIGHUP
,
SIG_IGN
);
/* Open the log file */
openlog
(
"systemcshee-batteryd"
,
LOG_PID
,
LOG_DAEMON
);
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/main.cpp
0 → 100644
+
6
−
0
View file @
08f20cc3
#include
<iostream>
int
main
()
{
std
::
cout
<<
"Hello, World!"
<<
std
::
endl
;
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment