Solved

Getting Environment Variables of Another User in C++ in Unix?

Posted on 2010-09-24
3
627 Views
Last Modified: 2012-08-13
Dear experts,

I have a process running in UNIX. I want the process to get the environment variables of a user other than the user in which it is running. Then I will run some of its threads with the environment of that another user. How can I achieve this?

BR,
0
Comment
Question by:GurcanK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 78

Assisted Solution

by:arnold
arnold earned 200 total points
ID: 33755397
If you want to run an under the effective rights of another user, you have to switch to the effective user's settings. The environment variables are definable and vary depending on what the user has configured under their shell.

Are you spawning the thread using fork?

Have a look at any daemon application, http, etc. and you see that while the process starts running as root it then continues to run under a user with lower level rights.

I.e. you can use execv with fork etc.
http://www.google.com/url?sa=t&source=web&cd=6&ved=0CC0QFjAF&url=https%3A%2F%2Fwww.securecoding.cert.org%2Fconfluence%2Fdownload%2Fattachments%2F26017980%2F07%2BUNIX%2BPermissions%2Band%2BPrivileges.pdf%3Fversion%3D1%26modificationDate%3D1238607959000&rct=j&q=unix%20programming%20fork%20drop%20privileges&ei=w8-cTI3eKIWKlwemhvnVCg&usg=AFQjCNF4mU4EeSAzTfDSBTdkFtRF_7QMXA&cad=rja

http%3A%2F%2Fwww.cs.uga.edu%2F~eileen%2F1730%2FNotes%2Fprocesses-UNIX.ppt&rct=j&q=unix%20programming%20fork%20drop%20privileges&ei=w8-cTI3eKIWKlwemhvnVCg&usg=AFQjCNFHbm7rVc-AW71bJ6okEqEeOJV3sA&cad=rja

etc.
get yourself an account on www.informit.com and you may be able to use one of the C++ programming references.

There are other similar resources where you can access various books to get an example.
0
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 33755665
The easiest way would be to launch a shell to execute an 'export' command that list all environment variables. From your other Q I understand that your process is running as 'root', so you could use
#include <iostream>
#include <vector>
using namespace std;

#include <stdlib.h>

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int run_cmd ( const char* pCmd, vector<string>& vOutput) {

  FILE *fp;
  int status;
  char path[PATH_MAX];

  fp = popen(pCmd, "r");
 // if (fp == NULL)
    /* Handle error */;

  while (fgets(path, PATH_MAX, fp) != NULL)
      printf("%s", path);

  status = pclose(fp);
  if (status == -1) {
    /* Error reported by pclose() */

  } else {

    /* Use macros described under wait() to inspect `status' in order
       to determine success/failure of command executed by popen() */
  }
return 0;
}

int main () {

  vector<string> vOutput;

  run_cmd("su -l OtherUser csh -c export",vOutput);

  // now vOutput contains all environment variables

  return 0;
}

Open in new window

0
 

Author Closing Comment

by:GurcanK
ID: 33763287
The comments were satisfactory.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Field name with special character (Ñ) in Oracle 11 193
Outlook 13 102
check the file dates in unix 14 77
comm diff cmp unix commands 2 23
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question