sending message to printk buffer from user space -- not working - linux

Actually i want my driver messages to reach my terminal for debugging purpose. So i just try to check by following below link.
I refred following link :--
http://elinux.org/Debugging_by_printing
I am using a ubuntu in side vmplayer virtual machine. Ubuntu is running in terminal mode inside virtual machine.
I am trying to send some message to kernel printk buffer, buts echo command fails.
klog demon is also running i confirmed with following command .
ps aux | grep klogd
Cat command on proc printk entry :---
# cat /proc/sys/kernel/printk
4 4 1 7
run echo command :---
#sudo echo "<1>Writing critical printk messages from userspace" >/dev/kmsg
But i am not able to get the message on the terminal. I am getting following error when runs above command :--
-bash: /dev/kmsg: Permission denied
Please suggest how to print on console ?
my actual requirement is to ... send messages of printk() in my driver ... directly to my console. I am just testing here from my console that messages of low priority reaches console or not .
how this post is right then .. ?
linux kprint messages on console
Please suggest.

The error is because the shell is the thing trying to write to /dev/kmsg (via the redirect), and it is not being run with sudo. Also, by default echo is usually a shell builtin, not a binary that can executed in another process, though that's kind of irrelevant here. The right way to do this is
echo "blah" | sudo tee /dev/kmsg
tee is a command that copies stdin to a file and stdout. It's called tee because it's like a T-shaped pipe in a pipeline.

Related

strange adb behaviour if script is piped into bash

I stumpled upon a strange behaviour that I can not explain. I tried to narrow down the problem. I have the following test testscript.sh script:
echo before
adb shell ls
echo after
If I run the script with bash -x testscript.sh, everything works as expected and I get the following output:
+ echo before
before
+ adb shell ls
acct
bin
bugreports
...
sdcard
storage
sys
system
ueventd.rc
vendor
+ echo before
before
But if I run the script as piped script with cat testscript.sh | bash -sx, I get the following output:
+ echo before
before
+ adb shell ls
acct
bin
bugreports
...
sdcard
storage
sys
system
ueventd.rc
vendor
The last echo after is not executed, and I can not figure out why. The script is running on an Ubuntu server 18.04. The adb is the one from the official Ubuntu package.
$ adb --version
Android Debug Bridge version 1.0.39
Version 1:8.1.0+r23-5~18.04
Installed as /usr/lib/android-sdk/platform-tools/adb
$ bash --version
GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
Please could someone enlight me, what is happening here.
When you run a script with bash scriptname, standard input of all the commands it runs is still connected to the terminal. So adb will read its standard input from the terminal.
When you redirect the input of bash, this redirection is inherited by adb. Unless you use the -n option to adb shell, it will read additional input from standard input and send it to the remote system as possible input for the command you run (it doesn't know that ls doesn't read standard input).
Change it to
adb shell -n ls

How to log the live output of a running process

I want to run a game server inside my Ubuntu machine. I want to run it in the background and write the live output of that process inside a log file. I tried using nohup and running the game server using "&" at the end but I couldn't make it work the way I wanted.
Then I started reading about named pipes and actually gave it a go. I made a simple script that in theory should work. But, of course I am missing something.
First, I made a pipe using the mkfifo command.
mkfifo testpipe
Then I created a small script:
#!/bin/bash
./mta-server64 > pipe &
pid=$!
echo $pid // so I know the pid of the process
cat < pipe > log.txt &
(Note: I wrote this code from memory.)
The code works only when there is an error and the process stops. It actually records the game console error. But when the game server is running I get no output in the log file.
I want to read the output (stdout and stderr if I am not mistaken) of a process running in background and record it those inside a log file.
I also thought about using screen as it logs everything inside a file but I would prefer not using it if there is a better solution.
EDIT:
First of all: thank you for the interest you had in helping me. In the same way, I have to apologize for only giving scarce details about what I intend to do with this small project and for my limited understanding of stdout and stderr.
Let's go to the first base.
I want to run a game server named Multi Theft Auto (https://multitheftauto.com/). This is GTA San Andreas but multiplayer.
I can easily run this game server in my Ubuntu server by calling the executable ./mta-server-64. After calling it the game server console appears:
[|] MTA: San Andreas :: 0/32 players :: 196 resources :: 125 fps (25)
MTA:BLUE Server for MTA:SA
==================================================================
= Multi Theft Auto: San Andreas v1.5.6 [64 bit]
==================================================================
= Server name : Default MTA Server
= Server IP address: auto
= Server port : 22884
=
= Log file : /root/mta/mods/deathmatch/logs/server.log
= Maximum players : 32
= HTTP port : 22564
= Voice Chat : Disabled
= Bandwidth saving : Medium
==================================================================
[09:49:07] Resource 'mapmanager' requests some acl rights. Use the command 'aclrequest list mapmanager'
[09:49:07] Resources: 196 loaded, 0 failed
[09:49:07] Starting resources...
[09:49:07] Server minclientversion is now 1.5.6-9.16588.0
[09:49:07] INFO: MAPMANAGER: Some important ACL permissions are missing. To ensure the correct functioning of Mapmanager, please write: aclrequest allow mapmanager all
[09:49:07] Gamemode 'play' started.
[09:49:07] Authorized serial account protection is enabled for the ACL group(s): `Admin` See http://mtasa.com/authserial
[09:49:07] WARNING: <owner_email_address> not set
[09:49:07] Server started and is ready to accept connections!
[09:49:07] To stop the server, type 'shutdown' or press Ctrl-C
[09:49:07] Type 'help' for a list of commands.
[09:49:07] Querying MTA master server... success! (Auto detected IP:xxx.xxx.xxx.xxx)
I am using the following script to run the process in the background and (try to) get the live output from:
#!/bin/bash
newport=$(shuf -i 22003-22900 -n 1)
newip=$(shuf -i 22003-22900 -n 1)
rm -rf ~/server/*
cp -r /home/user*/ftp/server/mtaserver/serverfiles/* ~/server
sed -i "s/<httpport>[0-9][0-9][0-9][0-9][0-9]<\/httpport>/<httpport>$newport<\/httpport>/g" ~/server/mods/deathmatch/mtaserver.conf
sed -i "s/<serverport>[0-9][0-9][0-9][0-9][0-9]<\/serverport>/<serverport>$newip<\/serverport>/g" ~/server/mods/deathmatch/mtaserver.conf
~/server/mta-server64 2>&1 | tee -a outfile &
mta_pid=$!
echo $mta_pid
sleep 6
pkill $mta_pid
(Note: Because of some technical problems I had to add the first few lines of script which automatically replace the game files with new ones and also replace the existing ports with random ones.)
This script starts the server and tries to log the output of the process. The process is automatically killed after few seconds so there is only one instance of the game server at any given time.
THE ISSUE:
This script only logs the output if there is an error. I still cannot get the live output of the process when it is still running. Maybe this is an issue with the game server but truly believe there should be a way to make it work the way I intend.
I believe you want to use tee command to split the pipe output to log file.
I suggest you read this article and these answers 1 2.
Usually this is enough nohup somecommand > somecommand.log 2>&1 & then, tail -F somecommand.log to follow the logs.
After 2 days I finally figured out a way to make it work (the way I intended to work, without taking in consideration any major security/performance risks).
Reading the comments made me realize I was attacking the wrong point. The stdout of the game server is buffered, thus making it impossible to log it into a log file using the methods I tried when I posted my question At least this is what I came to understand).
I did some research on how to run the application without having the stdout buffered: https://serverfault.com/questions/294218/is-there-a-way-to-redirect-output-to-a-file-without-buffering-on-unix-linux
My code now:
stdbuf -o0 ~/server/mta-server64 >> pipe &
cat < pipe | tee -a outfile &
After creating the named pipe it executes the game server inside that pipe and then appends the stdout into the log file.
The stdbug -o0 command disables the stdout buffering (as noted in the link above).
This works for me and I cannot guarantee it will work for anybody else. I am still not aware if disabling the buffering is a safe approach to my issue but for now it is what I need.

screen logging not working over ssh terminal

I want to run "screen" on a debian linode server, starting up over a ssh terminal window. I'd like a shell script to start and detach a screen, so that a process can continue when I log off. I'd also like the logging file screenlog.0 to be produced, so that there's a record if the process crashes.
But there's a problem in getting the log file to write. Locally, on a mac terminal window,
% screen -dm -L sh -c 'echo hello'
works fine, "hello" gets written to screenlog.0. But the same command issued in a ssh window to the server executes, but nothing gets written.
However, if in that window I go into screen,
% screen -L
and then do some stuff, the activity is written to screenlog.0 (on the server).
What am I missing?
It turns out that the screen() command can have problems. The above command sends no output to screenlog.0 under 'Debian GNU/Linux 9 (stretch)' , while 'Ubuntu 14.04.1 LTS' writes the odd message, "error: could not start server! try running as root!", to screenlog.0, even when running as root. 'Linux Mint 18.1' and MacOSX run correctly.
I was advised to use the venerable unix command "nohup" to solve my problem of detaching a process and logging its output, even when you close the ssh connection. Ordinarily, when you close a terminal window, the signal SIGHUP is sent to any processes that were started there. But
% nohup myprog > logfile.txt &
works perfectly. Old way, good way.

Getting stty: standard input: Inappropriate ioctl for device when using scp through an ssh tunnel

Per the title, I'm getting the following warning when I try to scp through an ssh tunnel. In my case, I cannot scp directly to foo because port 1234 on device foo is being forwarded to another machine bar on a private network (and bar is the machine that is giving me a tunnel to 192.168.1.23).
$ # -f and -N don't matter and are only to run this example in one terminal
$ ssh -f -N -p 1234 userA#foo -L3333:192.168.1.23:22
$ scp -P 3333 foo.py ubuntu#localhost:
ubuntu#localhost's password:
stty: standard input: Inappropriate ioctl for device
foo.py 100% 1829 1.8KB/s 00:00
Does anyone know why I might be getting this warning about Inappropriate ioctl for device?
I got the exact same problem when I included the following line on my ~/.bashrc:
stty -ixon
The purpose of this line was to allow the use of Ctrl-s in reverse search of bash.
This gmane link has a solution: (original link dead) => Web Archive version of gmane link
'stty' applies to ttys, which you have for interactive login sessions.
.kshrc is executed for all sessions, including ones where stdin isn't
a tty. The solution, other than moving it to your .profile, is to
make execution conditional on it being an interactive shell.
There are several ways to check for interecative shell. The following solves the problem for bash:
[[ $- == *i* ]] && stty -ixon
Got the same issue while executing the script remotely. After many tries didn't get any luck to solve this error. Then got an article to run a shell script through ssh. This was an issue related to ssh, not any other command. ssh -t "command" -t will allocate a pseudo TTY to the ssh and this error won't come.
at the end i created a blank .cshrc file ( for ubuntu 18.04). worked

sudo not working correctly after some time

I have Linux server (CentOS release 6.4) which is able to process source code sent by users. On the server is a Java application which starts a bash script which will run compilation and execution commands of these source codes in a limited way (time and memory are limited, no Internet, executed by limited user).
The Java program must be always be running, so it can register new job requests.
When started, the Java program works fine, but after some time (talking in days), commands are not executed properly. I get the following error message:
sudo: sorry, you must have a tty to run sudo
the line which is causing that is:
sudo -u codiana $COMMAND &
where $COMMAND is command to execute along with its arguments
After application restart (kill and start again) everything works.
Is there some time limit on Linux which can cause that?
You can comment /etc/sudoers:
#Defaults requiretty
Edit:
man sudoers | grep requiretty -A 5
requiretty If set, sudo will only run when the user is logged in
to a real tty. When this flag is set, sudo can only be
run from a login session and not via other means such
as cron(8) or cgi-bin scripts. This flag is off by
default.
So if this is not desired open /etc/sudoers with you text editor of choice and comment out this line.

Resources