agora inbox for postgres@postgres.berkeley.edu  
help / color / mirror / Atom feed
putenv in postmaster.c
3+ messages / 3 participants
[nested] [flat]

* putenv in postmaster.c
@ 1995-07-08 16:13  Shreeniwas N Sapre <sns@asterix.cs.unipune.ernet.in>
  0 siblings, 0 replies; 3+ messages in thread

From: Shreeniwas N Sapre @ 1995-07-08 16:13 UTC (permalink / raw)
  To: legacy


The use of putenv in postmaster.c (sample below) has crashed
postmaster on our system (DGUX 5.4R2.10). The symptom is its inability
to find "template1" after servicing the first 2 or 3 requests. My
diagnosis is the use of envEntry ( which is on stack) for putenv
causing corrupted environment entries, as putenv keeps a pointer to
the string and not a copy of the string. This problem surfaced after
we made a few changes (unrelated to this problem) to postmaster. We
have made envEntry as "static" as a solution.

Sample Code: 
BackendStartup(packet, port)
...
{
    char		envEntry[4][2 * ARGV_SIZE];
    
......
    sprintf(envEntry[0], "POSTPORT=%d", PostPortName);
    putenv(envEntry[0]);
...... Similar code here.
......
}
------
===========================================================================
Shreeniwas N. Sapre         |E-Mail: sns@cs.unipune.ernet.in
Phone : +91-212-357812(Off) | Snail: Department of Computer Science,
Fax   : +91-212-353899(Off) |        University of Poona, Pune 411007, INDIA
===========================================================================

==============================================================================
   To add/remove yourself to/from the POSTGRES mailing list: send mail with 
   the subject line ADD or DEL to "postgres-request@postgres.Berkeley.EDU".
   If this fails, send mail to "post_questions@postgres.Berkeley.EDU" and
   a human will deal with it.  DO NOT post to the "postgres" mailing list.
==============================================================================
              URL: http://s2k-ftp.CS.Berkeley.EDU:8000/postgres/



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: putenv in postmaster.c
@ 1995-07-10 17:09  rp2y@methi.ndim.edrc.cmu.edu
  0 siblings, 1 reply; 3+ messages in thread

From: rp2y@methi.ndim.edrc.cmu.edu @ 1995-07-10 17:09 UTC (permalink / raw)
  To: legacy; +Cc: Shreeniwas N Sapre <sns@asterix.cs.unipune.ernet.in>

> The use of putenv in postmaster.c (sample below) has crashed
> postmaster on our system (DGUX 5.4R2.10). The symptom is its inability
> to find "template1" after servicing the first 2 or 3 requests. My
> diagnosis is the use of envEntry ( which is on stack) for putenv
> causing corrupted environment entries, as putenv keeps a pointer to
> the string and not a copy of the string. This problem surfaced after
> we made a few changes (unrelated to this problem) to postmaster. We
> have made envEntry as "static" as a solution.
> 
> Sample Code: 
> BackendStartup(packet, port)
> ...
> {
>     char		envEntry[4][2 * ARGV_SIZE];
>     
> ......
>     sprintf(envEntry[0], "POSTPORT=%d", PostPortName);
>     putenv(envEntry[0]);
> ...... Similar code here.
> ......
> }

First, I'm confused by the code that you posted, this obviously isn't
right since the memory allocated for envEntry is allocated on the
stack when this function is called and released when the function
returns.  I am assuming that this is the "bad" example.

Second, I'm not sure where you got this code because both Postgres V4.2
and Postgres95 actually declare envEntry as an array of pointers and
call calloc() to allocate the memory (which is probably a cleaner
solution that using a "static" array of character arrays).

BackendStartup looks like this in Postgres V4.2:

BackendStartup(packet, port)
...
{
...
    char	*envEntry[4];
    

    for (i = 0; i < 4; ++i) {
	envEntry[i] = calloc(2 * ARGV_SIZE, 1);
    }
    sprintf(envEntry[0], "POSTPORT=%d", PostPortName);
    putenv(envEntry[0]);

...
}


Robert

--
+------------------------------------------------------------------------+
| Robert Patrick (rp2y+@edrc.cmu.edu) Engineering Design Research Center |
| n-dim Group                                 Carnegie Mellon University |
| World Wide Web: http://tika.ndim.edrc.cmu.edu/~rp2y/Home.html          |
+------------------------------------------------------------------------+













--
+------------------------------------------------------------------------+
| Robert Patrick (rp2y+@edrc.cmu.edu) Engineering Design Research Center |
| n-dim Group                                 Carnegie Mellon University |
| World Wide Web: http://tika.ndim.edrc.cmu.edu/~rp2y/Home.html          |
+------------------------------------------------------------------------+












==============================================================================
   To add/remove yourself to/from the POSTGRES mailing list: send mail with 
   the subject line ADD or DEL to "postgres-request@postgres.Berkeley.EDU".
   If this fails, send mail to "post_questions@postgres.Berkeley.EDU" and
   a human will deal with it.  DO NOT post to the "postgres" mailing list.
==============================================================================
              URL: http://s2k-ftp.CS.Berkeley.EDU:8000/postgres/



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: putenv in postmaster.c
@ 1995-07-12 16:50  Tom R.Hageman <tom@basil.icce.rug.nl>
  parent: rp2y@methi.ndim.edrc.cmu.edu
  0 siblings, 0 replies; 3+ messages in thread

From: Tom R.Hageman @ 1995-07-12 16:50 UTC (permalink / raw)
  To: rp2y@methi.ndim.edrc.cmu.edu; +Cc: legacy; Shreeniwas N Sapre <sns@asterix.cs.unipune.ernet.in>

Robert Patrick <rp2y@methi.ndim.edrc.cmu.edu> wrote:
> [Shreeniwas N Sapre <sns@asterix.cs.unipune.ernet.in> wrote:]
> > The use of putenv in postmaster.c (sample below) has crashed
> > postmaster on our system (DGUX 5.4R2.10). The symptom is its inability
> > to find "template1" after servicing the first 2 or 3 requests. My
> > diagnosis is the use of envEntry ( which is on stack) for putenv
> > causing corrupted environment entries, as putenv keeps a pointer to
> > the string and not a copy of the string. This problem surfaced after
> > we made a few changes (unrelated to this problem) to postmaster. We
> > have made envEntry as "static" as a solution.
> >
[code sample munched]
>
> First, I'm confused by the code that you posted, this obviously isn't
> right since the memory allocated for envEntry is allocated on the
> stack when this function is called and released when the function
> returns.  I am assuming that this is the "bad" example.
>
> Second, I'm not sure where you got this code because both Postgres V4.2
> and Postgres95 actually declare envEntry as an array of pointers and
> call calloc() to allocate the memory (which is probably a cleaner
> solution that using a "static" array of character arrays).
>
> BackendStartup looks like this in Postgres V4.2:
>
> BackendStartup(packet, port)
> ...
> {
> ...
>     char	*envEntry[4];
>
>
>     for (i = 0; i < 4; ++i) {
> 	envEntry[i] = calloc(2 * ARGV_SIZE, 1);
>     }
>     sprintf(envEntry[0], "POSTPORT=%d", PostPortName);
>     putenv(envEntry[0]);
>
> ...
> }

Well, I've got the original postgres 4.2 lying around and it indeed uses the  
method as described by the first poster.  There has been a patch (I think) by  
aoki that changed this to use calloc()ed strings.

The disadvantage of the new method, at least the way it is implemented now,  
is that it introduces a memory leak.  Which is rather a nuisance in a  
potentially never-ending process like the postmaster. (but then, I'm sure it  
is not the only one... <wry grin>)

--
__/__/__/__/  Tom Hageman  <tom@basil.icce.rug.nl>  [NeXTmail/Mime OK]
  __/ __/_/         IC Group  <tom@icgned.nl> (work)
 __/__/__/          "...to baldly go where no one has gone before."
__/  _/_/                                             -- star trek TNG

==============================================================================
   To add/remove yourself to/from the POSTGRES mailing list: send mail with 
   the subject line ADD or DEL to "postgres-request@postgres.Berkeley.EDU".
   If this fails, send mail to "post_questions@postgres.Berkeley.EDU" and
   a human will deal with it.  DO NOT post to the "postgres" mailing list.
==============================================================================
              URL: http://s2k-ftp.CS.Berkeley.EDU:8000/postgres/



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~1995-07-12 16:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1995-07-08 16:13 putenv in postmaster.c Shreeniwas N Sapre <sns@asterix.cs.unipune.ernet.in>
1995-07-10 17:09 Re: putenv in postmaster.c rp2y@methi.ndim.edrc.cmu.edu
1995-07-12 16:50 ` Tom R.Hageman <tom@basil.icce.rug.nl>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox