Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
lxc
Project
Overview
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
lxc
Commits
1dc4a3b5
Unverified
Commit
1dc4a3b5
authored
Jun 29, 2017
by
Christian Brauner
Committed by
Stéphane Graber
Jul 16, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
utils: lxc_make_abstract_socket_name()
Signed-off-by:
Christian Brauner
<
christian.brauner@ubuntu.com
>
parent
f6aa6929
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
59 deletions
+69
-59
commands.c
src/lxc/commands.c
+4
-59
utils.c
src/lxc/utils.c
+61
-0
utils.h
src/lxc/utils.h
+4
-0
No files found.
src/lxc/commands.c
View file @
1dc4a3b5
...
...
@@ -28,7 +28,6 @@
#include <fcntl.h>
#include <poll.h>
#include <sys/socket.h>
#include <inttypes.h>
#include <sys/un.h>
#include <sys/param.h>
#include <malloc.h>
...
...
@@ -76,62 +75,6 @@
lxc_log_define
(
lxc_commands
,
lxc
);
static
int
fill_sock_name
(
char
*
path
,
int
len
,
const
char
*
lxcname
,
const
char
*
lxcpath
,
const
char
*
hashed_sock_name
)
{
const
char
*
name
;
char
*
tmppath
;
size_t
tmplen
;
uint64_t
hash
;
int
ret
;
name
=
lxcname
;
if
(
!
name
)
name
=
""
;
if
(
hashed_sock_name
!=
NULL
)
{
ret
=
snprintf
(
path
,
len
,
"lxc/%s/command"
,
hashed_sock_name
);
if
(
ret
<
0
||
ret
>=
len
)
{
ERROR
(
"Error writing to command sock path"
);
return
-
1
;
}
return
0
;
}
if
(
!
lxcpath
)
{
lxcpath
=
lxc_global_config_value
(
"lxc.lxcpath"
);
if
(
!
lxcpath
)
{
ERROR
(
"Out of memory getting lxcpath"
);
return
-
1
;
}
}
ret
=
snprintf
(
path
,
len
,
"%s/%s/command"
,
lxcpath
,
name
);
if
(
ret
<
0
)
{
ERROR
(
"Error writing to command sock path"
);
return
-
1
;
}
if
(
ret
<
len
)
return
0
;
/* ret >= len; lxcpath or name is too long. hash both */
tmplen
=
strlen
(
name
)
+
strlen
(
lxcpath
)
+
2
;
tmppath
=
alloca
(
tmplen
);
ret
=
snprintf
(
tmppath
,
tmplen
,
"%s/%s"
,
lxcpath
,
name
);
if
(
ret
<
0
||
ret
>=
tmplen
)
{
ERROR
(
"memory error"
);
return
-
1
;
}
hash
=
fnv_64a_buf
(
tmppath
,
ret
,
FNV1A_64_INIT
);
ret
=
snprintf
(
path
,
len
,
"lxc/%016"
PRIx64
"/command"
,
hash
);
if
(
ret
<
0
||
ret
>=
len
)
{
ERROR
(
"Command socket name too long"
);
return
-
1
;
}
return
0
;
}
static
const
char
*
lxc_cmd_str
(
lxc_cmd_t
cmd
)
{
static
const
char
*
const
cmdname
[
LXC_CMD_MAX
]
=
{
...
...
@@ -300,7 +243,8 @@ static int lxc_cmd(const char *name, struct lxc_cmd_rr *cmd, int *stopped,
* because we print the sockname out sometimes.
*/
len
=
sizeof
(
path
)
-
2
;
if
(
fill_sock_name
(
offset
,
len
,
name
,
lxcpath
,
hashed_sock_name
))
if
(
lxc_make_abstract_socket_name
(
offset
,
len
,
name
,
lxcpath
,
hashed_sock_name
,
"command"
))
return
-
1
;
sock
=
lxc_abstract_unix_connect
(
path
);
...
...
@@ -1156,7 +1100,8 @@ int lxc_cmd_init(const char *name, struct lxc_handler *handler,
* because we print the sockname out sometimes.
*/
len
=
sizeof
(
path
)
-
2
;
if
(
fill_sock_name
(
offset
,
len
,
name
,
lxcpath
,
NULL
))
if
(
lxc_make_abstract_socket_name
(
offset
,
len
,
name
,
lxcpath
,
NULL
,
"command"
))
return
-
1
;
fd
=
lxc_abstract_unix_open
(
path
,
SOCK_STREAM
,
0
);
...
...
src/lxc/utils.c
View file @
1dc4a3b5
...
...
@@ -23,11 +23,13 @@
#include "config.h"
#define __STDC_FORMAT_MACROS
/* Required for PRIu64 to work. */
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <inttypes.h>
#include <libgen.h>
#include <stddef.h>
#include <stdio.h>
...
...
@@ -2336,3 +2338,62 @@ int run_command(char *buf, size_t buf_size, int (*child_fn)(void *), void *args)
return
fret
;
}
int
lxc_make_abstract_socket_name
(
char
*
path
,
int
len
,
const
char
*
lxcname
,
const
char
*
lxcpath
,
const
char
*
hashed_sock_name
,
const
char
*
suffix
)
{
const
char
*
name
;
char
*
tmppath
;
size_t
tmplen
;
uint64_t
hash
;
int
ret
;
name
=
lxcname
;
if
(
!
name
)
name
=
""
;
if
(
hashed_sock_name
!=
NULL
)
{
ret
=
snprintf
(
path
,
len
,
"lxc/%s/%s"
,
hashed_sock_name
,
suffix
);
if
(
ret
<
0
||
ret
>=
len
)
{
ERROR
(
"Failed to create abstract socket name"
);
return
-
1
;
}
return
0
;
}
if
(
!
lxcpath
)
{
lxcpath
=
lxc_global_config_value
(
"lxc.lxcpath"
);
if
(
!
lxcpath
)
{
ERROR
(
"Failed to allocate memory"
);
return
-
1
;
}
}
ret
=
snprintf
(
path
,
len
,
"%s/%s/%s"
,
lxcpath
,
name
,
suffix
);
if
(
ret
<
0
)
{
ERROR
(
"Failed to create abstract socket name"
);
return
-
1
;
}
if
(
ret
<
len
)
return
0
;
/* ret >= len; lxcpath or name is too long. hash both */
tmplen
=
strlen
(
name
)
+
strlen
(
lxcpath
)
+
2
;
tmppath
=
alloca
(
tmplen
);
ret
=
snprintf
(
tmppath
,
tmplen
,
"%s/%s"
,
lxcpath
,
name
);
if
(
ret
<
0
||
ret
>=
tmplen
)
{
ERROR
(
"Failed to create abstract socket name"
);
return
-
1
;
}
hash
=
fnv_64a_buf
(
tmppath
,
ret
,
FNV1A_64_INIT
);
ret
=
snprintf
(
path
,
len
,
"lxc/%016"
PRIx64
"/%s"
,
hash
,
suffix
);
if
(
ret
<
0
||
ret
>=
len
)
{
ERROR
(
"Failed to create abstract socket name"
);
return
-
1
;
}
return
0
;
}
src/lxc/utils.h
View file @
1dc4a3b5
...
...
@@ -374,5 +374,9 @@ int lxc_unstack_mountpoint(const char *path, bool lazy);
* @param[in] args Arguments to be passed to child_fn.
*/
int
run_command
(
char
*
buf
,
size_t
buf_size
,
int
(
*
child_fn
)(
void
*
),
void
*
args
);
int
lxc_make_abstract_socket_name
(
char
*
path
,
int
len
,
const
char
*
lxcname
,
const
char
*
lxcpath
,
const
char
*
hashed_sock_name
,
const
char
*
suffix
);
#endif
/* __LXC_UTILS_H */
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