Skip to content

Commit

Permalink
Implement diodmount --jobid that can start one server per job.
Browse files Browse the repository at this point in the history
Fix some bugs that prevented 'service diodctl restart' from working properly.
  • Loading branch information
garlick committed Apr 9, 2010
1 parent bbe945b commit 2b88e08
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 97 deletions.
26 changes: 21 additions & 5 deletions ChangeLog
@@ -1,12 +1,28 @@
2010-04-08 Jim Garlick <garlick@llnl.gov>
2010-04-09 Jim Garlick <garlick@llnl.gov>

* diodctl/serv.c, diod/diod.c, common/diod_sock.c : Fix bug where
diodctl's listen socket remained open in its children, preventing
it from being independently restarted.

* scripts/diodctl.init.in : In service_restart, call service_stop
and service_start directly instead of $0 stop && $0 start.
For some reason $0 start did not work in this context.

* diodctl/serv.c : Use jobid in addition to uid to identify servers.
Set name of child diod to descriptive name like diod-shared or
diod-jobid-%s or diod-uid-%d.

* diodctl/serv.c : Instead of storing the uid in the server struct,
store a pointer to the Npuser so that we can easily get at the
munge payload later.
* diodctl/ops.c : Pass jobid, stored in transport, into
diodctl_serv_create () and diodctl_serv_getname ().

* common/diod_trans.c : Store jobid with munge info in transport.
Access with diod_trans_set_authuser() and diod_trans_get_jobid().

2010-04-08 Jim Garlick <garlick@llnl.gov>

* common/diod_upool.c : If munge cred includes a payload, store it
with the Npuser struct, and allow it to be retrieved with
diod_upool_get_mungepayload.
diod_user_get_authinfo.

* diodmount/diodmount.c : Add -j (jobid) option, and embed string
in munge payload.
Expand Down
6 changes: 3 additions & 3 deletions common/diod_sock.c
Expand Up @@ -178,12 +178,12 @@ diod_sock_listen_hostport_list (List l, struct pollfd **fdsp, int *nfdsp,
return ret;
}

/* Listen on the first nfds file descriptors, which are assumed to be
/* Listen on the first [nfds] fds after [starting], which are assumed to be
* open and bound to appropriate addresses.
* Return 0 on failure, nonzero on success.
*/
int
diod_sock_listen_first_nfds (struct pollfd **fdsp, int *nfdsp, int nfds)
diod_sock_listen_nfds (struct pollfd **fdsp, int *nfdsp, int nfds, int starting)
{
struct pollfd *fds;
int i;
Expand All @@ -194,7 +194,7 @@ diod_sock_listen_first_nfds (struct pollfd **fdsp, int *nfdsp, int nfds)
goto done;
}
for (i = 0; i < nfds; i++)
fds[i].fd = i;
fds[i].fd = starting + i;
*nfdsp = nfds;
*fdsp = fds;

Expand Down
3 changes: 2 additions & 1 deletion common/diod_sock.h
Expand Up @@ -3,7 +3,8 @@ enum {
DIOD_SOCK_QUIET_EADDRINUSE=2,
};

int diod_sock_listen_first_nfds (struct pollfd **fdsp, int *nfdsp, int nfds);
int diod_sock_listen_nfds (struct pollfd **fdsp, int *nfdsp, int nfds,
int starting);

int diod_sock_listen_hostport_list (List l, struct pollfd **fdsp, int *nfdsp,
char *nport, int flags);
Expand Down
30 changes: 21 additions & 9 deletions common/diod_trans.c
Expand Up @@ -50,6 +50,7 @@ typedef struct {
int magic;
int authenticated;
uid_t authuser;
char *jobid;
} DTrans;

#define DIOD_TRANS_MAGIC 0xf00fbaaa
Expand All @@ -64,24 +65,21 @@ diod_trans_create (int fd, char *host, char *ip, char *svc)
Nptrans *npt;
DTrans *dt;

dt = malloc (sizeof(*dt));
if (!dt)
if (!(dt = malloc (sizeof(*dt))))
return NULL;
dt->magic = DIOD_TRANS_MAGIC;
dt->fd = fd;
dt->authenticated = 0;
dt->host = strdup (host);
if (!dt->host) {
dt->jobid = NULL;
if (!(dt->host = strdup (host))) {
diod_trans_destroy (dt);
return NULL;
}
dt->ip = strdup (ip);
if (!dt->ip) {
if (!(dt->ip = strdup (ip))) {
diod_trans_destroy (dt);
return NULL;
}
dt->svc = strdup (svc);
if (!dt->svc) {
if (!(dt->svc = strdup (svc))) {
diod_trans_destroy (dt);
return NULL;
}
Expand Down Expand Up @@ -111,6 +109,8 @@ diod_trans_destroy (void *a)
free (dt->ip);
if (dt->svc)
free (dt->svc);
if (dt->jobid)
free (dt->jobid);

free (dt);
}
Expand Down Expand Up @@ -166,14 +166,16 @@ diod_trans_get_svc (Nptrans *trans)
}

void
diod_trans_set_authuser (Nptrans *trans, uid_t uid)
diod_trans_set_authuser (Nptrans *trans, uid_t uid, char *jobid)
{
DTrans *dt = trans->aux;

assert (dt->magic == DIOD_TRANS_MAGIC);

dt->authuser = uid;
dt->authenticated = 1;
dt->jobid = jobid;

}

int
Expand All @@ -192,6 +194,16 @@ diod_trans_get_authuser (Nptrans *trans, uid_t *uidp)
return ret;
}

char *
diod_trans_get_jobid (Nptrans *trans)
{
DTrans *dt = trans->aux;

assert (dt->magic == DIOD_TRANS_MAGIC);

return dt->jobid;
}

/*
* vi:tabstop=4 shiftwidth=4 expandtab
*/
3 changes: 2 additions & 1 deletion common/diod_trans.h
Expand Up @@ -26,8 +26,9 @@ void diod_trans_destroy (void *trans);
char *diod_trans_get_host (Nptrans *trans);
char *diod_trans_get_ip (Nptrans *trans);
char *diod_trans_get_svc (Nptrans *trans);
void diod_trans_set_authuser (Nptrans *trans, uid_t uid);
void diod_trans_set_authuser (Nptrans *trans, uid_t uid, char *jobid);
int diod_trans_get_authuser (Nptrans *trans, uid_t *uidp);
char *diod_trans_get_jobid (Nptrans *trans);

/*
* vi:tabstop=4 shiftwidth=4 expandtab
Expand Down
51 changes: 29 additions & 22 deletions common/diod_upool.c
Expand Up @@ -101,7 +101,7 @@ static Npuserpool upool = {
typedef struct {
int magic;
int munged; /* user supplied a valid munge cred */
char *payload; /* munge payload (if any) */
char *jobid; /* munge payload (if any) */
int nsg; /* number of supplementary groups */
gid_t sg[NGROUPS_MAX];/* supplementary gid array */
} Duser;
Expand Down Expand Up @@ -143,8 +143,8 @@ diod_switch_user (Npuser *u)
static void
_free_duser (Duser *d)
{
if (d->payload)
free (d->payload);
if (d->jobid)
free (d->jobid);
free (d);
}

Expand Down Expand Up @@ -177,15 +177,15 @@ _getsg (struct passwd *pwd, gid_t *gp, int *glenp)
}

static Duser *
_alloc_duser (struct passwd *pwd, int munged, char *payload)
_alloc_duser (struct passwd *pwd, int munged, char *jobid)
{
Duser *d = NULL;

if (!(d = np_malloc (sizeof (*d))))
goto done;
d->magic = DUSER_MAGIC;
d->munged = munged;
d->payload = payload;
d->jobid = jobid;
if (!_getsg (pwd, d->sg, &d->nsg))
np_uerror (errno);
done:
Expand All @@ -207,12 +207,19 @@ diod_become_user (char *name, uid_t uid, int realtoo)
char buf[PASSWD_BUFSIZE];
int nsg;
gid_t sg[NGROUPS_MAX];
char *endptr;

if (name) {
errno = 0;
uid = strtoul (name, &endptr, 10);
if (errno == 0 && *name != '\0' && *endptr == '\0')
name = NULL;
}
if (name) {
if ((err = getpwnam_r (name, &pw, buf, sizeof(buf), &pwd)) != 0)
errn_exit (err, "error looking up uid %d", uid);
errn_exit (err, "error looking up user %s", name);
if (!pwd)
msg_exit ("error looking up uid %d", uid);
msg_exit ("error looking up user %s", name);
} else {
if ((err = getpwuid_r (uid, &pw, buf, sizeof(buf), &pwd)) != 0)
errn_exit (err, "error looking up uid %d", uid);
Expand All @@ -231,15 +238,14 @@ diod_become_user (char *name, uid_t uid, int realtoo)


static Npuser *
_alloc_user (Npuserpool *up, struct passwd *pwd, int munged,
char *payload)
_alloc_user (Npuserpool *up, struct passwd *pwd, int munged, char *jobid)
{
Npuser *u;
int err;

if (!(u = np_malloc (sizeof (*u))))
goto done;
if (!(u->aux = _alloc_duser (pwd, munged, payload)))
if (!(u->aux = _alloc_duser (pwd, munged, jobid)))
goto done;
if ((err = pthread_mutex_init (&u->lock, NULL)) != 0) {
np_uerror (err);
Expand Down Expand Up @@ -282,23 +288,24 @@ diod_udestroy (Npuserpool *up, Npuser *u)
}

int
diod_user_has_mungecred (Npuser *u)
diod_user_get_authinfo (Npuser *u, int *authenticated, char **jobidp)
{
Duser *d = u->aux;
char *cpy = NULL;
int res = 0;

assert (d->magic == DUSER_MAGIC);

return d->munged;
}

char *
diod_user_get_mungepayload (Npuser *u)
{
Duser *d = u->aux;

assert (d->magic == DUSER_MAGIC);

return d->payload;
if (d->munged && jobidp) {
if (d->jobid && !(cpy = strdup (d->jobid))) {
res = -1;
goto done;
}
*jobidp = cpy;
}
*authenticated = d->munged;
done:
return res;
}

static int
Expand Down
5 changes: 2 additions & 3 deletions common/diod_upool.h
Expand Up @@ -22,10 +22,9 @@
*****************************************************************************/

int diod_switch_user (Npuser *u);
int diod_user_has_mungecred (Npuser *u);
char *diod_user_get_mungepayload (Npuser *u);
char *diod_user_get_jobid (Npuser *u);
int diod_switch_user (Npuser *u);
void diod_become_user (char *name, uid_t uid, int realtoo);

int diod_user_get_authinfo (Npuser *u, int *authenticated, char **jobidp);

extern Npuserpool *diod_upool;
2 changes: 1 addition & 1 deletion diod/diod.c
Expand Up @@ -209,7 +209,7 @@ main(int argc, char **argv)
if (!srv)
msg_exit ("out of memory");
if (Fopt) {
if (!diod_sock_listen_first_nfds (&fds, &nfds, Fopt))
if (!diod_sock_listen_nfds (&fds, &nfds, Fopt, 3))
msg_exit ("failed to set up listen ports");
} else {
hplist = diod_conf_get_diodlisten ();
Expand Down
7 changes: 5 additions & 2 deletions diod/ops.c
Expand Up @@ -577,8 +577,11 @@ diod_attach (Npfid *fid, Npfid *nafid, Npstr *uname, Npstr *aname)
* By the time we get here, invalid munge creds have already been rejected.
*/
if (diod_conf_get_munge ()) {
if (diod_user_has_mungecred (fid->user)) {
diod_trans_set_authuser (fid->conn->trans, fid->user->uid);
int authenticated;

(void) diod_user_get_authinfo (fid->user, &authenticated, NULL);
if (authenticated) {
diod_trans_set_authuser (fid->conn->trans, fid->user->uid, NULL);
} else {
if (diod_trans_get_authuser (fid->conn->trans, &auid) < 0) {
np_uerror (EPERM);
Expand Down
18 changes: 14 additions & 4 deletions diodctl/ops.c
Expand Up @@ -96,8 +96,16 @@ _ctl_attach (Npfid *fid, Npfid *nafid, Npstr *uname, Npstr *aname)
* By the time we get here, invalid munge creds have already been rejected.
*/
if (diod_conf_get_munge ()) {
if (diod_user_has_mungecred (fid->user)) {
diod_trans_set_authuser (fid->conn->trans, fid->user->uid);
char *jobid;
int authenticated;

if (diod_user_get_authinfo (fid->user, &authenticated, &jobid) < 0) {
np_uerror (ENOMEM);
msg ("diodctl_attach: out of memory");
goto done;
}
if (authenticated) {
diod_trans_set_authuser (fid->conn->trans, fid->user->uid, jobid);
} else {
if (diod_trans_get_authuser (fid->conn->trans, &auid) < 0) {
np_uerror (EPERM);
Expand Down Expand Up @@ -181,8 +189,9 @@ static int
_server_read (Npfilefid* file, u64 offset, u32 count, u8* data, Npreq *req)
{
Npfid *fid = file->fid;
char *jobid = diod_trans_get_jobid (fid->conn->trans);

return diodctl_serv_getname (fid->user, offset, count, data);
return diodctl_serv_getname (fid->user, jobid, offset, count, data);
}

/* Handle a write to the 'ctl' file.
Expand All @@ -192,9 +201,10 @@ static int
_ctl_write (Npfilefid* file, u64 offset, u32 count, u8* data, Npreq *req)
{
Npfid *fid = file->fid;
char *jobid = diod_trans_get_jobid (fid->conn->trans);
int ret = 0;

if (diodctl_serv_create (fid->user))
if (diodctl_serv_create (fid->user, jobid))
ret = count;
return ret;
}
Expand Down

0 comments on commit 2b88e08

Please sign in to comment.