Skip to content
This repository was archived by the owner on Oct 23, 2021. It is now read-only.

Commit 7eec35c

Browse files
committed
version 0.26 More robust code (maybe)
1 parent e3e79ce commit 7eec35c

File tree

19 files changed

+390
-286
lines changed

19 files changed

+390
-286
lines changed

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
v0.26 2016-05-19
2+
* More robust code (maybe)
3+
14
v0.25 2016-02-24
25
* More accurate local device identification
36

TODO

Lines changed: 0 additions & 1 deletion
This file was deleted.

build.sh

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,31 @@
33
# This to build project
44
# ==============================================================================
55

6-
curdir=$(dirname $(readlink -f $0))
7-
builddir=$curdir/build
6+
RDIR=$(dirname $(readlink -f $0))
7+
BUILDDIR=${RDIR}/build
88

9-
function build {
10-
mkdir -p $builddir \
11-
&& cd $builddir \
9+
function build ()
10+
{
11+
mkdir -p $BUILDDIR \
12+
&& cd $BUILDDIR \
1213
&& cmake -DCMAKE_BUILD_TYPE=Release .. \
13-
&& make -j4
14+
&& cmake --build . --config Release -- -j4
1415

1516
return $?
1617
}
1718

18-
function install {
19-
cd $builddir \
19+
function install ()
20+
{
21+
cd $BUILDDIR \
2022
&& sudo make install -j4
2123

2224
return $?
2325
}
2426

2527
build
2628

27-
if [[ 0 -eq $? && 'install' == $1 ]]; then
29+
if [[ 0 -eq $? && 'install' == $1 ]]
30+
then
2831
install
2932
fi
3033

fingerprint

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub rsa4096/B66CC194 2016-04-15 [SC]
2+
Key fingerprint = 4444 9495 01B3 CC2A 7657 9C08 25FF D92A B66C C194
3+
uid [ultimate] Arondight <shell_way@foxmail.com>
4+
sub rsa4096/F96E3CB7 2016-04-15 [E]
5+

install.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,14 @@
33
# This to build project and do install
44
# ==============================================================================
55

6-
source ./build.sh install
6+
RDIR=$(dirname $(readlink -f $0))
7+
BUILD_SH="${RDIR}/build.sh"
8+
BUILD_ARGS="install"
9+
10+
if [[ -x $BUILD_SH ]]
11+
then
12+
command $BUILD_SH $BUILD_ARGS
13+
fi
14+
15+
exit $?
716

src/assert.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* ========================================================================== *
2+
* Copyright (c) 2015-2016 秦凡东(Qin Fandong)
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
* ========================================================================== *
17+
* Do assert
18+
* ========================================================================== */
19+
#ifndef __ASSERT_H__
20+
#define __ASSERT_H__
21+
22+
#include <libgen.h>
23+
#include "say.h"
24+
25+
#define LOG(m,t,f,...) \
26+
((say ((m), (t), "ASSERT FAILED in %4d of %s:\t(%s) - ", \
27+
__LINE__, basename (__FILE__), __FUNCTION__)), \
28+
(say ((m), (t), (f), ##__VA_ARGS__)))
29+
30+
#define ASSERT_ABORT(e,s) \
31+
((e) ? (void)0 : ((LOG (mode, MSG_E, (s))), abort ()))
32+
33+
#define ASSERT_LOG(e,s) \
34+
((e) ? (void)0 : ((LOG (mode, MSG_E, (s)))))
35+
36+
#define ASSERT_RETURN(e,s,r) \
37+
{ if (!(e)) { LOG (mode, MSG_E, (s)); return (r); } }
38+
39+
#endif
40+

src/chomp.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@
1717
* Delete all of '\n' at end of string
1818
* ========================================================================== */
1919
#include <string.h>
20+
#include "assert.h"
2021

2122
int
2223
chomp (char * const string)
2324
{
24-
int index;
25+
int index = 0;
26+
saymode_t mode = MODE_UNKNOWN;
2527

26-
if (!string)
27-
{
28-
return -1;
29-
}
28+
sayMode (&mode);
29+
30+
ASSERT_RETURN (string, "string is NULL.\n", -1);
3031

3132
for (index = strlen (string) - 1; index > -1; --index)
3233
{

src/daemonize.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <syslog.h>
2727
#include <sys/stat.h>
2828
#include <sys/resource.h>
29+
#include "assert.h"
2930

3031
enum {
3132
STATUS_OK = 1,
@@ -35,17 +36,16 @@ enum {
3536
/* ========================================================================== *
3637
* Set mask to 0, let a new file may has any privilege
3738
* ========================================================================== */
38-
int
39+
static int
3940
setMask (void)
4041
{
41-
umask (0);
42-
return STATUS_OK;
42+
return umask (0) ? STATUS_ERROR : STATUS_OK;
4343
}
4444

4545
/* ========================================================================== *
4646
* Change work directory to root
4747
* ========================================================================== */
48-
int
48+
static int
4949
chrootdir (void)
5050
{
5151
const char * const rootdir = "/";
@@ -62,10 +62,10 @@ chrootdir (void)
6262
/* ========================================================================== *
6363
* Create a new session
6464
* ========================================================================== */
65-
int
65+
static int
6666
newSession (void)
6767
{
68-
pid_t pid;
68+
pid_t pid = 0;
6969

7070
if ((pid = fork ()) < 0)
7171
{
@@ -90,11 +90,13 @@ newSession (void)
9090
/* ========================================================================== *
9191
* Disable controlling terminal
9292
* ========================================================================== */
93-
int
93+
static int
9494
noTTY (void)
9595
{
96-
struct sigaction action;
97-
pid_t pid;
96+
struct sigaction action = { 0 };
97+
pid_t pid = 0;
98+
99+
memset (&action, 0, sizeof (struct sigaction));
98100

99101
action.sa_handler = SIG_IGN;
100102
sigemptyset (&action.sa_mask);
@@ -123,18 +125,20 @@ noTTY (void)
123125
/* ========================================================================== *
124126
* Close all file descriptor
125127
* ========================================================================== */
126-
int
128+
static int
127129
closeFD (void)
128130
{
129-
struct rlimit limit;
131+
struct rlimit limit = { 0 };
132+
int fd = 0;
133+
134+
memset (&limit, 0, sizeof (struct rlimit));
130135

131136
if (getrlimit (RLIMIT_NOFILE, &limit) < 0)
132137
{
133138
perror ("getrlimit falied:");
134139
return STATUS_ERROR;
135140
}
136141

137-
int fd = 0;
138142
while (fd < (int)(RLIM_INFINITY == limit.rlim_max ? 1024 : limit.rlim_max))
139143
{
140144
close (fd++);
@@ -147,11 +151,11 @@ closeFD (void)
147151
* Reopen stdin, stdout and stderr to /dev/null
148152
* Invoke this after closeFD
149153
* ========================================================================== */
150-
int
154+
static int
151155
reopen (void)
152156
{
153157
const char * const nullFile = "/dev/null";
154-
int in, out, err;
158+
int in = 0, out = 0, err = 0;
155159

156160
in = open (nullFile, O_RDWR);
157161
out = dup (in);
@@ -169,7 +173,7 @@ reopen (void)
169173
/* ========================================================================== *
170174
* Init log
171175
* ========================================================================== */
172-
int
176+
static int
173177
initLog (const char * const ident)
174178
{
175179
openlog (ident, LOG_CONS, LOG_DAEMON);
@@ -185,6 +189,8 @@ daemonize (const char * const ident)
185189
{
186190
int status = 0;
187191

192+
/* Also work well when ident is NULL so here no need to check */
193+
188194
status |= setMask ();
189195
status |= newSession ();
190196
status |= noTTY ();

src/devs.c

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "say.h"
3131
#include "find.h"
3232
#include "readfile.h"
33+
#include "assert.h"
3334
#include "config.h"
3435

3536
static char **list = NULL;
@@ -38,21 +39,23 @@ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
3839
/* ========================================================================== *
3940
* Return loacl devices
4041
* ========================================================================== */
41-
int
42+
static int
4243
localDevs (void)
4344
{
4445
char **text = NULL;
4546
char *pos = NULL;
46-
char path[MAXPATHLEN + 1], buff[MAXPATHLEN + 1];
47-
saymode_t mode;
48-
int uuid2path, label2path;
49-
int index, index2;
50-
int no;
51-
int len;
52-
int chr;
47+
char path[MAXPATHLEN + 1] = { 0 }, buff[MAXPATHLEN + 1] = { 0 };
48+
saymode_t mode = MODE_UNKNOWN;
49+
int uuid2path = 0, label2path = 0;
50+
int index = 0, index2 = 0;
51+
int no = 0;
52+
int len = 0;
53+
int chr = 0;
5354
const char uuidInterface[] = "/dev/disk/by-uuid";
5455
const char labelInterface[] = "/dev/disk/by-label";
5556

57+
memset (path, 0, sizeof (path));
58+
memset (buff, 0, sizeof (buff));
5659
sayMode (&mode);
5760

5861
if (list)
@@ -181,17 +184,20 @@ localDevs (void)
181184
/* ========================================================================== *
182185
* Determine whether a devPath is a local device
183186
* ========================================================================== */
184-
int
187+
static int
185188
isLocalDev (const char * const devPath)
186189
{
187-
char *pattern;
188-
char buff[MAXPATHLEN + 1];
189-
saymode_t mode;
190-
int index;
191-
int status;
190+
char *pattern = NULL;
191+
char buff[MAXPATHLEN + 1] = { 0 };
192+
saymode_t mode = MODE_UNKNOWN;
193+
int index = 0;
194+
int status = 0;
192195

196+
memset (buff, 0, sizeof (buff));
193197
sayMode (&mode);
194198

199+
ASSERT_RETURN (devPath, "devPath is NULL.\n", -1);
200+
195201
strncpy (buff, devPath, MAXPATHLEN);
196202
pattern = basename (buff);
197203

@@ -204,16 +210,6 @@ isLocalDev (const char * const devPath)
204210
}
205211
}
206212

207-
/* TODO: Use a faster way to search FSTAB { *
208-
if (-1 == (status = find (FSTAB, pattern)))
209-
{
210-
say (mode, MSG_E, "find failed\n");
211-
return -1;
212-
}
213-
214-
return status;
215-
* } */
216-
217213
localDevs ();
218214

219215
status = 0;
@@ -233,17 +229,21 @@ isLocalDev (const char * const devPath)
233229
int
234230
devs (char ***addr)
235231
{
236-
DIR *dh;
237-
struct dirent *dir;
238-
char **uuids;
239-
char buff[MAXPATHLEN + 1], dev[MAXPATHLEN + 1];
240-
size_t len;
241-
saymode_t mode;
242-
int no, count;
232+
DIR *dh = NULL;
233+
struct dirent *dir = NULL;
234+
char **uuids = NULL;
235+
char buff[MAXPATHLEN + 1] = { 0 }, dev[MAXPATHLEN + 1] = { 0 };
236+
size_t len = 0;
237+
saymode_t mode = 0;
238+
int no = 0, count = 0;
243239
const char interface[] = "/dev/disk/by-uuid";
244240

241+
memset (buff, 0, sizeof (buff));
242+
memset (dev, 0, sizeof (dev));
245243
sayMode (&mode);
246244

245+
ASSERT_RETURN (addr, "addr is NULL.\n", -1);
246+
247247
if (!(dh = opendir (interface)))
248248
{
249249
say (mode, MSG_E, "opendir failed: %s\n", strerror (errno));

0 commit comments

Comments
 (0)