Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-09-24
3
Medium Priority
?
645 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
3 Comments
 
LVL 80

Assisted Solution

by:arnold
arnold earned 800 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 1200 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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

885 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