Why am I denied permission trying to shm_open? - linux

Consider the following C program:
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <sys/types.h>
#include <string.h>
int main() {
const char* name = "/memmap_ipc_shm";
int shmFd = shm_open(name, O_RDWR | O_CREAT, 0777);
if (shmFd < 0) {
fprintf(stderr,"bad shmfd opening %s: %s\n", name, strerror(errno));
return -1;
}
return 0;
}
When I run it on my GNU/Linux system (Devuan Beowulf, Linux 5.10.0-9, amd64 CPU), I get:
bad shmfd opening /memmap_ipc_shm: Permission denied
Why am I denied permission? I'm pretty sure I followed all the guidelines in the man shm_open page, my requested permissions seem ok - so what's wrong?

This may be happening when you have already created this shared memory object, but perhaps without the appropriate . Try calling shm_unlink(name) to ensure that's the case (or even do so as root if you encounter a permission failure again).
Note that this behavior seems to clash with the man page:
O_EXCL If O_CREAT was also specified, and a shared memory object with the given
name already exists, return an error. The check for the existence of the
object, and its creation if it does not exist, are performed atomically.

Related

Having trouble sending a message to ROS

I am pretty new in ROS. I am just trying to publish a message to a node in a linux server with this code:
#include "stdafx.h"
#include "ros.h"
#include <string>
#include <stdio.h>
#include <Windows.h>
using std::string;
int _tmain(int argc, _TCHAR * argv[])
{
ros::NodeHandle nh;
char *ros_master = "*.*.*.*";
printf("Connecting to server at %s\n", ros_master);
nh.initNode(ros_master);
printf("Advertising cmd_vel message\n");
string sent = "Hello robot";
ros::Publisher cmd_vel_pub("try", sent);
nh.advertise(cmd_vel_pub);
printf("All done!\n");
return 0;
}
The compiler gives me these errors:
Error C2664 'ros::Publisher::Publisher(ros::Publisher &&)': cannot convert argument 2 from 'std::string' to 'ros::Msg *' LeapMotion c:\users\vive-vr-pc\documents\visual studio 2015\projects\leapmotion\leapmotion\leapmotion.cpp 22
Error (active) no instance of constructor "ros::Publisher::Publisher" matches the argument list LeapMotion c:\Users\Vive-VR-PC\Documents\Visual Studio 2015\Projects\LeapMotion\LeapMotion\LeapMotion.cpp 22
I am on Visual Studio and there aren't a lot of tutorial from windows to linux, so I am confused on what to do. Many thanks for the help! :D
Take a look at the Hello World example. You cannot send types which are not defined as messages, i.e. std::string is not a ros message type. What you need is
#include <std_msgs/String.h>
Define and fill the string messages
std_msgs::String sent;
ros::Publisher cmd_vel_pub("try", &sent);
nh.advertise(cmd_vel_pub);
ros::Rate r(1); // once a second
sent.data = "Hello robot";
while (n.ok()){
cmd_vel_pun.publish(sent);
ros::spinOnce();
r.sleep();
}
Check out this blabbler example and these tutorials.

Why I couldn't use mount --bind /proc/<pid>/ns/mnt to another file in ubuntu?

Recently, I am learning about linux namespaces.
I hope even if the process ends, I could still attach the namespace that process is in.
Someone told I could use mount --bind /proc/<pid>/ns/pid ./pid to keep this file open. So I tried it.
When I mount uts, user, ipc, pid, net... They are all okay..
root#ubuntu:/home/jiashenh/workspace# touch uts
root#ubuntu:/home/jiashenh/workspace# mount --bind /proc/171/ns/uts ./uts
But when it comes to mnt.... I used it in the same way ...
root#ubuntu:/home/jiashenh/workspace# touch mnt
root#ubuntu:/home/jiashenh/workspace# mount --bind /proc/171/ns/mnt ./mnt
mount: wrong fs type, bad option, bad superblock on /proc/171/ns/mnt,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so
It failed.....So could anyone told me how to fix it?
.
Bind mount for /proc/<pid>/ns/mnt really doesn't work if called within the same mount namespace.
However, bind mount for ns/mnt works if:
mount() is called from a process with another mount namespace, and
mount() is called from a process where the ns/mnt being mounted is visible.
Both requirements are met if mount() is called from a process forked before unshare() call that created ns/mnt entry being mounted.
(Note that after unshare() call, previously visible ns/mnt entries are no more visible and become broken symlinks).
Here is a test:
#define _GNU_SOURCE
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <sys/types.h>
#include <sys/mount.h>
int main(int argc, char** argv) {
int fd = open("/tmp/mnt.ns", O_CREAT | O_RDWR);
close(fd);
if (argc > 1) {
if (fork() == 0) {
sleep(1);
} else {
unshare(CLONE_NEWNS);
wait(NULL);
return 0;
}
}
char src[PATH_MAX] = {};
snprintf(src, sizeof(src), "/proc/%u/ns/mnt", (unsigned)getppid());
if (mount(src, "/tmp/mnt.ns", NULL, MS_BIND, NULL) != 0) {
printf("mount failed: %s\n", strerror(errno));
return 1;
}
else {
printf("mount ok\n");
}
return 0;
}
This test fails without fork() and unshare():
$ sudo ./a.out
mount failed: Invalid argument
But works if it calls fork() and unshare() before mount() (note the x arg):
$ sudo ./a.out x
mount ok
See this commit for unshare tool and also this patch.
Unfortunately, this behaviour specific to ns/mnt is not documented in namespaces(7).

Linux alternative to _NSGetExecutablePath?

Is it possible to side-step _NSGetExecutablePath on Ubuntu Linux in place of a non-Apple specific approach?
I am trying to compile the following code on Ubuntu: https://github.com/Bohdan-Khomtchouk/HeatmapGenerator/blob/master/HeatmapGenerator2_Macintosh_OSX.cxx
As per this prior question that I asked: fatal error: mach-o/dyld.h: No such file or directory, I decided to comment out line 52 and am wondering if there is a general cross-platform (non-Apple specific) way that I can rewrite the code block of line 567 (the _NSGetExecutablePath block) in a manner that is non-Apple specific.
Alen Stojanov's answer to Programmatically retrieving the absolute path of an OS X command-line app and also How do you determine the full path of the currently running executable in go? gave me some ideas on where to start but I want to make certain that I am on the right track here before I go about doing this.
Is there a way to modify _NSGetExecutablePath to be compatible with Ubuntu Linux?
Currently, I am experiencing the following compiler error:
HeatmapGenerator_Macintosh_OSX.cxx:568:13: error: use of undeclared identifier
'_NSGetExecutablePath'
if (_NSGetExecutablePath(path, &size) == 0)
Basic idea how to do it in a way that should be portable across POSIX systems:
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
static char *path;
const char *appPath(void)
{
return path;
}
static void cleanup()
{
free(path);
}
int main(int argc, char **argv)
{
path = realpath(argv[0], 0);
if (!path)
{
perror("realpath");
return 1;
}
atexit(&cleanup);
printf("App path: %s\n", appPath());
return 0;
}
You can define an own module for it, just pass it argv[0] and export the appPath() function from a header.
edit: replaced exported variable by accessor method

ELF weak import / fallback stubs for glibc functions

I am trying to make our program runnable on some old Linux versions. One common import that prevents it is __longjmp_chk, added in glibc 2.11 but missing in older ones. One "solution" is to use -D_FORTIFY_SOURCE=0 but this turns off other fortify functions (__printf_chk etc) which are present in the target libc. Is there a way to make __longjmp_chk a "weak import" which would use the function from libc.so.6 if present, and fall back to local stub if not?
Is there a way to make __longjmp_chk a "weak import" which would use
the function from libc.so.6 if present, and fall back to local stub
if not?
I'd say yes, using dlsym() to check for __longjmp_chk and acting accordingly:
/* cc -ldl */
#define _GNU_SOURCE
#include <setjmp.h>
#include <stdio.h>
#include <dlfcn.h>
void __longjmp_chk(sigjmp_buf env, int val)
{
void (*p)(sigjmp_buf, int) = dlsym(RTLD_NEXT, "__longjmp_chk");
if (p)
printf("use the function from libc\n"),
p(env, val);
else
{
printf("falling back to local stub\n");
/* local stub - whatever that may be */
}
}
main()
{ // try it
sigjmp_buf env;
while (!setjmp(env)) __longjmp_chk(env, 1);
return 0;
}
I am trying to make our program runnable on some old Linux versions.
There are only a few ways to make this work, and most of them are enumerated here.
Is there a way to make __longjmp_chk a "weak import".
No.

How to tell if a file is on tmpfs given its path on Linux?

This might be a dumb question, but suppose I'm given a file path and I'd like to know if it points to a file on tmpfs (that is, it's an in-memory file). How can I do that using only Linux system calls? (That is, I can't go to the shell.)
Use the statfs syscall and see if the returned f_type field is TMPFS_MAGIC.
Here's a small utility demonstrating this:
#include <sys/vfs.h>
#include <linux/magic.h>
#include <stdio.h>
int main(int argc, char** argv) {
struct statfs info;
statfs(argv[1], &info);
if (info.f_type == TMPFS_MAGIC) {
printf("It's tmpfs\n");
return 0;
} else {
printf("It's not tmpfs\n");
return 1;
}
}
Example:
$ ./isittmpfs /etc/passwd
It's not tmpfs
$ ./isittmpfs /dev/shm/pulse-shm-1358569836
It's tmpfs
(NB: This is just an example of how to determine if a file is on tmpfs through syscalls. This answer does not suggest dropping to a shell even though the example code is invoked from a shell)

Resources