No permissions to change tty mode inside a docker container - linux

I'm trying to run some software inside a docker container, which wants to do VT_SETMODE for the tty currently in use. This will always fail with an error saying "Operation not permitted".
I've tried playing around with permissions/groups with no luck.
Finally I created a small snippet to reproduce the error:
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/vt.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
int main() {
const char *tty_path;
tty_path = "/dev/tty1";
int fd = open(tty_path, O_RDWR | O_CLOEXEC);
if (fd < 0) {
printf("ERROR: Failed to open %s\n", strerror(errno));
return 1;
}
struct vt_mode mode = {
.mode = VT_AUTO,
};
errno = 0;
ioctl(fd, VT_SETMODE, &mode);
if (errno) {
printf("ERROR: %s\n", strerror(errno));
return 1;
}
return 0;
}
I ran the code inside a docker container with a simple dockerfile:
FROM archlinux/base
RUN pacman -Sy --noconfirm gcc
That was started with command:
docker build -f Dockerfile -t tty-test . && docker run --device /dev/tty1 -v $HOME/tty-test:/volume -it tty-test /bin/bash -c 'cd /volume && gcc tty_r.c && ./a.out ; /bin/bash'
Output is this:
ERROR: Operation not permitted
Can anyone explain why the tty is not accessible from the container, or is there some way to give more control to the container?

Okay it seems that I have solved my own problem. I was trying to run the code inside a container which had no privilege to do tty-configuration. Adding --cap-add SYS_TTY_CONFIG to the docker run command fixed the issue.

Related

ubuntu18.04 vscode kernel module program ERROR: identifier "KBUILD_MODNAME" is undefine

My environment: Ubuntu 18.04, kernel: linux-5.3.0-53 vscode: lastest
Command for fixing linux/module.h not find asm/xxx.h
cd /usr/src/linux-headers-5.3.0-53/include
sudo ln -s asm-generic/ asm
My c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include",
"/usr/local/include",
"/usr/src/linux-headers-5.3.0-53/include",
"/usr/src/linux-headers-5.3.0-53-generic/include",
"/usr/src/linux-headers-5.3.0-53/arch/x86/include",
"/usr/src/linux-headers-5.3.0-53/include/uapi",
"/usr/lib/gcc/x86_64-linux-gnu/7.5.0/include"
],
"defines": [
"__GNUC__",
"__KERNEL__"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "gnu++14",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
My hello_module.c, a simple Linux kernel module program.
//a simple linux kernel module program.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL"); //error here
static int hello_init(void){
printk(KERN_ALERT "hello world\n");
return 0;
}
static void hello_exit(void){
printk(KERN_ALERT "goodbye world\n");
}
module_init(hello_init);
module_exit(hello_exit);
then ERROR:
identifier "KBUILD_MODNAME" is undefined
How to fix? Please help me..
KBUILD_MODNAME is automatically defined by the Makefile when you build your kernel module, so you will not find it anywhere in the kernel header files.
To stop vscode from complaining about undefined KBUILD_MODNAME identifier, just add a dummy definition to your c_cpp_properties.json file in the "defines" section
"defines": [
"KBUILD_MODNAME=\"hello_module\"",
"__GNUC__",
"__KERNEL__"
],
That should make vscode happy =)
The value you define for KBUILD_MODNAME does not really matter, since it is automatically defined during the make process.
I have not understood if this is your only problem, or if you also have problems to compile the module. If yes let me know and we will figure out how to solve that too!

Getting error LNK2019: unresolved external symbol when compiling SDL2 code in Windows using MSVC

The complete error output:
SDL2main.lib(SDL_windows_main.obj) : error LNK2019: unresolved external symbol __imp_CommandLineToArgvW referenced in function main_getcmdline
My compiler options:
cl -D WINDOWS -nologo -W4 -WX -wd4100 -Fe"output_file.exe" input_file.c SDL2.lib SDL2main.lib -I ./SDL2-2.0.12/include -link -LIBPATH:./SDL2-2.0.12/lib/x64 -SUBSYSTEM:CONSOLE
My input_file.c header and main function:
#ifdef LINUX
#include <SDL2/SDL.h> /* Comes with stdio.h and stdlib.h */
#elif WINDOWS
#include <stdio.h>
#include "SDL.h"
#endif
int main(int argc, char* argv[]) {
if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER) != 0)
{
fprintf(stderr, "Failed to initialize SDL: %s\n", SDL_GetError());
return -1;
}
. . .
The SDL development folder used in the command line arguments is the development libraries zip file downloaded from libsdl.org
Why am I getting this error?
you also need to use the library called "shell32.lib"
cl -D WINDOWS -nologo -W4 -WX -wd4100 -Fe"output_file.exe" input_file.c shell32.lib SDL2.lib SDL2main.lib -I ./SDL2-2.0.12/include -link -LIBPATH:./SDL2-2.0.12/lib/x64 -SUBSYSTEM:CONSOLE

Building pjSip 2.5 - unable to undefined reference to 'pj_jni_jvm'

Trying to build pjSIP from sources for Android, using following guide - https://trac.pjsip.org/repos/wiki/Getting-Started/Android
Checkout sources of pjsip (2.5)
export ANDROID_NDK_ROOT=/home/dev/android/android-ndk-r9b
export TARGET_ABI=armeabi
export APP_PLATFORM=android-13
./configure-android --use-ndk-cflags --disable-video --disable-l16-codec --disable-gsm-codec --disable-g722-codec --disable-g7221-codec --disable-ssl --disable-ilbc-codec --disable-tls
configuration is completed successfully.
make dep && make clean && make
And I got following error - >
../src/pj/guid_android.c:43: error: undefined reference to 'pj_jni_jvm'
../src/pj/guid_android.c:117: error: undefined reference to 'pj_jni_jvm'
The command invoked by linked which leads to this error
/home/dev/android/android-ndk-r9b/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc -o ../bin/pjlib-test-arm-unknown-linux-androideabi \
output/pjlib-test-arm-unknown-linux-androideabi/main.o output/pjlib-test-arm-unknown-linux-androideabi/activesock.o output/pjlib-test-arm-unknown-linux-androideabi/atomic.o output/pjlib-test-arm-unknown-linux-androideabi/echo_clt.o output/pjlib-test-arm-unknown-linux-androideabi/errno.o output/pjlib-test-arm-unknown-linux-androideabi/exception.o output/pjlib-test-arm-unknown-linux-androideabi/fifobuf.o output/pjlib-test-arm-unknown-linux-androideabi/file.o output/pjlib-test-arm-unknown-linux-androideabi/hash_test.o output/pjlib-test-arm-unknown-linux-androideabi/ioq_perf.o output/pjlib-test-arm-unknown-linux-androideabi/ioq_udp.o output/pjlib-test-arm-unknown-linux-androideabi/ioq_unreg.o output/pjlib-test-arm-unknown-linux-androideabi/ioq_tcp.o output/pjlib-test-arm-unknown-linux-androideabi/list.o output/pjlib-test-arm-unknown-linux-androideabi/mutex.o output/pjlib-test-arm-unknown-linux-androideabi/os.o output/pjlib-test-arm-unknown-linux-androideabi/pool.o output/pjlib-test-arm-unknown-linux-androideabi/pool_perf.o output/pjlib-test-arm-unknown-linux-androideabi/rand.o output/pjlib-test-arm-unknown-linux-androideabi/rbtree.o output/pjlib-test-arm-unknown-linux-androideabi/select.o output/pjlib-test-arm-unknown-linux-androideabi/sleep.o output/pjlib-test-arm-unknown-linux-androideabi/sock.o output/pjlib-test-arm-unknown-linux-androideabi/sock_perf.o output/pjlib-test-arm-unknown-linux-androideabi/ssl_sock.o output/pjlib-test-arm-unknown-linux-androideabi/string.o output/pjlib-test-arm-unknown-linux-androideabi/test.o output/pjlib-test-arm-unknown-linux-androideabi/thread.o output/pjlib-test-arm-unknown-linux-androideabi/timer.o output/pjlib-test-arm-unknown-linux-androideabi/timestamp.o output/pjlib-test-arm-unknown-linux-androideabi/udp_echo_srv_sync.o output/pjlib-test-arm-unknown-linux-androideabi/udp_echo_srv_ioqueue.o output/pjlib-test-arm-unknown-linux-androideabi/util.o -lpj-arm-unknown-linux-androideabi -nostdlib -L/home/dev/android/android-ndk-r9b/platforms/android-13/arch-arm/usr/lib -L/home/dev/android/android-ndk-r9b/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi/ -lm /home/dev/android/android-ndk-r9b/platforms/android-13/arch-arm/usr/lib/crtbegin_so.o -lgnustl_static -lc -lgcc -ldl -lOpenSLES -llog -L/home/dev/android/pjproject-2.5/pjlib/lib -L/home/dev/android/pjproject-2.5/pjlib-util/lib -L/home/dev/android/pjproject-2.5/pjnath/lib -L/home/dev/android/pjproject-2.5/pjmedia/lib -L/home/dev/android/pjproject-2.5/pjsip/lib -L/home/dev/android/pjproject-2.5/third_party/lib -nostdlib -L/home/dev/android/android-ndk-r9b/platforms/android-13/arch-arm/usr/lib -L/home/dev/android/android-ndk-r9b/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi/
Code of guid_androind.c
#include <pj/guid.h>
#include <pj/log.h>
#include <pj/string.h>
#include <jni.h>
extern JavaVM *pj_jni_jvm;
static pj_bool_t attach_jvm(JNIEnv **jni_env)
{
if ((*pj_jni_jvm)->GetEnv(pj_jni_jvm, (void **)jni_env,
JNI_VERSION_1_4) < 0)
{
if ((*pj_jni_jvm)->AttachCurrentThread(pj_jni_jvm, jni_env, NULL) < 0)
{
jni_env = NULL;
return PJ_FALSE;
}
return PJ_TRUE;
}
return PJ_FALSE;
}
#define detach_jvm(attached) \
if (attached) \
(*pj_jni_jvm)->DetachCurrentThread(pj_jni_jvm);
Struggling with this error for whole day, any clue is highly appreciated. Meanwhile I'm able to build version 2.3 without any errors, but not 2.5
I believe you missed a step.
https://trac.pjsip.org/repos/wiki/Getting-Started/Android#BuildPreparation
Set your config_site.h to the following:
/* Activate Android specific settings in the 'config_site_sample.h' */
#define PJ_CONFIG_ANDROID 1
#include <pj/config_site_sample.h>

How can I write to TTY from a kernel module?

First post to SO, so I'll try to make the question right.
I'm making a simple Linux kernel module with the goal of echoing data back to the TTY shell from where the kernel module was loaded. The problem I having is the kernel "Ooops"-ing with the following message (caught with " watch 'dmesg | tail -50' "). The kernel module's name is Seraphim:
[ 184.222748] SELinux: initialized (dev proc, type proc), uses genfs_contexts
[ 1877.456607] seraphim: module verification failed: signature and/or required key missing - tainting kernel
[ 1877.457321] ------------------
[ 1877.457324] Seraphim started.
[ 1877.457348] BUG: unable to handle kernel NULL pointer dereference at 0000000000000218
[ 1877.457411] IP: [<ffffffffa0012030>] seraphim_entry+0x30/0x1000 [seraphim]
[ 1877.457462] PGD 115a2e067 PUD 10aca8067 PMD 0
[ 1877.457498] Oops: 0000 [#1] SMP
[ 1877.457524] Modules linked in: seraphim(OF+) rfcomm bnep nf_conntrack_netbios_ns nf_conn track_broadcast ipt_MASQUERADE ip6t_REJECT xt_conntrack ebtable_nat ebtable_broute bridge stp llce btable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_ma etc.
The code used for writing data to the TTY terminal follows:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/tty.h>
static void printString(char *string) {
struct tty_struct *tty;
tty = current->signal->tty;
if(tty != NULL) {
(tty->driver->ops->write) (tty, string, strlen(string));
}
else
printk("tty equals to zero");
}
What am I doing wrong?
I was following the tutorial at http://www.tldp.org/LDP/lkmpg/2.6/lkmpg.pdf but it was out of date (the kernel I am using is 3.11.10-200 on Fedora 19), so I had to rummage through 3.11.10-200 source files to find the adequate structures.
use tty = get_current_tty(); instead of tty = current->signal->tty;
that's it
you need to lock the tty before accessing it and get_current_tty does it internally
NOTE: get_current_tty is under EXPORT_SYMBOL_GPL, hence your module or code

Visual C++ MySQL Connector/C errors?

My source code results in this:
// MySQL.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <winsock.h>
#include <string>
#include <sstream>
#include <stdio.h>
#include <mysql.h>
#define TABLE_OF_INTEREST "some_table"
#define SERVER_NAME "mysql_server"
#define DB_USER "user"
#define DB_USERPASS "pa55w0rd"
#define DB_NAME "db_name"
#define NEWLINE "\n"
using namespace std;
int main()
{
MYSQL *hnd = NULL; // mysql connection handle
const char *sinf = NULL; // mysql server information
if(!mysql_real_connect(hnd, SERVER_NAME, DB_USER, DB_USERPASS, DB_NAME, 0, NULL, 0)
cout << "Something happened.";
return 0;
}
error:
1>------ Build started: Project: MySQL, Configuration: Release Win32 ------
1> MySQL.cpp
1>MySQL.obj : error LNK2001: unresolved external symbol _mysql_real_connect#32
1>c:\users\josiah\documents\visual studio 2010\Projects\MySQL\Release\MySQL.exe : fatal error LNK1120: 1 unresolved external
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I don't think there issues with the source, I just need help on how to figure out where the problem is.
You're missing a reference to a lib. The C connector should have come with a .lib, add it to the additional input for the linker

Resources