softflowctl.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2002 Damien Miller <djm@mindrot.org> All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  14. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  15. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  18. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  20. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "common.h"
  25. RCSID("$Id$");
  26. static void
  27. usage(void)
  28. {
  29. fprintf(stderr, "Usage: [-c ctlsock] softflowctl [command]\n");
  30. }
  31. int
  32. main(int argc, char **argv)
  33. {
  34. const char *ctlsock_path;
  35. char buf[8192], *command;
  36. struct sockaddr_un ctl;
  37. socklen_t ctllen;
  38. int ctlsock, ch;
  39. FILE *ctlf;
  40. extern char *optarg;
  41. extern int optind;
  42. ctlsock_path = DEFAULT_CTLSOCK;
  43. while ((ch = getopt(argc, argv, "hc:")) != -1) {
  44. switch (ch) {
  45. case 'h':
  46. usage();
  47. return (0);
  48. case 'c':
  49. ctlsock_path = optarg;
  50. break;
  51. default:
  52. fprintf(stderr, "Invalid commandline option.\n");
  53. usage();
  54. exit(1);
  55. }
  56. }
  57. /* Accept only one argument */
  58. if (optind != argc - 1) {
  59. usage();
  60. exit(1);
  61. }
  62. command = argv[optind];
  63. memset(&ctl, '\0', sizeof(ctl));
  64. if (strlcpy(ctl.sun_path, ctlsock_path, sizeof(ctl.sun_path)) >=
  65. sizeof(ctl.sun_path)) {
  66. fprintf(stderr, "Control socket path too long.\n");
  67. exit(1);
  68. }
  69. ctl.sun_path[sizeof(ctl.sun_path) - 1] = '\0';
  70. ctl.sun_family = AF_UNIX;
  71. ctllen = offsetof(struct sockaddr_un, sun_path) +
  72. strlen(ctlsock_path) + 1;
  73. #ifdef SOCK_HAS_LEN
  74. ctl.sun_len = ctllen;
  75. #endif
  76. if ((ctlsock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
  77. fprintf(stderr, "ctl socket() error: %s\n",
  78. strerror(errno));
  79. exit(1);
  80. }
  81. if (connect(ctlsock, (struct sockaddr*)&ctl, sizeof(ctl)) == -1) {
  82. fprintf(stderr, "ctl connect(\"%s\") error: %s\n",
  83. ctl.sun_path, strerror(errno));
  84. exit(1);
  85. }
  86. if ((ctlf = fdopen(ctlsock, "r+")) == NULL) {
  87. fprintf(stderr, "fdopen: %s\n", strerror(errno));
  88. exit(1);
  89. }
  90. setlinebuf(ctlf);
  91. /* Send command */
  92. if (fprintf(ctlf, "%s\n", command) < 0) {
  93. fprintf(stderr, "write: %s\n", strerror(errno));
  94. exit(1);
  95. }
  96. /* Write out reply */
  97. while((fgets(buf, sizeof(buf), ctlf)) != NULL)
  98. fputs(buf, stdout);
  99. fclose(ctlf);
  100. close(ctlsock);
  101. exit(0);
  102. }