Change Another Process’ Environment Variables in Linux

Each process has its environment variables in Linux. But is it possible to change another process’ environment variable from another process in Linux? If it is possible to do so, how to do it?

There is a trick which can work for some scenarios. First, find out the process ID, then start gdb and attach to the process. After gdb has attached to the process, call the putenv() with the environment variable name and value.

$ sudo gdb -p <process ID>
(gdb) call putenv ("MYVAR=myvalue")
(gdb) detach

Let’s test it with an C++ program as follows.

#include <cstdlib>

#include <iostream>
#include <chrono>
#include <thread>

int main()
{
   while (true) {
      if (const char* myvar = std::getenv("MYVAR"))
         std::cout << "MYVAR is: " << myvar << '\n';
      else
         std::cout << "MYVAR is not set\n";
      std::this_thread::sleep_for(std::chrono::seconds(1));
   }
}

Build and run it

$ g++ --std=c++11 getenv.cpp -o /tmp/getenv
$ /tmp/getenv 
MYVAR is not set
MYVAR is not set
MYVAR is not set
MYVAR is not set
...

Now, we can find the process ID of getenv by

$ pidof getenv
52824

Then, we can attach to the process by

$ sudo gdb -p 52824
GNU gdb (Ubuntu 9.1-0ubuntu1) 9.1
...
Attaching to process 52824
...
__GI___clock_nanosleep (clock_id=clock_id@entry=0, flags=flags@entry=0, req=<optimized out>, rem=<optimized out>)
    at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:79
79  ../sysdeps/unix/sysv/linux/clock_nanosleep.c: Permission denied.
(gdb) 

The getenv program stops there.

In the gdb console, let’s run

(gdb) call putenv("MYVAR=myvalue")
$1 = 0
(gdb) detach
Detaching from program: /tmp/getenv, process 52824
[Inferior 1 (process 52824) detached]
(gdb) 

Then the getenv program resumes to execute and starts to print out the MYVAR new value

..
MYVAR is not set
MYVAR is not set
MYVAR is: myvalue
MYVAR is: myvalue
MYVAR is: myvalue
...

The environment of the getenv process has been updated!

One last note, the programs may cache the environment values by itself or the libraries it uses. Under such cases, this trick will not work.

Similar Posts

  • Micosoft招聘部分算法题

    Micosoft招聘部分算法题 1.链表和数组的区别在哪里? 2.编写实现链表排序的一种算法。说明为什么你会选择用这样的方法? 3.编写实现数组排序的一种算法。说明为什么你会选择用这样的方法? 4.请编写能直接实现strstr()函数功能的代码。 5.编写反转字符串的程序,要求优化速度、优化空间。 6.在链表里如何发现循环链接? 7.给出洗牌的一个算法,并将洗好的牌存储在一个整形数组里。 8.写一个函数,检查字符是否是整数,如果是,返回其整数值。(或者:怎样只用4行代码编写出一个从字符串到长整形的函数?) 9.给出一个函数来输出一个字符串的所有排列。 10.请编写实现malloc()内存分配函数功能一样的代码。 11.给出一个函数来复制两个字符串A和B。字符串A的后几个字节和字符串B的前几个字节重叠。 12.怎样编写一个程序,把一个有序整数数组放到二叉树中? 13.怎样从顶部开始逐层打印二叉树结点数据?请编程。 14.怎样把一个链表掉个顺序(也就是反序,注意链表的边界条件并考虑空链表)? 来源:·日月光华 bbs.fudan.edu.cn Read more: OCaml Learning Materials GNU glibc Manual Inline Assembly with GCC on Linux Online Tutorials for Linux Beginners How to Run a Command Upon Files or Directories Changes on Linux Filter Salutation in Microsoft Dynamics CRM Add…

  • How To Enable GNOME Classic Mode in Fedora / CentOS / RHEL Linux

    GNOME 3.8 introduces the classic mode: Classic mode is a new feature for those people who prefer a more traditional desktop experience. Built entirely from GNOME 3 technologies, it adds a number of features such as an application menu, a places menu and a window switcher along the bottom of the screen. Each of these…

Leave a Reply

Your email address will not be published. Required fields are marked *