/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. #if HAVE_CONFIG_H
  27. # include "config.h"
  28. #endif
  29. #include <stdlib.h> /* getenv */
  30. #include <string.h>
  31. #include <netdb.h>
  32. #include "src/pdsh/wcoll.h"
  33. #include "src/pdsh/mod.h"
  34. #include "src/common/hostlist.h"
  35. #include "src/common/xmalloc.h"
  36. #include "src/common/err.h"
  37. #include "src/common/list.h"
  38. #include "src/common/split.h"
  39. #if STATIC_MODULES
  40. # define pdsh_module_info netgroup_module_info
  41. # define pdsh_module_priority netgroup_module_priority
  42. #endif
  43. int pdsh_module_priority = DEFAULT_MODULE_PRIORITY;
  44. static hostlist_t read_netgroup(opt_t *opt);
  45. static int netgroup_postop (opt_t *);
  46. static int netgroup_process_opt(opt_t *, int, char *);
  47. static List groups = NULL;
  48. static List exgroups = NULL;
  49. /*
  50. * Export pdsh module operations structure
  51. */
  52. struct pdsh_module_operations netgroup_module_ops = {
  53. (ModInitF) NULL,
  54. (ModExitF) NULL,
  55. (ModReadWcollF) read_netgroup,
  56. (ModPostOpF) netgroup_postop,
  57. };
  58. /*
  59. * Export rcmd module operations
  60. */
  61. struct pdsh_rcmd_operations netgroup_rcmd_ops = {
  62. (RcmdInitF) NULL,
  63. (RcmdSigF) NULL,
  64. (RcmdF) NULL,
  65. };
  66. /*
  67. * Export module options
  68. */
  69. struct pdsh_module_option netgroup_module_options[] =
  70. { { 'g', "groupname", "target hosts in netgroup \"groupname\"",
  71. DSH | PCP, (optFunc) netgroup_process_opt },
  72. { 'X', "groupname", "exclude hosts in netgroup \"groupname\"",
  73. DSH | PCP, (optFunc) netgroup_process_opt },
  74. PDSH_OPT_TABLE_END
  75. };
  76. /*
  77. * Machines module info
  78. */
  79. struct pdsh_module pdsh_module_info = {
  80. "misc",
  81. "netgroup",
  82. "Mark Grondona <mgrondona@llnl.gov>",
  83. "Target netgroups from pdsh",
  84. DSH | PCP,
  85. &netgroup_module_ops,
  86. &netgroup_rcmd_ops,
  87. &netgroup_module_options[0],
  88. };
  89. static int netgroup_process_opt(opt_t *pdsh_opt, int opt, char *arg)
  90. {
  91. switch (opt) {
  92. case 'g':
  93. groups = list_split_append (groups, ",", arg);
  94. break;
  95. case 'X':
  96. exgroups = list_split_append (exgroups, ",", arg);
  97. break;
  98. default:
  99. err ("%p: netgroup_process_opt: invalid option `%c'\n", opt);
  100. return -1;
  101. break;
  102. }
  103. return 0;
  104. }
  105. static hostlist_t _read_netgroup (const char *group)
  106. {
  107. hostlist_t hl = NULL;
  108. char *host, *user, *domain;
  109. char buf[4096];
  110. int rc;
  111. setnetgrent (group);
  112. while (rc = getnetgrent_r (&host, &user, &domain, buf, sizeof (buf))) {
  113. if (hl == NULL)
  114. hl = hostlist_create (host);
  115. else
  116. hostlist_push (hl, host);
  117. }
  118. endnetgrent ();
  119. return (hl);
  120. }
  121. static hostlist_t _read_groups (List grouplist)
  122. {
  123. ListIterator i = NULL;
  124. hostlist_t hl = NULL;
  125. char *group;
  126. i = list_iterator_create (grouplist);
  127. while ((group = list_next (i))) {
  128. hostlist_t l = _read_netgroup (group);
  129. if (l == NULL)
  130. continue;
  131. if (hl == NULL) {
  132. hl = l;
  133. } else {
  134. hostlist_push_list (hl, l);
  135. hostlist_destroy (l);
  136. }
  137. }
  138. list_iterator_destroy (i);
  139. if (hl != NULL)
  140. hostlist_uniq (hl);
  141. return (hl);
  142. }
  143. static hostlist_t read_netgroup (opt_t *opt)
  144. {
  145. if (!groups)
  146. return NULL;
  147. if (opt->wcoll && groups)
  148. errx("Do not specify both -w and -g");
  149. return _read_groups (groups);
  150. }
  151. static int
  152. _delete_all (hostlist_t hl, hostlist_t dl)
  153. {
  154. int rc = 0;
  155. char * host = NULL;
  156. hostlist_iterator_t i = hostlist_iterator_create (dl);
  157. while ((host = hostlist_next (i))) {
  158. rc += hostlist_delete_host (hl, host);
  159. free (host);
  160. }
  161. hostlist_iterator_destroy (i);
  162. return (rc);
  163. }
  164. static int netgroup_postop (opt_t *opt)
  165. {
  166. hostlist_t hl = NULL;
  167. if (!opt->wcoll || !exgroups)
  168. return (0);
  169. if ((hl = _read_groups (exgroups)) == NULL)
  170. return (0);
  171. _delete_all (opt->wcoll, hl);
  172. return 0;
  173. }
  174. /*
  175. * vi: tabstop=4 shiftwidth=4 expandtab
  176. */