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