File descriptor not generated

I ran into a problem in a shell script. I narrowed down the problem and found that it was due to a file descriptor from standard input that is not generated in the proc file system. Below is the test script I wrote:

#!/bin/ksh var=`ls -lrt /proc/$$/path/0` echo $var if [[ -f /proc/$$/path/0 ]] then echo "found file descriptor" else echo "file descriptor not found" fi 

I tested this in the / tmp / directory with an example input file:

 $ ./checkip.sh < /tmp/testip lrwxrwxrwx 1 root root 0 Dec 31 09:15 /proc/19358/path/0 -> /tmp/testip found file descriptor 

Now I tested in one of the directories where we ran into a problem.

 $ ./checkip.sh < /var/opt/xxxxxxxx/testip lrwxrwxrwx 1 root root 0 Dec 31 09:15 /proc/20124/path/0 file descriptor not found $ 

I thought it was with the xxxxxxxx directory. so I checked it again with the file in the parent directory.

 $ ./checkip.sh < /var/opt/testip lrwxrwxrwx 1 root root 0 Dec 31 09:16 /proc/21286/path/0 -> /var/opt/testip found file descriptor $ 

It worked again. But I do not see the difference between the permissions of the opt directory and xxxxxxxx

 $ ls -l |grep xxxxxxxx drwxr-xr-x 292 abcdefg abc 8192 Dec 31 08:33 xxxxxxxx $ cd .. $ ls -l | grep opt drwxr-xr-x 17 abcdefg abc 512 Dec 31 09:14 opt 

I am confused by why the symbolic link for 0 (standard input) is not created correctly. Can someone help me find the reason for this?

EDIT - for comment

file does not exist

 $ ./checkip.sh < /tmp/notexistfile /tmp/notexistfile: No such file or directory. 

file exists

 $ ./checkip.sh < /var/opt/testip lrwxrwxrwx 1 root root 0 Dec 31 10:49 /proc/15917/path/0 -> /var/opt/testip found file descriptor 

file exists

 $ ./checkip.sh < /var/opt/xxxxxxxx/testip lrwxrwxrwx 1 root root 0 Dec 31 10:49 /proc/16566/path/0 file descriptor not found $ 

The problem I could see was a symlink for standard input in proc / pid / path / 0, which is not created for a single directory. But what could be the reason. I have no ideas !. Even the farm output does not show anything about how the file (sym link) proc / pid / path / 0 is created.

Truss output: working register (check if stat64 call succeeded):

 9107/1: 0.0117 read(62, 0xFEF687A0, 1024) = 116 9107/1: # ! / bin / ksh\n\nif [ [ - f / proc / $ $ / p 9107/1: ath / 0 ] ]\nthen\necho " foundfiled 9107/1: escriptor "\nelse\necho " filedescr 9107/1: iptornotfound "\nfi\n 9107/1: 0.0118 sysconfig(6) = 4096 9107/1: 0.0119 stat64(0x0808E630, 0x08089B40) = 0 9107/1: 0x0808E630: "/proc/9107/path/0" 9107/1: d=0x04F40002 i=4191608033 m=0100644 l=1 u=308 g=205 sz=3 9107/1: at = Dec 31 08:33:06 GMT 2014 [ 1420014786.307569314 ] 9107/1: mt = Dec 31 08:33:06 GMT 2014 [ 1420014786.307619723 ] 9107/1: ct = Dec 31 08:33:06 GMT 2014 [ 1420014786.307619723 ] 9107/1: bsz=4096 blks=8 fs=tmpfs 

farm output: the case does not work (check the failure of the stat64 call):

 10125/1: 0.0057 read(62, 0xFEF687A0, 1024) = 116 10125/1: # ! / bin / ksh\n\nif [ [ - f / proc / $ $ / p 10125/1: ath / 0 ] ]\nthen\necho " foundfiled 10125/1: escriptor "\nelse\necho " filedescr 10125/1: iptornotfound "\nfi\n 10125/1: 0.0058 sysconfig(6) = 4096 10125/1: 0.0071 stat64(0x0808E630, 0x08089B40) Err#2 ENOENT 10125/1: 0x0808E630: "/proc/10125/path/0" 10125/1: 0.0071 lwp_sigmask(3, 0x00020000, 0x00000000) = 0xFFBFFEFF [0x0000FFFF] 10125/1: 0.0071 stat64(0x0808E792, 0x08047600) Err#2 ENOENT 10125/1: 0x0808E792: "/usr/sbin/echo" 10125/1: 0.0072 lwp_sigmask(3, 0x00000000, 0x00000000) = 0xFFBFFEFF [0x0000FFFF] 10125/1: 0.0072 lwp_sigmask(3, 0x00020000, 0x00000000) = 0xFFBFFEFF [0x0000FFFF] 
+5
source share
2 answers

It depends on your if if statement. It specifically checks if FILE (-f) exists. What you have in all the examples is links, not files. The resolution of these links determines whether the -f request is executed or not. When I checked the results of / var with ls on my own system, the link pointed to a special character device that would not define the criteria for β€œfiles”.

I am confused in the direction in the script. I do not see anywhere that it actually uses the input you entered. "Var" is completely autonomous inside the script, as you indicated above. In order to get -f to work on the specified file name, you need to do something according to if [-f $ 1] (if your script does not change between publication and the example runs).

0
source

I was able to reproduce the same behavior by deleting the file at the beginning of the script:

 # cat ./aaa #!/bin/ksh rm -f /tmp/file.tmp var=`ls -lrt /proc/$$/path/0` echo $var if [[ -f /proc/$$/path/0 ]]; then echo "found file descriptor" else echo "file descriptor not found" fi 

Then:

 # touch /tmp/file.tmp # ./aaa < /tmp/file.tmp lrwxrwxrwx 1 root root 0 Dec 25 14:52 /proc/784/path/0 file descriptor not found 

My suggestion is to execute ls -li /var/opt/xxxxxxxx/testip several times, and I'm pretty sure that you will see a different index with each run or run it before and after the script is executed.

I think that in your case the file is overwritten by something else, and you always see the file, but when you run this script, a redirection opens to read the file from a specific inode and until the 1st command is executed, the input file is overwritten to another index that is not knows the script.

0
source

Source: https://habr.com/ru/post/1210170/


All Articles