How to find IP of ssh connection in kernel code? - linux

I want to find out IP of ssh connection using kernel code. However, I'm unable to find any suitable reference on Internet.
I tried using usermodehelper API but it seems it doesn't provide the output back in kernel.
Any help is much appreciated!

Let's assume 3298 is the pid of the process you are interested in. In userpsace, I can call
ls -l /proc/3298/fd
It will list something similar like
lrwx------ 1 name name 64 Apr 6 11:27 0 -> /dev/pts/2
lrwx------ 1 name name 64 Apr 6 11:27 1 -> /dev/pts/2
lrwx------ 1 name name 64 Apr 6 11:27 2 -> /dev/pts/2
lrwx------ 1 name name 64 Apr 6 11:27 3 -> socket:[50256]
lrwx------ 1 name name 64 Apr 6 11:37 4 -> /dev/pts/2
lrwx------ 1 name name 64 Apr 6 11:37 5 -> /dev/pts/2
lrwx------ 1 name name 64 Apr 6 11:37 6 -> /dev/pts/2
We now know that fd 3 (and only fd 3) is the file descriptor you are looking for. Your information should be in fd 3. You should be able to do the same thing in kernelspace by looking at the kernel sources how /proc/pid/fd is implemented.

Related

Does Linux have cache memory?

I was running a simulation on a terminal and the simulation did not go through due to disk space issue. (It reported "No space left on device")
Then we cleaned up some space and ran simulation on the same terminal.
However, it still complained the space issue.
When we ran on a new terminal, the simulation went through.
Hence I want to understand the cause of this.
Please help
Thank you.
This is a common issue in linux.
If a process has opened a file and not closed it, removing the file only removes the directory entry (think name) from the directory it is in. Until the file is closed by the process or the process terminates the disk space will not be reclaimed.
To find these files you can look through the /proc file system. Every running process can be found in there by it process id (pid).
Here I'm running a python program that opened a file and is doing nothing. If I use ps to find the pid of the process and cd int /proc/<pid>/fd I can see the open file descriptors and the names of the files that are open:
$ pwd
/proc/38246/fd
$ ls -l
total 0
lrwx------ 1 x x 64 Sep 8 15:39 0 -> /dev/pts/0
lrwx------ 1 x x 64 Sep 8 15:39 1 -> /dev/pts/0
lrwx------ 1 x x 64 Sep 8 15:39 2 -> /dev/pts/0
lr-x------ 1 x x 64 Sep 8 15:39 3 -> /tmp/test
If I remove the file /tmp/test I see this:
$ rm /tmp/test
$ ls -l
total 0
lrwx------ 1 x x 64 Sep 8 15:39 0 -> /dev/pts/0
lrwx------ 1 x x 64 Sep 8 15:39 1 -> /dev/pts/0
lrwx------ 1 x x 64 Sep 8 15:39 2 -> /dev/pts/0
lr-x------ 1 x x 64 Sep 8 15:39 3 -> /tmp/test (deleted)
Search through /proc/*/fd/ for files that say deleted.

Invalid type for argument 1 to gimp-image-crop when trying to batch crop in GIMP Batch Mode

I have some 1080x1920 png files and I want to crop them into 1080x1728 with offset-y 65, so I tried and it returns this:
gimp -i -b '(gimp-image-crop "*.png" 1080 1728 0 65)' -b '(gimp-quit 0)'
$ ll
total 1796
drwxrwxr-x 2 cat cat 4096 Jul 25 15:06 ./
drwxrwxr-x 4 cat cat 4096 Jul 25 14:37 ../
-rwxrwxr-x 1 cat cat 278356 Jul 9 10:56 1.png*
-rwxrwxr-x 1 cat cat 278356 Jul 9 10:56 Screenshot_20210709-105437.png*
-rwxrwxr-x 1 cat cat 58088 Jul 9 10:56 Screenshot_20210709-105445.png*
-rwxrwxr-x 1 cat cat 108385 Jul 9 14:54 Screenshot_20210709-145331.png*
-rwxrwxr-x 1 cat cat 130486 Jul 9 22:18 Screenshot_20210709-221631.png*
-rwxrwxr-x 1 cat cat 133602 Jul 9 22:20 Screenshot_20210709-221834.png*
-rwxrwxr-x 1 cat cat 153976 Jul 9 22:22 Screenshot_20210709-222035.png*
-rwxrwxr-x 1 cat cat 149365 Jul 9 22:23 Screenshot_20210709-222039.png*
-rwxrwxr-x 1 cat cat 124369 Jul 11 17:01 Screenshot_20210711-165959.png*
-rwxrwxr-x 1 cat cat 138719 Jul 24 23:06 Screenshot_20210724-230338.png*
-rwxrwxr-x 1 cat cat 88272 Jul 24 23:06 Screenshot_20210724-230346.png*
-rwxrwxr-x 1 cat cat 104779 Jul 24 23:06 Screenshot_20210724-230356.png*
-rwxrwxr-x 1 cat cat 63867 Jul 24 23:05 Screenshot_20210724-230408.png*
$ gimp -i -b '(gimp-image-crop "*.png" 1080 1728 0 65)' -b '(gimp-quit 0)'
batch command experienced an execution error:
Error: Invalid type for argument 1 to gimp-image-crop
$
It always returns Invalid type for argument 1 to gimp-image-crop.
I check in Procedure Browser in GIMP and it tells:
The argument 1 is "THE IMAGE". I also tried "1.png" and ".+png" but it always returns the same error: Error: Invalid type for argument 1 to gimp-image-crop
I don't understand which part I do wrong.
My OS is Linux Mint 20.2 Cinnamon and GIMP version is 2.10.18.
Please help.
The image argument of gimp-image-crop is the handle of a loaded image in Gimp, not an image file. So you have to gimp-file-load the image (this gives you a handle), apply your operations, and then gimp-file-save. Note that saving an image to a "flat" format (JPG, PNG...) is actually saving a layer, so if your image is more than one layer you may have to first create a layer to save using gimp-layer-new-from-visible.
This said, agreed that in most cases it is faster to use ImageMagick.

PBSPro qsub Output File Name for Job Array

It is desirable to have PBSPro std output files that are easily understood while making use of job arrays. I have not found a way.
Here are a set of jobs for discussion -
Job id Name User Time Use S Queue
---------------- ---------------- ---------------- -------- - -----
651902.srvname pl_0000 xxxxxxxxx 00:00:00 R large
651903[].srvname dp_0000-0001 xxxxxxxxx 0 H large
651904.srvname bp_0100 xxxxxxxxx 0 H large
651905[].srvname dp_0000-bpx6 xxxxxxxxx 0 H large
Running qsub without changing the output name results in files for each of the subjobs for job arrays. For example, the following files are produced for job 651905[]:
651905[1].srvname.OU
651905[2].srvname.OU
...
651905[x].srvname.OU
Using qsub -o [JOBNAME] which is known when launching, results in a single file for the job array id so that the std output is only available for one of the subjobs.
The desired output file set is:
dp_0000-bpx6[1].OU
dp_0000-bpx6[2].OU
...
dp_0000-bpx6[x].OU
How can this be accomplished? In other words, how can the output file name be set to something more understandable while preserving the array index?
A secondary question is how can I include the sequence number along with the job name? Something like -
dp_0000-bpx6.651905[1].OU
dp_0000-bpx6.651905[2].OU
...
dp_0000-bpx6.651905[x].OU
This won't get you fully there, but close.
qsub -J "0-512:512" -N pl_0000 -o pl_0000.^array_index^ -- /usr/bin/echo "HI"
Produces this output. You can see each array index with their own output.
-rw------- 1 pbsdev pbsdev 3 Apr 29 21:00 pl_0000.0
-rw------- 1 pbsdev pbsdev 3 Apr 29 21:00 pl_0000.512
-rw------- 1 pbsdev pbsdev 0 Apr 29 21:00 pl_0000.e1441.0
-rw------- 1 pbsdev pbsdev 0 Apr 29 21:00 pl_0000.e1441.512
This one will take the jobid and put it in the output filename along with the index.
jobid=$(qsub -h -J "0-512:512" -N pl_0000 -- /usr/bin/echo "HI") && qalter -o pl_0000.${jobid}.^array_index^.OU ${jobid} && qrls ${jobid}; echo ${jobid}
1446[].pdw-s1
Produces this output. You can see each jobid and array index with their own output.
drwxrwxr-x 2 root pbsdev 69632 Apr 29 21:05 .
drwxrwxr-x 6 root pbsdev 20480 Apr 29 20:49 ..
-rw------- 1 pbsdev pbsdev 3 Apr 29 21:05 pl_0000.1446[].pdw-s1.0.OU
-rw------- 1 pbsdev pbsdev 3 Apr 29 21:05 pl_0000.1446[].pdw-s1.512.OU
-rw------- 1 pbsdev pbsdev 0 Apr 29 21:05 pl_0000.e1446.0
-rw------- 1 pbsdev pbsdev 0 Apr 29 21:05 pl_0000.e1446.512
JOBID v v Array Index
pl_0000.1446[].pdw-s1.512.OU
You can add a -e and change the error output in the qalter. Note, the -h is needed to hold the job, so then qalter has time to modify it, then qrls releases it.

what does "deleted" mean in segfault error in syslog?

Mar 21 15:59:56 loong kernel: [ 364.376809] communicator[5399]: segfault at 273c6 ip 00000000000273c6 sp 00007fc0c8a15818 error 14 in communicator (deleted)[400000+29000]
When you delete a file currently in use, it gets marked deleted and is no longer listed but can still be used until the process currently having it open closes it.
$ cat > foo &
[1] 13566
$ rm foo
[1]+ Stopped cat > foo
$ ls foo
ls: cannot access foo: No such file or directory
$ ls -l /proc/13566/fd
total 0
lrwx------ 1 pterjan pterjan 64 Mar 22 21:17 0 -> /dev/pts/4
l-wx------ 1 pterjan pterjan 64 Mar 22 21:17 1 -> /tmp/foo (deleted)
lrwx------ 1 pterjan pterjan 64 Mar 22 21:17 2 -> /dev/pts/4
So, the communicator executable had been deleted before it crashed (maybe it was replaced by a new one during an update, or it was just deleted).

Passing Parameter To Bash Shell Script Through Input Redirection (<)

So, I need to write a shell script that accepts a file path as an argument and uses it within the shell script. I know that I could do something like:
$ ./script filename
With this solution, this would make it so that I could access this filename string using $1 in the shell script.However, I'm wondering if it is possible to use input redirection instead so I could pass the filename using it:
$ ./script < filename
If so, how would I access this filename in the shell script? I'm fairly new to shell script coding so I'm not sure if this is even possible.
You can extract the name of file passed with < by listing /proc/<pid>/fd directory, as follows:
ls -ltr /proc/$$/fd
Then
$ cat myscript.sh
ls -ltr /proc/$$/fd
$ ./myscript.sh < hello
total 0
lr-x------ 1 u g 64 Feb 25 08:42 255 -> /tmp/myscript.sh
lrwx------ 1 u g 64 Feb 25 08:42 2 -> /dev/pts/0
lrwx------ 1 u g 64 Feb 25 08:42 1 -> /dev/pts/0
lr-x------ 1 u g 64 Feb 25 08:42 0 -> /tmp/hello
I can't guess whether this is useful
And doesn't work when input is passed through a pipe
$ cat hello| ./myscript.sh
total 0
lr-x------ 1 u p 64 Feb 25 08:50 255 -> /tmp/myscript.sh
lrwx------ 1 u p 64 Feb 25 08:50 2 -> /dev/pts/0
lrwx------ 1 u p 64 Feb 25 08:50 1 -> /dev/pts/0
lr-x------ 1 u p 64 Feb 25 08:50 0 -> pipe:[82796]
Alternatively, you can use lsof and a little line handling to extract value
filename=$(/usr/sbin/lsof -p $$| grep " 0u"| cut -c 60-)
You cannot access the filename in the case of ./script < filename but only its contents. The reason is that the shell opens filename and set the corresponding file descriptor to be the standard input of the ./script.
With your second solution you don't access the filename, but your script's standard input is coming from the file.
With the first solution printing the file would look like this:
cat "$1"
With the second:
while read line; do echo "$line"; done

Resources