+ debug_setup_1(NULL, subdir);
+
+ /*
+ * Create the new file with a unique sequence number.
+ */
+ mask = (mode_t)umask((mode_t)0037); /* Allow the group read bit through */
+ for(i = 0; fd < 0; i++) {
+ amfree(db_name);
+ if ((db_name = get_debug_name(curtime, i)) == NULL) {
+ error("Cannot create %s debug file", get_pname());
+ /*NOTREACHED*/
+ }
+
+ if ((s = newvstralloc(s, dbgdir, db_name, NULL)) == NULL) {
+ error("Cannot allocate %s debug file name memory", get_pname());
+ /*NOTREACHED*/
+ }
+
+ if ((fd = open(s, O_WRONLY|O_CREAT|O_EXCL|O_APPEND, 0640)) < 0) {
+ if (errno != EEXIST) {
+ error("Cannot create %s debug file: %s",
+ get_pname(), strerror(errno));
+ /*NOTREACHED*/
+ }
+ amfree(s);
+ }
+ }
+ (void)umask(mask); /* Restore mask */
+
+ /*
+ * Finish setup.
+ *
+ * Note: we release control of the string 's' points to.
+ */
+ debug_setup_2(s, fd, "start");
+}
+
+void
+debug_reopen(
+ char * dbfilename,
+ char * notation)
+{
+ char *s = NULL;
+ int fd;
+
+ if (dbfilename == NULL) {
+ return;
+ }
+
+ /*
+ * Do initial setup.
+ */
+ debug_setup_1(NULL, NULL);
+
+ /*
+ * Reopen the file.
+ */
+ if (*dbfilename == '/') {
+ s = stralloc(dbfilename);
+ } else {
+ s = newvstralloc(s, dbgdir, dbfilename, NULL);
+ }
+ if ((fd = open(s, O_RDWR|O_APPEND)) < 0) {
+ error("cannot reopen %s debug file %s", get_pname(), dbfilename);
+ /*NOTREACHED*/
+ }
+
+ /*
+ * Finish setup.
+ *
+ * Note: we release control of the string 's' points to.
+ */
+ debug_setup_2(s, fd, notation);
+}
+
+void
+debug_rename(
+ char *config,
+ char *subdir)
+{
+ int fd = -1;
+ int i;
+ char *s = NULL;
+ mode_t mask;
+
+ if (!db_filename)
+ return;
+
+ /*
+ * Do initial setup.
+ */
+ debug_setup_1(config, subdir);
+
+ s = newvstralloc(s, dbgdir, db_name, NULL);
+
+ if (strcmp(db_filename, s) == 0) {
+ amfree(s);
+ return;
+ }
+
+ mask = (mode_t)umask((mode_t)0037);
+ /* check if a file with the same name already exist */
+ if ((fd = open(s, O_WRONLY|O_CREAT|O_EXCL|O_APPEND, 0640)) < 0) {
+ for(i = 0; fd < 0; i++) {
+ amfree(db_name);
+ if ((db_name = get_debug_name(curtime, i)) == NULL) {
+ dbprintf(("Cannot create %s debug file", get_pname()));
+ break;
+ }
+
+ s = newvstralloc(s, dbgdir, db_name, NULL);
+ if ((fd = open(s, O_WRONLY|O_CREAT|O_EXCL|O_APPEND, 0640)) < 0) {
+ if (errno != EEXIST) {
+ dbprintf(("Cannot create %s debug file: %s", get_pname(),
+ strerror(errno)));
+ break;
+ }
+ }
+ }
+ }
+
+ if (fd >= 0) {
+ rename(db_filename, s);
+ }
+ (void)umask(mask); /* Restore mask */
+ close(fd);
+ /*
+ * Finish setup.
+ *
+ * Note: we release control of the string 's' points to.
+ */
+ debug_setup_2(s, -1, "rename");