softflowctl.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. static void
  26. usage(void)
  27. {
  28. fprintf(stderr, "Usage: [-c ctlsock] softflowctl [command]\n");
  29. }
  30. int
  31. main(int argc, char **argv)
  32. {
  33. const char *ctlsock_path;
  34. char buf[8192], *command;
  35. struct sockaddr_un ctl;
  36. socklen_t ctllen;
  37. int ctlsock, ch;
  38. FILE *ctlf;
  39. extern char *optarg;
  40. extern int optind;
  41. ctlsock_path = DEFAULT_CTLSOCK;
  42. while ((ch = getopt(argc, argv, "hc:")) != -1) {
  43. switch (ch) {
  44. case 'h':
  45. usage();
  46. return (0);
  47. case 'c':
  48. ctlsock_path = optarg;
  49. break;
  50. default:
  51. fprintf(stderr, "Invalid commandline option.\n");
  52. usage();
  53. exit(1);
  54. }
  55. }
  56. /* Accept only one argument */
  57. if (optind != argc - 1) {
  58. usage();
  59. exit(1);
  60. }
  61. command = argv[optind];
  62. memset(&ctl, '\0', sizeof(ctl));
  63. if (strlcpy(ctl.sun_path, ctlsock_path, sizeof(ctl.sun_path)) >=
  64. sizeof(ctl.sun_path)) {
  65. fprintf(stderr, "Control socket path too long.\n");
  66. exit(1);
  67. }
  68. ctl.sun_path[sizeof(ctl.sun_path) - 1] = '\0';
  69. ctl.sun_family = AF_UNIX;
  70. ctllen = offsetof(struct sockaddr_un, sun_path) +
  71. strlen(ctlsock_path) + 1;
  72. #ifdef SOCK_HAS_LEN
  73. ctl.sun_len = ctllen;
  74. #endif
  75. if ((ctlsock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
  76. fprintf(stderr, "ctl socket() error: %s\n",
  77. strerror(errno));
  78. exit(1);
  79. }
  80. if (connect(ctlsock, (struct sockaddr*)&ctl, sizeof(ctl)) == -1) {
  81. fprintf(stderr, "ctl connect(\"%s\") error: %s\n",
  82. ctl.sun_path, strerror(errno));
  83. exit(1);
  84. }
  85. if ((ctlf = fdopen(ctlsock, "r+")) == NULL) {
  86. fprintf(stderr, "fdopen: %s\n", strerror(errno));
  87. exit(1);
  88. }
  89. setlinebuf(ctlf);
  90. /* Send command */
  91. if (fprintf(ctlf, "%s\n", command) < 0) {
  92. fprintf(stderr, "write: %s\n", strerror(errno));
  93. exit(1);
  94. }
  95. /* Write out reply */
  96. while((fgets(buf, sizeof(buf), ctlf)) != NULL)
  97. fputs(buf, stdout);
  98. fclose(ctlf);
  99. close(ctlsock);
  100. exit(0);
  101. }