A small USV for ALIX boards with i2c support
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.0KB

  1. /***************************************************************************
  2. * Copyright (C) 07/2007 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; version 2 of the License *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <sys/un.h>
  25. #include <sys/stat.h>
  26. #include <errno.h>
  27. #include "logging.h"
  28. int unix_listen(const char *filename)
  29. {
  30. int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  31. if (sockfd == -1) {
  32. log_print(LOG_ERROR, "unix_listen: socket()");
  33. return -1;
  34. }
  35. if (fcntl(sockfd, F_SETFD, FD_CLOEXEC) < 0) {
  36. log_print(LOG_WARN, "unix_listen(): fcntl(FD_CLOEXEC)");
  37. return -1;
  38. }
  39. struct sockaddr_un addr;
  40. addr.sun_family = AF_UNIX;
  41. strncpy(addr.sun_path, filename, sizeof(addr.sun_path));
  42. int len = sizeof(addr.sun_family) + strlen(addr.sun_path);
  43. if (unlink(addr.sun_path) == -1 && errno != ENOENT) {
  44. log_print(LOG_ERROR, "unix_listen: unlink()");
  45. return -1;
  46. }
  47. errno = 0;
  48. mode_t old_umask = umask(0077);
  49. int ret = bind(sockfd, (struct sockaddr *) &addr, len);
  50. umask(old_umask);
  51. if (ret == -1) {
  52. log_print(LOG_ERROR, "unix_listen: bind(%s)", filename);
  53. close(sockfd);
  54. return -1;
  55. }
  56. if (listen(sockfd, 5) == -1) {
  57. log_print(LOG_ERROR, "unix_listen: listen()");
  58. close(sockfd);
  59. return -1;
  60. }
  61. return sockfd;
  62. }
  63. int unix_connect(const char *filename)
  64. {
  65. int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  66. if (sockfd == -1) {
  67. log_print(LOG_ERROR, "unix_connect: socket()");
  68. return -1;
  69. }
  70. struct sockaddr_un addr;
  71. addr.sun_family = AF_UNIX;
  72. strncpy(addr.sun_path, filename, sizeof(addr.sun_path));
  73. int len = sizeof(addr.sun_family) + strlen(addr.sun_path);
  74. if (connect(sockfd, (struct sockaddr *)&addr, len) < 0) {
  75. log_print(LOG_ERROR, "unix_connect: connect(%s)", filename);
  76. close(sockfd);
  77. return -1;
  78. }
  79. return sockfd;
  80. }