I think this should work, to illustrate the principle...
#include <stdio.h>
int main(int argc, char *argv[]) {
argv[0][0] = 65;
sleep(10);
}
will change the name, and put an "A" instead of the first letter. CtrlZ to pause, then run ps
to see the name changed. I have no clue, but it seems somewhat dangerous, since some things might depend on argv[0]
.
Also, I tried replacing the pointer itself to another string; no cigar. So this would only work with strcpy
and strings shorter or equal than the original name.
There might or might not be a better way for this. I don't know.
EDIT: nonliteral solution: If you're forking, you know the child's PID (getpid()
in the child, result of fork()
in the parent). Just output it somewhere where you can read it, and kill the child by PID.
another nonliteral solution: make softlinks to the executable with another name (ln -s a.out kill_this_a.out
), then when you exec, exec the link. The name will be the link's name.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…