Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
6
66
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Eric Vidal
66
Commits
c2945521
Commit
c2945521
authored
5 years ago
by
Eric Vidal
Browse files
Options
Downloads
Patches
Plain Diff
do not crash on empty tree, less memory allocation
parent
913ae235
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/66/66-all.c
+60
-41
60 additions, 41 deletions
src/66/66-all.c
with
60 additions
and
41 deletions
src/66/66-all.c
+
60
−
41
View file @
c2945521
...
...
@@ -20,15 +20,13 @@
#include
<oblibs/error2.h>
#include
<oblibs/obgetopt.h>
#include
<oblibs/stralist.h>
#include
<oblibs/types.h>
#include
<oblibs/string.h>
#include
<oblibs/files.h>
#include
<oblibs/
directory
.h>
#include
<oblibs/
sastr
.h>
#include
<skalibs/buffer.h>
#include
<skalibs/stralloc.h>
#include
<skalibs/genalloc.h>
#include
<skalibs/djbunix.h>
#include
<66/constants.h>
...
...
@@ -64,7 +62,8 @@ int doit(char const *tree,char const *treename,char const *live, unsigned int wh
int
wstat
;
pid_t
pid
;
genalloc
ga
=
GENALLOC_ZERO
;
//stralist
//genalloc ga = GENALLOC_ZERO ; //stralist
stralloc
dirlist
=
STRALLOC_ZERO
;
char
ownerstr
[
UID_FMT
]
;
size_t
ownerlen
=
uid_fmt
(
ownerstr
,
owner
)
;
...
...
@@ -80,13 +79,14 @@ int doit(char const *tree,char const *treename,char const *live, unsigned int wh
src
[
livelen
+
SS_STATE_LEN
+
1
+
ownerlen
]
=
'/'
;
memcpy
(
src
+
livelen
+
SS_STATE_LEN
+
1
+
ownerlen
+
1
,
treename
,
treenamelen
)
;
src
[
livelen
+
SS_STATE_LEN
+
1
+
ownerlen
+
1
+
treenamelen
]
=
0
;
if
(
!
dir_get
(
&
ga
,
src
,
"init"
,
S_IFREG
))
strerr_diefu2sys
(
111
,
"get
state
file from directory: "
,
src
)
;
if
(
genalloc_len
(
stralist
,
&
ga
)
)
if
(
!
sastr_
dir_get
(
&
dirlist
,
src
,
"init"
,
S_IFREG
))
strerr_diefu2sys
(
111
,
"get
init
file from directory: "
,
src
)
;
if
(
dirlist
.
len
)
{
char
const
*
newargv
[
10
+
genalloc_len
(
stralist
,
&
ga
)]
;
size_t
i
=
0
,
len
=
sastr_len
(
&
dirlist
)
;
char
const
*
newargv
[
10
+
len
+
1
]
;
unsigned
int
m
=
0
;
char
fmt
[
UINT_FMT
]
;
fmt
[
uint_fmt
(
fmt
,
VERBOSITY
)]
=
0
;
...
...
@@ -107,8 +107,9 @@ int doit(char const *tree,char const *treename,char const *live, unsigned int wh
newargv
[
m
++
]
=
"-t"
;
newargv
[
m
++
]
=
treename
;
for
(
unsigned
int
i
=
0
;
i
<
genalloc_len
(
stralist
,
&
ga
)
;
i
++
)
newargv
[
m
++
]
=
gaistr
(
&
ga
,
i
)
;
len
=
dirlist
.
len
;
for
(;
i
<
len
;
i
+=
strlen
(
dirlist
.
s
+
i
)
+
1
)
newargv
[
m
++
]
=
dirlist
.
s
+
i
;
newargv
[
m
++
]
=
0
;
...
...
@@ -122,10 +123,10 @@ int doit(char const *tree,char const *treename,char const *live, unsigned int wh
}
else
VERBO1
strerr_warni3x
(
"Empty tree: "
,
treename
,
" -- nothing to do"
)
;
gen
alloc_
deep
free
(
stralist
,
&
ga
,
stra_free
)
;
str
alloc_free
(
&
dirlist
)
;
return
1
;
err:
gen
alloc_
deep
free
(
stralist
,
&
ga
,
stra_free
)
;
str
alloc_free
(
&
dirlist
)
;
return
0
;
}
...
...
@@ -154,7 +155,7 @@ int main(int argc, char const *const *argv,char const *const *envp)
int
r
,
what
,
wstat
,
shut
=
0
,
fd
;
pid_t
pid
;
uid_t
owner
;
size_t
statesize
,
statelen
,
pos
=
0
;
char
const
*
treename
=
NULL
;
stralloc
base
=
STRALLOC_ZERO
;
...
...
@@ -163,7 +164,7 @@ int main(int argc, char const *const *argv,char const *const *envp)
stralloc
tree
=
STRALLOC_ZERO
;
stralloc
live
=
STRALLOC_ZERO
;
stralloc
contents
=
STRALLOC_ZERO
;
gen
alloc
in
=
GEN
ALLOC_ZERO
;
//stralist
str
alloc
ugly
=
STR
ALLOC_ZERO
;
what
=
1
;
...
...
@@ -220,9 +221,6 @@ int main(int argc, char const *const *argv,char const *const *envp)
if
(
!
r
)
retstralloc
(
111
,
"main"
)
;
if
(
r
<
0
)
strerr_dief3x
(
111
,
"livetree: "
,
livetree
.
s
,
" must be an absolute path"
)
;
size_t
statesize
;
/** /system/state */
size_t
statelen
;
char
state
[
base
.
len
+
SS_SYSTEM_LEN
+
SS_STATE_LEN
+
1
]
;
memcpy
(
state
,
base
.
s
,
base
.
len
)
;
memcpy
(
state
+
base
.
len
,
SS_SYSTEM
,
SS_SYSTEM_LEN
)
;
...
...
@@ -234,30 +232,29 @@ int main(int argc, char const *const *argv,char const *const *envp)
if
(
r
<
0
)
strerr_dief2x
(
111
,
"conflict format for: "
,
state
)
;
if
(
!
r
)
strerr_diefu2sys
(
111
,
"find: "
,
state
)
;
statesize
=
file_get_size
(
state
)
;
r
=
openreadfileclose
(
state
,
&
contents
,
statesize
)
;
if
(
!
r
)
strerr_diefu2sys
(
111
,
"open: "
,
state
)
;
/** ensure that we have an empty line at the end of the string*/
if
(
!
stralloc_cats
(
&
contents
,
"
\n
"
))
retstralloc
(
111
,
"main"
)
;
if
(
!
stralloc_0
(
&
contents
))
retstralloc
(
111
,
"main"
)
;
/** only one tree?*/
if
(
treename
)
{
if
(
!
stra_add
(
&
in
,
treename
))
strerr_diefu3x
(
111
,
"add: "
,
treename
,
" as tree to start"
)
;
if
(
!
stralloc_cats
(
&
contents
,
treename
))
strerr_diefu3x
(
111
,
"add: "
,
treename
,
" as tree to start"
)
;
if
(
!
stralloc_0
(
&
contents
))
retstralloc
(
111
,
"main"
)
;
}
else
{
if
(
!
clean_val
(
&
in
,
contents
.
s
))
strerr_diefu2x
(
111
,
"clean: "
,
contents
.
s
)
;
}
statesize
=
file_get_size
(
state
)
;
r
=
openreadfileclose
(
state
,
&
contents
,
statesize
)
;
if
(
!
r
)
strerr_diefu2sys
(
111
,
"open: "
,
state
)
;
if
(
!
in
.
len
)
{
strerr_warni1x
(
"nothing to do"
)
;
goto
end
;
/** ensure that we have an empty line at the end of the string*/
if
(
!
stralloc_cats
(
&
contents
,
"
\n
"
))
retstralloc
(
111
,
"main"
)
;
if
(
!
stralloc_0
(
&
contents
))
retstralloc
(
111
,
"main"
)
;
if
(
!
sastr_clean_element
(
&
contents
))
{
strerr_warni1x
(
"nothing to do"
)
;
goto
end
;
}
}
if
(
shut
)
...
...
@@ -282,11 +279,11 @@ int main(int argc, char const *const *argv,char const *const *envp)
else
redir_fd
()
;
}
for
(
unsigned
int
i
=
0
;
i
<
genalloc_len
(
stralist
,
&
in
)
;
i
++
)
for
(
;
pos
<
contents
.
len
;
pos
+=
strlen
(
contents
.
s
+
pos
)
+
1
)
{
tree
.
len
=
0
;
char
*
treename
=
gaistr
(
&
in
,
i
)
;
char
*
treename
=
contents
.
s
+
pos
;
if
(
!
stralloc_cats
(
&
tree
,
treename
))
retstralloc
(
111
,
"main"
)
;
if
(
!
stralloc_0
(
&
tree
))
retstralloc
(
111
,
"main"
)
;
...
...
@@ -296,7 +293,29 @@ int main(int argc, char const *const *argv,char const *const *envp)
if
(
!
tree_get_permissions
(
tree
.
s
,
owner
))
strerr_dief2x
(
110
,
"You're not allowed to use the tree: "
,
tree
.
s
)
;
/* we need to make an ugly check here, a tree might be empty.
* 66-init will not complain about this but doit function will do.
* So, we need to scan the tree before trying to start any service from it.
* This check is a temporary one because the tree dependencies feature
* will come on a near future and it will change this default behavior */
size_t
newlen
=
tree
.
len
;
if
(
!
stralloc_cats
(
&
tree
,
SS_SVDIRS
SS_SVC
))
retstralloc
(
111
,
"main"
)
;
if
(
!
stralloc_0
(
&
tree
))
retstralloc
(
111
,
"main"
)
;
if
(
!
sastr_dir_get
(
&
ugly
,
tree
.
s
,
""
,
S_IFDIR
))
strerr_diefu2x
(
111
,
"get classic services from: "
,
tree
.
s
)
;
if
(
!
ugly
.
len
)
{
tree
.
len
=
newlen
;
ugly
.
len
=
0
;
if
(
!
stralloc_cats
(
&
tree
,
SS_SVDIRS
SS_DB
SS_SRC
))
retstralloc
(
111
,
"main"
)
;
if
(
!
stralloc_0
(
&
tree
))
retstralloc
(
111
,
"main"
)
;
if
(
!
sastr_dir_get
(
&
ugly
,
tree
.
s
,
SS_MASTER
+
1
,
S_IFDIR
))
strerr_diefu2sys
(
111
,
"get rc services from: "
,
tree
.
s
)
;
if
(
!
ugly
.
len
)
{
strerr_warni3x
(
"empty tree: "
,
treename
,
" -- nothing to do"
)
;
goto
end
;
}
}
if
(
what
)
{
char
const
*
newargv
[
9
]
;
...
...
@@ -347,7 +366,7 @@ int main(int argc, char const *const *argv,char const *const *envp)
stralloc_free
(
&
livetree
)
;
stralloc_free
(
&
scandir
)
;
stralloc_free
(
&
contents
)
;
gen
alloc_
deep
free
(
stralist
,
&
in
,
stra_free
)
;
str
alloc_free
(
&
ugly
)
;
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