s3: use directory_create_or_exist_strict() to create corepath
authorGregor Beck <gbeck@sernet.de>
Mon, 22 Apr 2013 11:33:00 +0000 (13:33 +0200)
committerMichael Adam <obnox@samba.org>
Wed, 11 Dec 2013 16:54:16 +0000 (17:54 +0100)
This simplifies the code and even works in testenv where the chown call
fails.

Signed-off-by: Gregor Beck <gbeck@sernet.de>
Reviewed-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Michael Adam <obnox@samba.org>
source3/lib/dumpcore.c

index 90acc1679b69d3ab2e8f9cabf177cf15cbdd3507..f284ea46738410cd9fef5bbd7f97a879e1a21535 100644 (file)
@@ -43,34 +43,35 @@ static char *corepath;
  */
 static char *get_default_corepath(const char *logbase, const char *progname)
 {
+       const mode_t mode = 0700;
+       const uid_t uid = getuid();
        char *tmp_corepath;
 
        /* Setup core dir in logbase. */
        tmp_corepath = talloc_asprintf(NULL, "%s/cores", logbase);
-       if (!tmp_corepath)
+       if (!tmp_corepath) {
+               DEBUG(0, ("Out of memory\n"));
                return NULL;
+       }
 
-       if ((mkdir(tmp_corepath, 0700) == -1) && errno != EEXIST)
-               goto err_out;
-
-       if (chmod(tmp_corepath, 0700) == -1)
+       if (!directory_create_or_exist_strict(tmp_corepath, uid, mode)) {
+               DEBUG(0, ("Failed to create %s for user %d with mode 0%o\n",
+                         tmp_corepath, (int)uid, (int)mode));
                goto err_out;
-
-       talloc_free(tmp_corepath);
+       }
 
        /* Setup progname-specific core subdir */
-       tmp_corepath = talloc_asprintf(NULL, "%s/cores/%s", logbase, progname);
-       if (!tmp_corepath)
-               return NULL;
-
-       if (mkdir(tmp_corepath, 0700) == -1 && errno != EEXIST)
-               goto err_out;
-
-       if (chown(tmp_corepath, getuid(), getgid()) == -1)
+       tmp_corepath = talloc_asprintf_append(tmp_corepath, "/%s", progname);
+       if (!tmp_corepath) {
+               DEBUG(0, ("Out of memory\n"));
                goto err_out;
+       }
 
-       if (chmod(tmp_corepath, 0700) == -1)
+       if (!directory_create_or_exist(tmp_corepath, uid, mode)) {
+               DEBUG(0, ("Failed to create %s for user %d with mode 0%o\n",
+                         tmp_corepath, (int)uid, (int)mode));
                goto err_out;
+       }
 
        return tmp_corepath;