* parseServiceInfo: if a service name has been given, look it up and absorb * connection options from it into *options. * * Returns 0 on success, nonzero on failure. On failure, if errorMessage * isn't null, also store an error message there. (Note: the only reason * this function and related ones don't dump core on errorMessage == NULL * is the undocumented fact that printfPQExpBuffer doe
| 5327 | * a null PQExpBuffer pointer.) |
| 5328 | */ |
| 5329 | static int |
| 5330 | parseServiceInfo(PQconninfoOption *options, PQExpBuffer errorMessage) |
| 5331 | { |
| 5332 | const char *service = conninfo_getval(options, "service"); |
| 5333 | char serviceFile[MAXPGPATH]; |
| 5334 | char *env; |
| 5335 | bool group_found = false; |
| 5336 | int status; |
| 5337 | struct stat stat_buf; |
| 5338 | |
| 5339 | /* |
| 5340 | * We have to special-case the environment variable PGSERVICE here, since |
| 5341 | * this is and should be called before inserting environment defaults for |
| 5342 | * other connection options. |
| 5343 | */ |
| 5344 | if (service == NULL) |
| 5345 | service = getenv("PGSERVICE"); |
| 5346 | |
| 5347 | /* If no service name given, nothing to do */ |
| 5348 | if (service == NULL) |
| 5349 | return 0; |
| 5350 | |
| 5351 | /* |
| 5352 | * Try PGSERVICEFILE if specified, else try ~/.pg_service.conf (if that |
| 5353 | * exists). |
| 5354 | */ |
| 5355 | if ((env = getenv("PGSERVICEFILE")) != NULL) |
| 5356 | strlcpy(serviceFile, env, sizeof(serviceFile)); |
| 5357 | else |
| 5358 | { |
| 5359 | char homedir[MAXPGPATH]; |
| 5360 | |
| 5361 | if (!pqGetHomeDirectory(homedir, sizeof(homedir))) |
| 5362 | goto next_file; |
| 5363 | snprintf(serviceFile, MAXPGPATH, "%s/%s", homedir, ".pg_service.conf"); |
| 5364 | if (stat(serviceFile, &stat_buf) != 0) |
| 5365 | goto next_file; |
| 5366 | } |
| 5367 | |
| 5368 | status = parseServiceFile(serviceFile, service, options, errorMessage, &group_found); |
| 5369 | if (group_found || status != 0) |
| 5370 | return status; |
| 5371 | |
| 5372 | next_file: |
| 5373 | |
| 5374 | /* |
| 5375 | * This could be used by any application so we can't use the binary |
| 5376 | * location to find our config files. |
| 5377 | */ |
| 5378 | snprintf(serviceFile, MAXPGPATH, "%s/pg_service.conf", |
| 5379 | getenv("PGSYSCONFDIR") ? getenv("PGSYSCONFDIR") : SYSCONFDIR); |
| 5380 | if (stat(serviceFile, &stat_buf) != 0) |
| 5381 | goto last_file; |
| 5382 | |
| 5383 | status = parseServiceFile(serviceFile, service, options, errorMessage, &group_found); |
| 5384 | if (status != 0) |
| 5385 | return status; |
| 5386 |
no test coverage detected