Skip to content

Commit ee23dd1

Browse files
pthread_setcancelstate.c: re-implementation
Signed-off-by: DreamConnected <1487442471@qq.com>
1 parent 33c5a68 commit ee23dd1

6 files changed

Lines changed: 61 additions & 19 deletions

File tree

meson.build

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,7 @@ if have
497497
endif
498498
endif
499499

500-
## NDK doesn't implement thread_setcancelstate functions, and is only used as a placeholder here
501-
have = cc.has_function('pthread_kill', prefix: '#include <pthread.h>')
500+
have = cc.has_function('pthread_setcancelstate', prefix: '#include <pthread.h>')
502501
srcconf.set10('HAVE_PTHREAD_SETCANCELSTATE', have)
503502

504503
have = cc.has_function('rand_r')

src/include/getgrgid_r.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,9 @@
5656

5757
#define FIX(x) (gr->gr_##x = gr->gr_##x - line + buf)
5858

59-
#define SIG_CANCEL_SIGNAL SIGUSR1
60-
#define PTHREAD_CANCEL_ENABLE 1
61-
#define PTHREAD_CANCEL_DISABLE 0
62-
63-
static int pthread_setcancelstate(int state, int *oldstate) {
64-
sigset_t new, old;
65-
int ret;
66-
sigemptyset (&new);
67-
sigaddset (&new, SIG_CANCEL_SIGNAL);
68-
69-
ret = pthread_sigmask(state == PTHREAD_CANCEL_ENABLE ? SIG_BLOCK : SIG_UNBLOCK, &new , &old);
70-
if(oldstate != NULL)
71-
{
72-
*oldstate =sigismember(&old,SIG_CANCEL_SIGNAL) == 0 ? PTHREAD_CANCEL_DISABLE : PTHREAD_CANCEL_ENABLE;
73-
}
74-
return ret;
75-
}
59+
#if !HAVE_PTHREAD_SETCANCELSTATE
60+
#include <pthread_ext.h>
61+
#endif
7662

7763
static unsigned atou(char **s)
7864
{

src/include/meson.build

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,9 @@ if srcconf.get('HAVE_HASMNTOPT') == 0
4444
'hasmntopt.c',
4545
'hasmntopt.h')
4646
endif
47+
48+
if srcconf.get('HAVE_PTHREAD_SETCANCELSTATE') == 0
49+
include_sources += files(
50+
'pthread_setcancelstate.c',
51+
'pthread_ext.h')
52+
endif

src/include/pthread_ext.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* liblxcapi
2+
*
3+
* SPDX-License-Identifier: LGPL-2.1+ *
4+
*
5+
*/
6+
7+
#ifndef _PTHREAD_EXT_H
8+
#define _PTHREAD_EXT_H
9+
10+
#include "../lxc/compiler.h"
11+
12+
#ifndef PTHREAD_CANCELED
13+
#define PTHREAD_CANCELED ((void *)-1)
14+
#endif
15+
16+
__hidden extern int pthread_setcancelstate(int, int *);
17+
18+
#endif
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* liblxcapi
2+
*
3+
* SPDX-License-Identifier: LGPL-2.1+ *
4+
*
5+
* This is not a standard implementation.
6+
* It only protects getgrgid_r().
7+
*/
8+
9+
#include <pthread.h>
10+
#include "pthread_ext.h"
11+
12+
static const int signals[] = {
13+
SIGINT,
14+
SIGTERM,
15+
SIGQUIT,
16+
};
17+
18+
int pthread_setcancelstate(int state, int *oldstate)
19+
{
20+
sigset_t signal_set;
21+
sigset_t old_mask;
22+
sigemptyset(&signal_set);
23+
24+
for (size_t i = 0; i < sizeof(signals)/sizeof(signals[0]); i++)
25+
sigaddset(&signal_set, signals[i]);
26+
27+
int operation = (state == 1) ? SIG_UNBLOCK : SIG_BLOCK;
28+
return pthread_sigmask(operation, &signal_set, &old_mask);
29+
}

src/lxc/start.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@
6868
#include "strlcpy.h"
6969
#endif
7070

71+
#if IS_BIONIC
72+
#include <pthread_ext.h>
73+
#endif
74+
7175
#if HAVE_LANDLOCK_MONITOR
7276
#ifndef landlock_create_ruleset
7377
static inline int

0 commit comments

Comments
 (0)