macOS does not support sem_init. use sem_open instead

This commit is contained in:
Ryo Nakamura
2023-03-15 23:18:33 +09:00
parent e038b3020d
commit f0c70a148b
3 changed files with 43 additions and 6 deletions

View File

@@ -26,7 +26,7 @@ struct mscp {
int *cores; /* usable cpu cores by COREMASK */ int *cores; /* usable cpu cores by COREMASK */
int nr_cores; /* length of array of cores */ int nr_cores; /* length of array of cores */
sem_t sem; /* semaphore for concurrent sem_t *sem; /* semaphore for concurrent
* connecting ssh sessions */ * connecting ssh sessions */
sftp_session first; /* first sftp session */ sftp_session first; /* first sftp session */
@@ -204,9 +204,24 @@ static int validate_and_set_defaut_params(struct mscp_opts *o)
return 0; return 0;
} }
static int random_string(char *buf, size_t size)
{
char chars[] = "abcdefhijklmnopkwxyz1234567890";
int n, x;
for (n = 0; n < size - 1; n++) {
x = get_random(sizeof(chars) - 1);
buf[n] = chars[x];
}
buf[size - 1] = '\0';
return 0;
}
struct mscp *mscp_init(const char *remote_host, int direction, struct mscp *mscp_init(const char *remote_host, int direction,
struct mscp_opts *o, struct mscp_ssh_opts *s) struct mscp_opts *o, struct mscp_ssh_opts *s)
{ {
char sem_name[PSEMNAMLEN] = "mscp-";
struct mscp *m; struct mscp *m;
int n; int n;
@@ -237,8 +252,13 @@ struct mscp *mscp_init(const char *remote_host, int direction,
INIT_LIST_HEAD(&m->src_list); INIT_LIST_HEAD(&m->src_list);
INIT_LIST_HEAD(&m->path_list); INIT_LIST_HEAD(&m->path_list);
chunk_pool_init(&m->cp); chunk_pool_init(&m->cp);
if (sem_init(&m->sem, 0, o->max_startups) < 0) {
mscp_set_error("failed to initialize semaphore: %s", strerrno()); n = strlen(sem_name);
if (random_string(sem_name + n, sizeof(sem_name) - n - 1) < 0)
goto free_out;
if ((m->sem = sem_open(sem_name, O_CREAT, 600, o->max_startups)) == SEM_FAILED) {
mscp_set_error("sem_open: %s", strerrno());
goto free_out; goto free_out;
} }
@@ -567,7 +587,7 @@ void *mscp_copy_thread(void *arg)
} }
} }
if (sem_wait(&m->sem) < 0) { if (sem_wait(m->sem) < 0) {
mscp_set_error("sem_wait: %s\n", strerrno()); mscp_set_error("sem_wait: %s\n", strerrno());
mpr_err(m->msg_fp, "%s", mscp_get_error()); mpr_err(m->msg_fp, "%s", mscp_get_error());
goto err_out; goto err_out;
@@ -577,7 +597,7 @@ void *mscp_copy_thread(void *arg)
m->remote, t->id); m->remote, t->id);
t->sftp = ssh_init_sftp_session(m->remote, m->ssh_opts); t->sftp = ssh_init_sftp_session(m->remote, m->ssh_opts);
if (sem_post(&m->sem) < 0) { if (sem_post(m->sem) < 0) {
mscp_set_error("sem_post: %s\n", strerrno()); mscp_set_error("sem_post: %s\n", strerrno());
mpr_err(m->msg_fp, "%s", mscp_get_error()); mpr_err(m->msg_fp, "%s", mscp_get_error());
goto err_out; goto err_out;
@@ -688,6 +708,7 @@ void mscp_free(struct mscp *m)
free(m->remote); free(m->remote);
if (m->cores) if (m->cores)
free(m->cores); free(m->cores);
sem_close(m->sem);
free(m); free(m);
} }

View File

@@ -1,4 +1,5 @@
#ifdef __APPLE__ #ifdef __APPLE__
#include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#elif linux #elif linux
@@ -32,6 +33,11 @@ int set_thread_affinity(pthread_t tid, int core)
return 0; return 0;
} }
int get_random(int max)
{
return arc4random() % max;
}
#endif #endif
#ifdef linux #ifdef linux
@@ -56,5 +62,10 @@ int set_thread_affinity(pthread_t tid, int core)
core, strerrno()); core, strerrno());
return ret; return ret;
} }
int get_random(int max)
{
return random() % max;
}
#endif #endif

View File

@@ -3,7 +3,12 @@
#include <pthread.h> #include <pthread.h>
int nr_cpus(); #ifndef PSEMNAMLEN /* defined in macOS, but not in Linux */
#define PSEMNAMLEN 31
#endif
int nr_cpus(void);
int set_thread_affinity(pthread_t tid, int core); int set_thread_affinity(pthread_t tid, int core);
int get_random(int max);
#endif /* _PLATFORM_H_ */ #endif /* _PLATFORM_H_ */