Hi, I coded up the first bits for pthread_atfork() handling. I have no real application to validate it against, but while I work on a test case I wanted to get early feedback to see if we should look at this direction.
Fix library locking during fork() and exec() by threads From: Balbir Singh <[email protected]> Locking is inherently broken and can be painful for applications, if the library explicitly uses mutexes without a pthread_atfork() handler. The code is a first pass at solving the issue brought up. The current patch deals only with api.c, config.c is spared at the moment assuming that it used only by configuration parsers and they as of NOW do not care about fork() and exec() scenarios from threads. More test cases are required to validate the changes Signed-off-by: Balbir Singh <[email protected]> --- src/api.c | 28 ++++++++++++++++++++++++++++ 1 files changed, 28 insertions(+), 0 deletions(-) diff --git a/src/api.c b/src/api.c index 4823006..fd750a2 100644 --- a/src/api.c +++ b/src/api.c @@ -118,6 +118,28 @@ const char const *cgroup_strerror_codes[] = { "Cannot have mount and namespace keyword in the same configuration file", }; +/** + * Handle locking issues during fork and exec. Follow the standard patter + * prepare grabs all locks + */ +static void cg_fork_prepare(void) +{ + pthread_rwlock_wrlock(&cg_mount_table_lock); + pthread_rwlock_wrlock(&rl_lock); +} + +static void cg_fork_parent(void) +{ + pthread_rwlock_unlock(&rl_lock); + pthread_rwlock_unlock(&cg_mount_table_lock); +} + +static void cg_fork_child(void) +{ + pthread_rwlock_unlock(&rl_lock); + pthread_rwlock_unlock(&cg_mount_table_lock); +} + static int cg_chown_file(FTS *fts, FTSENT *ent, uid_t owner, gid_t group) { int ret = 0; @@ -665,6 +687,12 @@ int cgroup_init(void) char mntent_buffer[4 * FILENAME_MAX]; char *strtok_buffer = NULL; + err = pthread_atfork(cg_fork_prepare, cg_fork_parent, cg_fork_child); + if (err) { + last_errno = errno; + return ECGOTHER; + } + pthread_rwlock_wrlock(&cg_mount_table_lock); proc_cgroup = fopen("/proc/cgroups", "re"); -- Three Cheers, Balbir ------------------------------------------------------------------------------ This SF.net email is sponsored by Sprint What will you do first with EVO, the first 4G phone? Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first _______________________________________________ Libcg-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/libcg-devel
