-1

I have a C program which waits a string as argument, I can use it like that:

./myprog "hello"

What I want to do is:

  • Start the program without argument
  • Get the PID of this prog
  • Use this PID as a base to make a string
  • Pass this string as the argument of the running prog.

I know how to get the pid. I can do it so:

./myprog &
PID=$!

I have another prog to calculate the string I want with this pid:

./StringFromPid $PID

What I don't know is: how to use the result of this last line as the input of myProg (which is already launch, but without his string arg, knowing that when it's launched without arg it will simply close...)

I've tried:

./StringFromPid $PID | ./myprog

But there's two problems with it:

  1. It launches another instance of myprog, which has another pid, so that's not what I want.

  2. For some reason, it doesn't even work. I mean, the output of ./StringFromPid $PID is not used as input for ./myprog. It just close, like if there were no arg.

I can't modify myprog, but I can modify StringFromPid; I can even put the code of StringFromPid in my bash script since it's not big at all. And I've read a lot of tuto about bash script but I'm still new to it (not a lot of practice)

I've found a solution, but it's not optimal (far from it):

#!/bin/bash
./myprog $(echo $(./StringFromPid $(let "a = $$";let "b = 5"; let "c = a + b"; echo $c)))

The +5 is because I just found that when I launch this script, there is a +5 difference between his PID and myprog PID. No idea why, but it works.

However I'm still open for better solutions. In fact this work on my pc (I'm on Ubuntu 16.04 by the way,) but at the end it must work on the CTF server where I have no admin rights

double-beep
  • 5,031
  • 17
  • 33
  • 41
Ablia
  • 317
  • 1
  • 3
  • 14
  • Why do you need to use the pid of `myprog` to compute that string? Isn't any other number good enough for that? For example, the pid of the current process. – axiac Mar 09 '19 at 13:36
  • no, i really need the pid of myprog. myprog will use his pid to calculate a string, and y task is to give him exactly the string he'll get. It's for a ctf. I've understand everything, what it uses to calculate the string, how i can get this same string...I just don't know how to give him an argument AFTER starting it without arg...I hought meybe make him sleep while i calculate the string, but then how to wake him up AND give him an argument? – Ablia Mar 09 '19 at 13:51
  • 1
    why not using `pidof -x $0` inside the script? – Hernan Garcia Mar 09 '19 at 14:24
  • 2
    But why do you need the PID as an argument? You already know the value; there is no additional benefit from putting it in `$1` too. – tripleee Mar 09 '19 at 14:28
  • @Herman Garcia because pidof -x $0 give me the PID of my bash script. I want the pid of 'myprog' and i can't modify the script of 'myprog' (which is a C programm, btw), so the only way i found until now is start myprog and get the pid of the last process launch, namely $!. – Ablia Mar 09 '19 at 14:29
  • @triplee my task is not to find the value, but to use it as the argument of myprog. myprog will compare the value i gave him with the one it calculate, and then say Ok and give me the flag if they're the same. – Ablia Mar 09 '19 at 14:33
  • This all sounds very strange. However, you could achieve what want if you knew `myprog`'s PID beforehand. In Linux there is a hack to specify the PID when starting a program, see https://stackoverflow.com/q/18122592/6770384. Alternatively, if you could pass the PID via stdin instead of using arguments you could start `myprogram < someNamedPipe` and pass the PID once you know it without problems. – Socowi Mar 09 '19 at 14:33
  • What OS is the script running on? – jhnc Mar 09 '19 at 15:02
  • @Socowi finding the PID beforehand would be a great solution! i'm trying to understand how this fork thing work. However, i have no admin right's, so i'm not sure what i can really do. – Ablia Mar 09 '19 at 15:23
  • The +5 is completely coincidental, it will stop working when you run on a system with more activity. – tripleee Mar 09 '19 at 16:31

1 Answers1

3

Simplest way is to exec the C program.

#!/bin/bash

# pid of current shell
PID1=$$
echo $PID1

# do stuff

(
    # this is a subshell

    # $$ doesn't change but BASHPID does
    PID2=$BASHPID
    echo $PID2

    # the pid of this C program will be $PID2
    exec myCprogram $(./StringFromPid $PID2)

    # exec doesn't return
    echo "can't get here"
)

# do more stuff

If you're on a platform that supports it, it may be possible to use the LD_PRELOAD trick to override getpid().

But exec is simpler.

jhnc
  • 11,310
  • 1
  • 9
  • 26
  • Oh thanks, that is much better than my solution. However, it doesn't work: the pid of the C program is not PID2 but PID2+1. No idea why. Can you explain why it should be PID2? – Ablia Mar 09 '19 at 15:27
  • sorry, I didn't test. bash doesn't change $$ in subshell. try this version – jhnc Mar 09 '19 at 15:50