Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/fs/mkstemp/, src/: Move fmkomstemp() to separate files under lib/fs/mkstemp/, and split into mkomstemp() #1139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ libshadow_la_SOURCES = \
find_new_sub_gids.c \
find_new_sub_uids.c \
fputsx.c \
fs/mkstemp/fmkomstemp.c \
fs/mkstemp/fmkomstemp.h \
fs/mkstemp/mkomstemp.c \
fs/mkstemp/mkomstemp.h \
fs/readlink/areadlink.c \
fs/readlink/areadlink.h \
fs/readlink/readlinknul.c \
Expand Down
13 changes: 13 additions & 0 deletions lib/fs/mkstemp/fmkomstemp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#include <config.h>

#include "fs/mkstemp/fmkomstemp.h"

#include <stdio.h>
#include <sys/types.h>


extern inline FILE *fmkomstemp(char *template, unsigned int flags, mode_t m);
43 changes: 43 additions & 0 deletions lib/fs/mkstemp/fmkomstemp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_FS_MKSTEMP_FMKOMSTEMP_H_
#define SHADOW_INCLUDE_LIB_FS_MKSTEMP_FMKOMSTEMP_H_


#include <config.h>

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

#include "fs/mkstemp/mkomstemp.h"


inline FILE *fmkomstemp(char *template, unsigned int flags, mode_t m);


inline FILE *
fmkomstemp(char *template, unsigned int flags, mode_t m)
{
int fd;
FILE *fp;

fd = mkomstemp(template, flags, m);
if (fd == -1)
return NULL;

fp = fdopen(fd, "w");
if (fp == NULL)
goto fail;

return fp;
fail:
close(fd);
unlink(template);
return NULL;
}


#endif // include guard
12 changes: 12 additions & 0 deletions lib/fs/mkstemp/mkomstemp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#include <config.h>

#include "fs/mkstemp/mkomstemp.h"

#include <sys/types.h>


extern inline int mkomstemp(char *template, unsigned int flags, mode_t m);
40 changes: 40 additions & 0 deletions lib/fs/mkstemp/mkomstemp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_FS_MKSTEMP_MKOMSTEMP_H_
#define SHADOW_INCLUDE_LIB_FS_MKSTEMP_MKOMSTEMP_H_


#include <config.h>

#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>


inline int mkomstemp(char *template, unsigned int flags, mode_t m);


inline int
mkomstemp(char *template, unsigned int flags, mode_t m)
{
int fd;

fd = mkostemp(template, flags);
if (fd == -1)
return -1;

if (fchmod(fd, m) == -1)
goto fail;

return fd;
fail:
close(fd);
unlink(template);
return -1;
}


#endif // include guard
28 changes: 1 addition & 27 deletions src/useradd.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "chkname.h"
#include "defines.h"
#include "faillog.h"
#include "fs/mkstemp/fmkomstemp.h"
#include "getdef.h"
#include "groupio.h"
#include "nscd.h"
Expand Down Expand Up @@ -243,8 +244,6 @@ static void create_home (void);
static void create_mail (void);
static void check_uid_range(int rflg, uid_t user_id);

static FILE *fmkomstemp(char *template, unsigned int flags, mode_t m);


/*
* fail_exit - undo as much as possible
Expand Down Expand Up @@ -2744,28 +2743,3 @@ int main (int argc, char **argv)

return E_SUCCESS;
}


static FILE *
fmkomstemp(char *template, unsigned int flags, mode_t m)
{
int fd;
FILE *fp;

fd = mkostemp(template, flags);
if (fd == -1)
return NULL;

if (fchmod(fd, m) == -1)
goto fail;

fp = fdopen(fd, "w");
if (fp == NULL)
goto fail;

return fp;
fail:
close(fd);
unlink(template);
return NULL;
}
Loading