Skip to content

Commit

Permalink
credential-cache--daemon: refactor check_socket_directory
Browse files Browse the repository at this point in the history
This function does an early return, and therefore has to
repeat its cleanup. We can stick the later bit of the
function into an "else" and avoid duplicating the shared
part (which will get bigger in a future patch).

Let's also rename the function to init_socket_directory. It
not only checks the directory but also creates it. Saying
"init" is more accurate.

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jon Griffiths authored and Junio C Hamano committed Feb 23, 2016
1 parent 326e5bc commit a6e5e28
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions credential-cache--daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ static const char permissions_advice[] =
"users may be able to read your cached credentials. Consider running:\n"
"\n"
" chmod 0700 %s";
static void check_socket_directory(const char *path)
static void init_socket_directory(const char *path)
{
struct stat st;
char *path_copy = xstrdup(path);
Expand All @@ -224,20 +224,18 @@ static void check_socket_directory(const char *path)
if (!stat(dir, &st)) {
if (st.st_mode & 077)
die(permissions_advice, dir);
free(path_copy);
return;
} else {
/*
* We must be sure to create the directory with the correct mode,
* not just chmod it after the fact; otherwise, there is a race
* condition in which somebody can chdir to it, sleep, then try to open
* our protected socket.
*/
if (safe_create_leading_directories_const(dir) < 0)
die_errno("unable to create directories for '%s'", dir);
if (mkdir(dir, 0700) < 0)
die_errno("unable to mkdir '%s'", dir);
}

/*
* We must be sure to create the directory with the correct mode,
* not just chmod it after the fact; otherwise, there is a race
* condition in which somebody can chdir to it, sleep, then try to open
* our protected socket.
*/
if (safe_create_leading_directories_const(dir) < 0)
die_errno("unable to create directories for '%s'", dir);
if (mkdir(dir, 0700) < 0)
die_errno("unable to mkdir '%s'", dir);
free(path_copy);
}

Expand All @@ -264,7 +262,7 @@ int main(int argc, const char **argv)
if (!socket_path)
usage_with_options(usage, options);

check_socket_directory(socket_path);
init_socket_directory(socket_path);
register_tempfile(&socket_file, socket_path);

if (ignore_sighup)
Expand Down

0 comments on commit a6e5e28

Please sign in to comment.