PageRenderTime 29ms CodeModel.GetById 20ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/modules/netgroup.c

https://code.google.com/
C | 212 lines | 136 code | 36 blank | 40 comment | 20 complexity | 73a14db91509241b3805c2e291c295f0 MD5 | raw file
  1/*****************************************************************************\
  2 *  $Id$
  3 *****************************************************************************
  4 *  Copyright (C) 2005-2006 The Regents of the University of California.
  5 *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
  6 *  Written by Mark Grondona <mgrondona@llnl.gov>
  7 *  UCRL-CODE-2003-005.
  8 *  
  9 *  This file is part of Pdsh, a parallel remote shell program.
 10 *  For details, see <http://www.llnl.gov/linux/pdsh/>.
 11 *  
 12 *  Pdsh is free software; you can redistribute it and/or modify it under
 13 *  the terms of the GNU General Public License as published by the Free
 14 *  Software Foundation; either version 2 of the License, or (at your option)
 15 *  any later version.
 16 *  
 17 *  Pdsh is distributed in the hope that it will be useful, but WITHOUT ANY
 18 *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 19 *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 20 *  details.
 21 *  
 22 *  You should have received a copy of the GNU General Public License along
 23 *  with Pdsh; if not, write to the Free Software Foundation, Inc.,
 24 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 25\*****************************************************************************/
 26
 27#if HAVE_CONFIG_H
 28#  include "config.h"
 29#endif
 30
 31#include <stdlib.h> /* getenv */
 32#include <string.h>
 33#include <netdb.h>
 34
 35#include "src/pdsh/wcoll.h"
 36#include "src/pdsh/mod.h"
 37#include "src/common/hostlist.h"
 38#include "src/common/xmalloc.h"
 39#include "src/common/err.h"
 40#include "src/common/list.h"
 41#include "src/common/split.h"
 42
 43#if STATIC_MODULES
 44#  define pdsh_module_info netgroup_module_info
 45#  define pdsh_module_priority netgroup_module_priority
 46#endif    
 47
 48int pdsh_module_priority = DEFAULT_MODULE_PRIORITY;
 49
 50static hostlist_t read_netgroup(opt_t *opt);
 51static int netgroup_postop (opt_t *);
 52static int netgroup_process_opt(opt_t *, int, char *);
 53
 54static List groups = NULL;
 55static List exgroups = NULL;
 56
 57/* 
 58 * Export pdsh module operations structure
 59 */
 60struct pdsh_module_operations netgroup_module_ops = {
 61    (ModInitF)       NULL, 
 62    (ModExitF)       NULL, 
 63    (ModReadWcollF)  read_netgroup, 
 64    (ModPostOpF)     netgroup_postop,
 65};
 66
 67/* 
 68 * Export rcmd module operations
 69 */
 70struct pdsh_rcmd_operations netgroup_rcmd_ops = {
 71    (RcmdInitF)  NULL,
 72    (RcmdSigF)   NULL,
 73    (RcmdF)      NULL,
 74};
 75
 76/* 
 77 * Export module options
 78 */
 79struct pdsh_module_option netgroup_module_options[] = 
 80 { { 'g', "groupname", "target hosts in netgroup \"groupname\"", 
 81   DSH | PCP, (optFunc) netgroup_process_opt },
 82   { 'X', "groupname", "exclude hosts in netgroup \"groupname\"", 
 83   DSH | PCP, (optFunc) netgroup_process_opt },
 84   PDSH_OPT_TABLE_END
 85 };
 86
 87/*
 88 * Machines module info 
 89 */
 90struct pdsh_module pdsh_module_info = {
 91  "misc",
 92  "netgroup",
 93  "Mark Grondona <mgrondona@llnl.gov>",
 94  "Target netgroups from pdsh",
 95  DSH | PCP, 
 96  &netgroup_module_ops,
 97  &netgroup_rcmd_ops,
 98  &netgroup_module_options[0],
 99};
100
101static int netgroup_process_opt(opt_t *pdsh_opt, int opt, char *arg)
102{
103    switch (opt) {
104    case 'g':
105        groups = list_split_append (groups, ",", arg);
106        break;
107    case 'X':
108        exgroups = list_split_append (exgroups, ",", arg);
109        break;
110    default:
111        err ("%p: netgroup_process_opt: invalid option `%c'\n", opt);
112        return -1;
113        break;
114    }
115    return 0;
116}
117
118static hostlist_t _read_netgroup (const char *group)
119{
120	hostlist_t hl = NULL;
121	char *host, *user, *domain;
122	char buf[4096];
123    int rc;
124
125	setnetgrent (group);
126
127	while (rc = getnetgrent_r (&host, &user, &domain, buf, sizeof (buf))) {
128		if (hl == NULL)
129			hl = hostlist_create (host);
130		else
131			hostlist_push (hl, host);
132	}
133
134	endnetgrent ();
135
136	return (hl);
137}
138
139static hostlist_t _read_groups (List grouplist)
140{
141    ListIterator i  = NULL;
142    hostlist_t   hl = NULL;
143    char *group;
144
145    i = list_iterator_create (grouplist);
146
147    while ((group = list_next (i))) {
148        hostlist_t l = _read_netgroup (group);
149
150        if (l == NULL)
151            continue;
152    
153        if (hl == NULL) {
154            hl = l;
155        } else {
156            hostlist_push_list (hl, l);
157            hostlist_destroy (l);
158        }
159    }
160
161    list_iterator_destroy (i);
162
163    if (hl != NULL)
164        hostlist_uniq (hl);
165
166    return (hl);
167}
168
169static hostlist_t read_netgroup (opt_t *opt)
170{
171    if (!groups)
172        return NULL;
173
174    if (opt->wcoll && groups)
175        errx("Do not specify both -w and -g");
176
177    return _read_groups (groups);
178}
179
180static int
181_delete_all (hostlist_t hl, hostlist_t dl)
182{
183    int                 rc   = 0;
184    char *              host = NULL;
185    hostlist_iterator_t i    = hostlist_iterator_create (dl);
186
187    while ((host = hostlist_next (i))) {
188        rc += hostlist_delete_host (hl, host);
189        free (host);
190    }
191    hostlist_iterator_destroy (i);
192    return (rc);
193}
194
195static int netgroup_postop (opt_t *opt)
196{
197    hostlist_t hl = NULL;
198    
199    if (!opt->wcoll || !exgroups)
200        return (0);
201
202    if ((hl = _read_groups (exgroups)) == NULL)
203        return (0);
204
205    _delete_all (opt->wcoll, hl);
206
207    return 0;
208}
209
210/*
211 * vi: tabstop=4 shiftwidth=4 expandtab
212 */