2

I have writen a fastcGI application using C and C++

I have a free function that returns a string, if a specific environment variable has not been set. The function looks like this:

namespace
{
    std::string getNameString()
    {
        char * datastr_ = getenv(MY_ENVAR.c_str());

        if (datastr_)
            return std::string(datastr_);
        return DEFAULT_NAME;
    }
};

I then carry out the following steps (in the order given below)

  1. I have edited my /etc/environment and added the appropriate environment variable
  2. I confirm that the variable has been set by typing printenv on the console
  3. I stop and then start the apache daemon

When I debug my application, I find that the environment variable has not been set. I suspect that the environment under which the fastcgi application is running may different from the environment 'normal' applications run under.

Does anyone know how to retrieve environment variable in a fastcgi app?

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
oompahloompah
  • 9,087
  • 19
  • 62
  • 90
  • I KNEW someone pedantic would point that out. Well getenv() is not C++ either ... – oompahloompah Apr 21 '11 at 11:26
  • 1
    There's nothing pedantic about it, it's just plainly incorrect. It's either C or C++, and this is not C. `getenv` is a library function, nothing to do with the language. – Cat Plus Plus Apr 21 '11 at 11:30
  • "It's either C or C++": Don't be silly - you CAN have code that uses both C and C++. The code I have written intermixes C and C++ - and links to both the libfcgi and libfcgi++ libraries (I merely posted a snippet of the code) – oompahloompah Apr 21 '11 at 11:37

2 Answers2

1

I suspect that fastcgi processes are spawned in a "cleaned" environment by default, given your observations. Apache certainly provides a way of setting environment variables for fastcgi. This has the added bonus of being slightly less cryptic too (who expects a webservice to behave differently when /etc/environment is changed?), like this you keep "web config things" with "web config things".

Flexo
  • 87,323
  • 22
  • 191
  • 272
  • Reading up on the link you provided now ... If that fails, I'll just have to resort to reading the data from a configuration file (or perhaps storing it somewhere in memcache) ... – oompahloompah Apr 21 '11 at 11:41
0

You could look here http://httpd.apache.org/docs/current/env.html and try to set the env variable in the apache process. I have assumed latest apache version.

ColWhi
  • 1,077
  • 6
  • 16
  • Thanks. I did try using SetEnv and PassEnv directives, but getenv() still returned a NULL pointer. – oompahloompah Apr 21 '11 at 11:39
  • A FastCGI process is not an Apache process, it runs as a separate process, and Apache just make a request for such process to start (requests made the fcgi-pm). The solution is to use the configuration of the FastCGI module. If the module is `mod_fastcgi`, then [Flexo gave the good answer](http://stackoverflow.com/a/5743405/279335) – Hibou57 Mar 22 '13 at 08:58