# A Linux program fails to create thread without root permissions



## aragats (Jul 25, 2020)

I installed Linux compatibility packages and tried to run commercial Beyond Compare program. It failed:

```
% bcompare
[FORMS.PP] ExceptionOccurred 
  Sender=EThread
  Exception=Failed to create new thread
  Stack trace:
  $000000000086FAD8   bcompare ($0000000000400000)   TTHREAD.SYSCREATE,  tthread.inc line 160
TApplication.HandleException Failed to create new thread
  Stack trace:
  $000000000086FAD8   bcompare ($0000000000400000)   TTHREAD.SYSCREATE,  tthread.inc line 160
Exception at 000000000086FAD8: EThread:
Failed to create new thread.
```
However, it perfectly works when launched as root. It's a Qt4 based software.
Any idea what can cause that? Any way to troubleshoot?
Thanks for directions!


----------



## shkhln (Jul 25, 2020)

aragats said:


> Any way to troubleshoot?



Trace it.


----------



## George (Jul 25, 2020)

If it works as root, but not as regular user, would this be an idea? `chmod a+s ./bcompare`


----------



## tingo (Jul 25, 2020)

Probably a resource problem. Something the program need access to, but can't get to as a normal user (because of permissions).


----------



## aragats (Jul 25, 2020)

shkhln said:


> Trace it.


Cannot set scheduler: "Operation not permitted". Why?
	
	



```
....
 38024 BCompare CALL  linux_sched_setscheduler(0x18a1a,0,0x810c06d38)
 38024 BCompare RET   linux_open 0
 38024 BCompare RET   linux_sched_setscheduler -1 errno -1 Operation not permitted
 38024 BCompare CALL  linux_tgkill(0x9488,0x18a1a,0x20)
 38024 BCompare CALL  linux_set_robust_list(0x810c069e0,0x18)
 38024 BCompare RET   linux_set_robust_list 0
 38024 BCompare PSIG  SIGRT1 caught handler=0x802806820 mask=0x0 code=SI_LWP
 38024 BCompare RET   linux_tgkill 0
....
```



Elazar said:


> would this be an idea?  chmod a+s ./bcompare


Setting SUID is a good work-around (may be dangerous though).
I'd prefer to find the root cause.


----------



## shkhln (Jul 25, 2020)

aragats said:


> Cannot set scheduler: "Operation not permitted". Why?



Probably a question for the bug tracker.



aragats said:


> ```
> ....
> 38024 BCompare CALL  linux_sched_setscheduler(0x18a1a,0,0x810c06d38)
> 38024 BCompare RET   linux_open 0
> ...



I see neither segfault nor thread ids (`kdump -H`).


----------



## aragats (Jul 25, 2020)

shkhln said:


> I see neither segfault nor thread ids


But who said it segfaults? It catches an exception.
The entire output of `kdump -H` is attached.
That scheduler error appears on the line 23095.
Thanks!


----------



## shkhln (Jul 25, 2020)

aragats said:


> But who said it segfaults?



No one. This is just the most common case of problems in C/C++.



aragats said:


> It catches an exception.



Although looks like this a filthy Delphi application, so in C terms this is more of an assertion check.


----------



## shkhln (Jul 25, 2020)

Install linux-c7-devtools and try

```
% cat nosched.c
int sched_setscheduler() {
  return 0;
}
% /compat/linux/bin/gcc -fPIC -shared nosched.c -o nosched.so
% env LD_PRELOAD="$PWD/nosched.so" BCompare
```

If that works, then this should be reported to the FreeBSD bug tracker.


----------



## aragats (Jul 25, 2020)

Looks like I'm missing something:
	
	



```
% env LD_LIBRARY_PATH=/opt/bc4/lib/beyondcompare env LD_PRELOAD=/opt/bc4/nosched.so /opt/bc4/lib/beyondcompare/BCompare
ld-elf.so.1: Shared object "libc.so.6" not found, required by "nosched.so"
ld-elf.so.1: Shared object "libc.so.6" not found, required by "nosched.so"
ld-elf.so.1: Shared object "libc.so.6" not found, required by "nosched.so"
ld-elf.so.1: Shared object "libc.so.6" not found, required by "nosched.so"
[FORMS.PP] ExceptionOccurred 
  Sender=EThread
  Exception=Failed to create new thread
  Stack trace:
  $000000000086FAD8   BCompare ($0000000000400000)   TTHREAD.SYSCREATE,  tthread.inc line 160
TApplication.HandleException Failed to create new thread
  Stack trace:
  $000000000086FAD8   BCompare ($0000000000400000)   TTHREAD.SYSCREATE,  tthread.inc line 160
Exception at 000000000086FAD8: EThread:
Failed to create new thread. 

% env LD_LIBRARY_PATH=/opt/bc4/lib/beyondcompare:/compat/linux/usr/lib64 env LD_PRELOAD=/opt/bc4/nosched.so /opt/bc4/lib/beyondcompare/BCompare
/bin/sh: line 1: 56726 Bus error               (core dumped) pgrep gnome- > /dev/null
/bin/sh: line 1: 56731 Bus error               (core dumped) pgrep kdesktop > /dev/null
/bin/sh: line 1: 56733 Bus error               (core dumped) pgrep plasma-desktop > /dev/null
/bin/sh: line 1: 56735 Bus error               (core dumped) pgrep xfdesktop > /dev/null
[FORMS.PP] ExceptionOccurred 
  Sender=EThread
  Exception=Failed to create new thread
  Stack trace:
  $000000000086FAD8   BCompare ($0000000000400000)   TTHREAD.SYSCREATE,  tthread.inc line 160
TApplication.HandleException Failed to create new thread
  Stack trace:
  $000000000086FAD8   BCompare ($0000000000400000)   TTHREAD.SYSCREATE,  tthread.inc line 160
Exception at 000000000086FAD8: EThread:
Failed to create new thread.

% env LD_LIBRARY_PATH=/opt/bc4/lib/beyondcompare:/compat/linux/usr/lib env LD_PRELOAD=/opt/bc4/nosched.so /opt/bc4/lib/beyondcompare/BCompare
ld-elf.so.1: /compat/linux/usr/lib/libc.so.6: unsupported file layout
ld-elf.so.1: /compat/linux/usr/lib/libc.so.6: unsupported file layout
ld-elf.so.1: /compat/linux/usr/lib/libc.so.6: unsupported file layout
ld-elf.so.1: /compat/linux/usr/lib/libc.so.6: unsupported file layout
[FORMS.PP] ExceptionOccurred 
  Sender=EThread
  Exception=Failed to create new thread
  Stack trace:
  $000000000086FAD8   BCompare ($0000000000400000)   TTHREAD.SYSCREATE,  tthread.inc line 160
TApplication.HandleException Failed to create new thread
  Stack trace:
  $000000000086FAD8   BCompare ($0000000000400000)   TTHREAD.SYSCREATE,  tthread.inc line 160
Exception at 000000000086FAD8: EThread:
Failed to create new thread.
```


----------



## tingo (Jul 25, 2020)

So, this is a file compare program (with a lot of fancy words in the advertising). Makes me wonder if this exercise (trying to get it to run on FreeBSD) is going to be worth it at all. Perhaps time is better spent looking for a file compare program that already works on FreeBSD. Just my 0.02 eurocents.


----------



## shkhln (Jul 25, 2020)

Here you go:

```
% cat nosched.c
#define _GNU_SOURCE

#include <dlfcn.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>

int pthread_noop() {
  fprintf(stderr, "%s(...)\n", __func__);
  return 0;
}

// https://stackoverflow.com/questions/15599026/how-can-i-intercept-dlsym-calls-using-ld-preload/18825060#18825060
void* _dl_sym(void*, const char*, void*);

static void* (*libc_dlsym)(void*, const char*) = NULL;

void* dlsym(void* handle, const char* symbol) {

  if (!libc_dlsym) {
    libc_dlsym = _dl_sym(RTLD_NEXT, "dlsym", dlsym);
  }

  //~ fprintf(stderr, "%s(_, %s)\n", __func__, symbol);

  if (strcmp(symbol, "pthread_attr_setinheritsched") == 0) {
    return pthread_noop;
  }

  return libc_dlsym(handle, symbol);
}
% /compat/linux/bin/gcc -Wall -fPIC -shared nosched.c -ldl -o nosched.so
% env LD_LIBRARY_PATH=/opt/bc4/lib/beyondcompare LD_PRELOAD=/opt/bc4/nosched.so /opt/bc4/lib/beyondcompare/BCompare
```

EDIT: removed accidental -m32


----------



## shkhln (Jul 25, 2020)

tingo said:


> So, this is a file compare program (with a lot of fancy words in the advertising). Makes me wonder if this exercise (trying to get it to run on FreeBSD) is going to be worth it at all. Perhaps time is better spent looking for a file compare program that already works on FreeBSD. Just my 0.02 eurocents.



It's a very basic exercise.


----------



## aragats (Jul 25, 2020)

This works, thanks shkhln !
So, what's the conclusion?


----------



## shkhln (Jul 25, 2020)

Just file a bug report.



aragats said:


> So, what's the conclusion?



We have confirmed that linux_sched_setscheduler is indeed the issue there. No more, no less.


----------



## aragats (Jul 25, 2020)

shkhln said:


> Just file a bug report


Okay, thanks! Will do.


----------



## aragats (Jul 25, 2020)

tingo said:


> if this exercise (trying to get it to run on FreeBSD) is going to be worth it at all


Well, since FreeBSD officially supports Linux executables, why shouldn't we improve it by understanding and fixing issues?


tingo said:


> Perhaps time is better spent looking for a file compare program that already works on FreeBSD


There is a few arguments you may or may not accept:
1. Beyond Compare is a _de facto_ standard source comparison tool in the company I work for. It is used in all Linux and Windows boxes within the company.
2. In terms of _productivity_ it is really useful for large hierarchical projects or for entire filesystems comparisons for embedded systems.
3. BC automatically detects and compares text, binary and graphics.

In everyday activities I use regular diff(). For the GUI I couldn't find anything meaningful, e.g. textproc/meld works fine, but it's a toy compared to BC...


----------



## tingo (Jul 26, 2020)

aragats said:


> Well, since FreeBSD officially supports Linux executables, why shouldn't we improve it by understanding and fixing issues?


Of course, that is a valid reason.


aragats said:


> There is a few arguments you may or may not accept:
> 1. Beyond Compare is a _de facto_ standard source comparison tool in the company I work for. It is used in all Linux and Windows boxes within the company.
> 2. In terms of _productivity_ it is really useful for large hierarchical projects or for entire filesystems comparisons for embedded systems.
> 3. BC automatically detects and compares text, binary and graphics.


Your reason number 1 is a very good reason for this exercise. 
If your initial post had included a reason for this exercise, I wouldn't have written my "perhaps" post.


----------



## shkhln (Aug 23, 2020)

aragats, did you eventually report this?


----------



## drhowarddrfine (Aug 24, 2020)

aragats said:


> Well, since FreeBSD officially supports Linux executables, why shouldn't we improve it by understanding and fixing issues?


FreeBSD does not support Linux executables. It only runs them.


----------



## shkhln (Aug 24, 2020)

s/runs/temporarily tolerates/


----------



## matias (Oct 4, 2020)

Thanks aragats & shkhln I have consolidated your fixes and my bcompare installation script into a port.
There are some core dumps happening in the background still, but it doesn't prevent correct execution.
I submitted it last week and it should land into the ports repo someday.
It's also on github in the meantime









						GitHub - matias-pizarro/freebsd-ports-bcompare: Port Beyond Compare to FreeBSD
					

Port Beyond Compare to FreeBSD. Contribute to matias-pizarro/freebsd-ports-bcompare development by creating an account on GitHub.




					github.com
				








						249919 – [NEW PORT] editors/linux-bcompare: compare, sync, and merge files and folders (X11)
					






					bugs.freebsd.org


----------

