Re: [HACKERS] Odd problem with read_pg_options ... - Mailing list pgsql-hackers

From Massimo Dal Zotto
Subject Re: [HACKERS] Odd problem with read_pg_options ...
Date
Msg-id 199810142237.AAA01452@nikita.wizard.net
Whole thread Raw
In response to Odd problem with read_pg_options ...  ("Marc G. Fournier" <scrappy@hub.org>)
Responses Re: [HACKERS] Odd problem with read_pg_options ...  (Bruce Momjian <maillist@candle.pha.pa.us>)
List pgsql-hackers
>
>
> I reported earlier a SegFault when doing an initdb, and have narrowed it
> down somewhat...still probing, but figured I'd see if I'm overlooking
> something obvious...
>
> echo "vacuum" | postgres -o /dev/null -F -Q -D/home/centre/marc/pgsql/data
>
> This is the stage that the first SegFault happens in initdb.
>
> I added some debugging, and it turns out that the 'DataDir' variable isn't
> being initialized at this point:
>
> void
> read_pg_options(SIGNAL_ARGS)
> {
>   int     fd;
>   int     n;
>   int     verbose;
>   char    buffer[BUF_SIZE];
>   char    c;
>   char     *s,
>          *p;
>
>   printf("before sprintf()\n");
>   printf("%s\n", DataDir);
>   sprintf(buffer, "%s/%s", DataDir, "pg_options");
>   printf("after sprintf()\n");
>   if ((fd = open(buffer, O_RDONLY)) < 0)
>     return;
>
> =====================
>
> Still looking, but uncovered a slight bug:
>
> diff -cr postgres.c.orig postgres.c
> *** postgres.c.orig     Tue Oct 13 16:47:00 1998
> --- postgres.c  Tue Oct 13 16:47:33 1998
> ***************
> *** 1052,1057 ****
> --- 1052,1058 ----
>
>                         case 'D':                       /* PGDATA directory */
>                                 DataDir = optarg;
> +                               break;
>
>                         case 'd':                       /* debug level */
>                                 flagQ = false;
>
>
> Further into it...if I do a setenv PGDATA, it gets around the 'bug'...back
> later...
>
> Marc G. Fournier                               scrappy@hub.org
> Systems Administrator @ hub.org
> scrappy@{postgresql|isc}.org                       ICQ#7615664
>

The problem is that read_pg_options needs DataDir to read its file but
DataDir is set after read_pg_options if postgres is called interactively.
If postgres is forked by postgres DataDir is read from the PGDATA enviromnent
variable set by the postmaster and this explains while the bug disappears.
I have written this patch but I don't like it. Any better idea?

*** src/backend/utils/init/globals.c.orig    Thu Oct 15 00:13:03 1998
--- src/backend/utils/init/globals.c    Thu Oct 15 00:13:07 1998
***************
*** 46,52 ****
  struct Port *MyProcPort;
  long        MyCancelKey;

! char       *DataDir;

   /*
    * The PGDATA directory user says to use, or defaults to via environment
--- 46,52 ----
  struct Port *MyProcPort;
  long        MyCancelKey;

! char       *DataDir = NULL;

   /*
    * The PGDATA directory user says to use, or defaults to via environment
*** src/backend/utils/misc/trace.c.orig    Thu Sep  3 09:00:39 1998
--- src/backend/utils/misc/trace.c    Thu Oct 15 00:18:49 1998
***************
*** 343,348 ****
--- 343,353 ----
      char       *s,
                 *p;

+     if (!DataDir) {
+         fprintf(stderr, "read_pg_options: DataDir not defined\n");
+         return;
+     }
+
      sprintf(buffer, "%s/%s", DataDir, "pg_options");
      if ((fd = open(buffer, O_RDONLY)) < 0)
          return;
*** src/backend/tcop/postgres.c.orig    Tue Sep  1 09:01:27 1998
--- src/backend/tcop/postgres.c    Thu Oct 15 00:23:24 1998
***************
*** 1049,1055 ****
--- 1049,1061 ----
                  break;

              case 'D':            /* PGDATA directory */
+                     if (!DataDir) {
+                     DataDir = optarg;
+                     /* must be done after DataDir is defined */
+                     read_pg_options(0);
+                 }
                  DataDir = optarg;
+                 break;

              case 'd':            /* debug level */
                  flagQ = false;

--
Massimo Dal Zotto

+----------------------------------------------------------------------+
|  Massimo Dal Zotto                email:  dz@cs.unitn.it             |
|  Via Marconi, 141                 phone:  ++39-461-534251            |
|  38057 Pergine Valsugana (TN)     www:  http://www.cs.unitn.it/~dz/  |
|  Italy                            pgp:  finger dz@tango.cs.unitn.it  |
+----------------------------------------------------------------------+

pgsql-hackers by date:

Previous
From: Brook Milligan
Date:
Subject: Re: [HACKERS] perl interface bug?
Next
From: Egon Schmid
Date:
Subject: Problems