* [PATCHv2 1/2] static-nodes: don't fail if modules.devname not found
@ 2013-07-14 13:13 Tom Gundersen
2013-07-14 13:13 ` [PATCHv2 2/2] static-nodes: create parent directories of output file Tom Gundersen
0 siblings, 1 reply; 3+ messages in thread
From: Tom Gundersen @ 2013-07-14 13:13 UTC (permalink / raw)
To: linux-modules; +Cc: Tom Gundersen
In containers/VM's/initrd one might not have installed any modules and accompanying modules.devname
Don't fail if this is the case, just warn.
When used in systemd this means we don't get a failing unit on booting containers.
---
v2: simplify based on feedback from Dave
tools/static-nodes.c | 31 +++++++++++++++++++------------
1 file changed, 19 insertions(+), 12 deletions(-)
diff --git a/tools/static-nodes.c b/tools/static-nodes.c
index 96bb71e..351c02b 100644
--- a/tools/static-nodes.c
+++ b/tools/static-nodes.c
@@ -155,7 +155,8 @@ static int do_static_nodes(int argc, char *argv[])
{
struct utsname kernel;
char modules[PATH_MAX];
- FILE *in = NULL, *out = stdout;
+ const char *output = "/dev/stdout";
+ FILE *in = NULL, *out = NULL;
const struct static_nodes_format *format = &static_nodes_format_human;
char buf[4096];
int ret = EXIT_SUCCESS;
@@ -170,13 +171,7 @@ static int do_static_nodes(int argc, char *argv[])
}
switch (c) {
case 'o':
- out = fopen(optarg, "we");
- if (out == NULL) {
- fprintf(stderr, "Error: could not create %s!\n",
- optarg);
- ret = EXIT_FAILURE;
- goto finish;
- }
+ output = optarg;
break;
case 'f':
valid = 0;
@@ -217,12 +212,24 @@ static int do_static_nodes(int argc, char *argv[])
goto finish;
}
- snprintf(modules, sizeof(modules), "/lib/modules/%s/modules.devname",
- kernel.release);
+ snprintf(modules, sizeof(modules), "/lib/modules/%s/modules.devname", kernel.release);
in = fopen(modules, "re");
if (in == NULL) {
- fprintf(stderr, "Error: could not open /lib/modules/%s/modules.devname - %m\n",
- kernel.release);
+ if (errno == ENOENT) {
+ fprintf(stderr, "Warning: /lib/modules/%s/modules.devname not found - ignoring\n",
+ kernel.release);
+ ret = EXIT_SUCCESS;
+ } else {
+ fprintf(stderr, "Error: could not open /lib/modules/%s/modules.devname - %m\n",
+ kernel.release);
+ ret = EXIT_FAILURE;
+ }
+ goto finish;
+ }
+
+ out = fopen(output, "we");
+ if (out == NULL) {
+ fprintf(stderr, "Error: could not create %s - %m\n", output);
ret = EXIT_FAILURE;
goto finish;
}
--
1.8.3.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCHv2 2/2] static-nodes: create parent directories of output file
2013-07-14 13:13 [PATCHv2 1/2] static-nodes: don't fail if modules.devname not found Tom Gundersen
@ 2013-07-14 13:13 ` Tom Gundersen
2013-07-14 14:57 ` Lucas De Marchi
0 siblings, 1 reply; 3+ messages in thread
From: Tom Gundersen @ 2013-07-14 13:13 UTC (permalink / raw)
To: linux-modules; +Cc: Tom Gundersen
Allows us to drop call to "mkdir -p" from the systemd service file.
---
v2: rebase
Makefile.am | 3 +-
tools/mkdir.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++
tools/mkdir.h | 25 ++++++++++++++++
tools/static-nodes.c | 14 +++++++--
4 files changed, 119 insertions(+), 4 deletions(-)
create mode 100644 tools/mkdir.c
create mode 100644 tools/mkdir.h
diff --git a/Makefile.am b/Makefile.am
index 0ed944c..ab49dfd 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -111,7 +111,8 @@ tools_kmod_SOURCES = tools/kmod.c tools/kmod.h tools/lsmod.c \
tools/rmmod.c tools/insmod.c \
tools/modinfo.c tools/modprobe.c \
tools/depmod.c tools/log.h tools/log.c \
- tools/static-nodes.c
+ tools/static-nodes.c tools/mkdir.c \
+ tools/mkdir.h
tools_kmod_LDADD = libkmod/libkmod-util.la \
libkmod/libkmod-internal.la
diff --git a/tools/mkdir.c b/tools/mkdir.c
new file mode 100644
index 0000000..9935366
--- /dev/null
+++ b/tools/mkdir.c
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2012 Lucas De Marchi <lucas.de.marchi@gmail.com
+ * Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include "mkdir.h"
+
+static int mkdir_p(char *path, mode_t mode)
+{
+ char *end = path + strlen(path);
+ struct stat st;
+ int r;
+
+ /*
+ * don't try to overwrite existing file, succeed if it is a directory,
+ * fail otherwise
+ */
+ if (stat(path, &st) >= 0) {
+ if (S_ISDIR(st.st_mode)) {
+ return 0;
+ } else
+ return -ENOTDIR;
+ }
+
+ /* Find the last '/' */
+ while (end > path && *end != '/')
+ end--;
+
+ /* discard extra '/' */
+ while (end > path && *(end - 1) == '/')
+ end--;
+
+ /* if a parent directory was found, create it recursively */
+ if (end != path) {
+ *end = '\0';
+ r = mkdir_p(path, mode);
+ *end = '/';
+
+ if (r < 0)
+ return -errno;
+ }
+
+ /* create the desired directory */
+ return mkdir(path, mode);
+}
+
+int mkdir_parents(const char *path, mode_t mode) {
+ char *dirname = strdupa(path);
+ char *offset = strrchr(dirname, '/');
+
+ /* no parent directories */
+ if (!offset) {
+ return 0;
+ }
+
+ /* compute the dirname */
+ while (*(offset - 1) == '/')
+ offset--;
+ *offset = '\0';
+
+ return mkdir_p(dirname, mode);
+}
diff --git a/tools/mkdir.h b/tools/mkdir.h
new file mode 100644
index 0000000..5327fbe
--- /dev/null
+++ b/tools/mkdir.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2012 Lucas De Marchi <lucas.de.marchi@gmail.com>
+ * Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#pragma once
+
+#include <sys/stat.h>
+#include <sys/types.h>
+
+int mkdir_parents(const char *path, mode_t mode);
diff --git a/tools/static-nodes.c b/tools/static-nodes.c
index 351c02b..faccefc 100644
--- a/tools/static-nodes.c
+++ b/tools/static-nodes.c
@@ -32,6 +32,8 @@
#include <sys/types.h>
#include "libkmod-util.h"
+#include "mkdir.h"
+
#include "kmod.h"
struct static_nodes_format {
@@ -154,12 +156,11 @@ static void help(void)
static int do_static_nodes(int argc, char *argv[])
{
struct utsname kernel;
- char modules[PATH_MAX];
+ char modules[PATH_MAX], buf[4096];
const char *output = "/dev/stdout";
FILE *in = NULL, *out = NULL;
const struct static_nodes_format *format = &static_nodes_format_human;
- char buf[4096];
- int ret = EXIT_SUCCESS;
+ int r, ret = EXIT_SUCCESS;
for (;;) {
int c, idx = 0, valid;
@@ -227,6 +228,13 @@ static int do_static_nodes(int argc, char *argv[])
goto finish;
}
+ r = mkdir_parents(output, 0755);
+ if (r < 0) {
+ fprintf(stderr, "Error: could not create parent directory for %s - %m.\n", output);
+ ret = EXIT_FAILURE;
+ goto finish;
+ }
+
out = fopen(output, "we");
if (out == NULL) {
fprintf(stderr, "Error: could not create %s - %m\n", output);
--
1.8.3.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCHv2 2/2] static-nodes: create parent directories of output file
2013-07-14 13:13 ` [PATCHv2 2/2] static-nodes: create parent directories of output file Tom Gundersen
@ 2013-07-14 14:57 ` Lucas De Marchi
0 siblings, 0 replies; 3+ messages in thread
From: Lucas De Marchi @ 2013-07-14 14:57 UTC (permalink / raw)
To: Tom Gundersen; +Cc: linux-modules
On Sun, Jul 14, 2013 at 10:13 AM, Tom Gundersen <teg@jklm.no> wrote:
> Allows us to drop call to "mkdir -p" from the systemd service file.
> ---
>
> v2: rebase
>
> Makefile.am | 3 +-
> tools/mkdir.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> tools/mkdir.h | 25 ++++++++++++++++
> tools/static-nodes.c | 14 +++++++--
> 4 files changed, 119 insertions(+), 4 deletions(-)
> create mode 100644 tools/mkdir.c
> create mode 100644 tools/mkdir.h
>
> diff --git a/Makefile.am b/Makefile.am
> index 0ed944c..ab49dfd 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -111,7 +111,8 @@ tools_kmod_SOURCES = tools/kmod.c tools/kmod.h tools/lsmod.c \
> tools/rmmod.c tools/insmod.c \
> tools/modinfo.c tools/modprobe.c \
> tools/depmod.c tools/log.h tools/log.c \
> - tools/static-nodes.c
> + tools/static-nodes.c tools/mkdir.c \
> + tools/mkdir.h
> tools_kmod_LDADD = libkmod/libkmod-util.la \
> libkmod/libkmod-internal.la
>
> diff --git a/tools/mkdir.c b/tools/mkdir.c
> new file mode 100644
> index 0000000..9935366
> --- /dev/null
> +++ b/tools/mkdir.c
> @@ -0,0 +1,81 @@
> +/*
> + * Copyright (C) 2012 Lucas De Marchi <lucas.de.marchi@gmail.com
> + * Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with this library; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#include <errno.h>
> +#include <string.h>
> +#include <sys/stat.h>
> +#include <sys/types.h>
> +
> +#include "mkdir.h"
> +
> +static int mkdir_p(char *path, mode_t mode)
> +{
> + char *end = path + strlen(path);
> + struct stat st;
> + int r;
> +
> + /*
> + * don't try to overwrite existing file, succeed if it is a directory,
> + * fail otherwise
> + */
> + if (stat(path, &st) >= 0) {
> + if (S_ISDIR(st.st_mode)) {
> + return 0;
> + } else
> + return -ENOTDIR;
> + }
> +
> + /* Find the last '/' */
> + while (end > path && *end != '/')
> + end--;
> +
> + /* discard extra '/' */
> + while (end > path && *(end - 1) == '/')
> + end--;
> +
> + /* if a parent directory was found, create it recursively */
> + if (end != path) {
> + *end = '\0';
> + r = mkdir_p(path, mode);
> + *end = '/';
> +
> + if (r < 0)
> + return -errno;
> + }
> +
> + /* create the desired directory */
> + return mkdir(path, mode);
> +}
We already have mkdir_p in kmod. You can move it to libkmod-utils and
adapt it as you please if you want to use it outside of testsuite. The
difference I see is that it's not a recursive implementation, but
rather uses an inner loop.
> +
> +int mkdir_parents(const char *path, mode_t mode) {
> + char *dirname = strdupa(path);
> + char *offset = strrchr(dirname, '/');
> +
> + /* no parent directories */
> + if (!offset) {
> + return 0;
> + }
> +
> + /* compute the dirname */
> + while (*(offset - 1) == '/')
> + offset--;
> + *offset = '\0';
> +
> + return mkdir_p(dirname, mode);
> +}
You can add this function to libkmod-util, too.
> diff --git a/tools/mkdir.h b/tools/mkdir.h
> new file mode 100644
> index 0000000..5327fbe
> --- /dev/null
> +++ b/tools/mkdir.h
> @@ -0,0 +1,25 @@
> +/*
> + * Copyright (C) 2012 Lucas De Marchi <lucas.de.marchi@gmail.com>
> + * Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with this library; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#pragma once
> +
> +#include <sys/stat.h>
> +#include <sys/types.h>
> +
> +int mkdir_parents(const char *path, mode_t mode);
> diff --git a/tools/static-nodes.c b/tools/static-nodes.c
> index 351c02b..faccefc 100644
> --- a/tools/static-nodes.c
> +++ b/tools/static-nodes.c
> @@ -32,6 +32,8 @@
> #include <sys/types.h>
> #include "libkmod-util.h"
>
> +#include "mkdir.h"
> +
> #include "kmod.h"
>
> struct static_nodes_format {
> @@ -154,12 +156,11 @@ static void help(void)
> static int do_static_nodes(int argc, char *argv[])
> {
> struct utsname kernel;
> - char modules[PATH_MAX];
> + char modules[PATH_MAX], buf[4096];
> const char *output = "/dev/stdout";
> FILE *in = NULL, *out = NULL;
> const struct static_nodes_format *format = &static_nodes_format_human;
> - char buf[4096];
> - int ret = EXIT_SUCCESS;
> + int r, ret = EXIT_SUCCESS;
>
> for (;;) {
> int c, idx = 0, valid;
> @@ -227,6 +228,13 @@ static int do_static_nodes(int argc, char *argv[])
> goto finish;
> }
>
> + r = mkdir_parents(output, 0755);
> + if (r < 0) {
> + fprintf(stderr, "Error: could not create parent directory for %s - %m.\n", output);
> + ret = EXIT_FAILURE;
> + goto finish;
> + }
> +
> out = fopen(output, "we");
> if (out == NULL) {
> fprintf(stderr, "Error: could not create %s - %m\n", output);
> --
> 1.8.3.2
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-modules" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
--
Lucas De Marchi
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2013-07-14 14:57 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-14 13:13 [PATCHv2 1/2] static-nodes: don't fail if modules.devname not found Tom Gundersen
2013-07-14 13:13 ` [PATCHv2 2/2] static-nodes: create parent directories of output file Tom Gundersen
2013-07-14 14:57 ` Lucas De Marchi
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.